Browse Source

fix potential error

nayooti 5 years ago
parent
commit
2edaf06bd6
1 changed files with 29 additions and 4 deletions
  1. 29 4
      deltachat-ios/Handler/TabBarRestorer.swift

+ 29 - 4
deltachat-ios/Handler/TabBarRestorer.swift

@@ -2,15 +2,40 @@ import UIKit
 
 
 class TabBarRestorer: NSObject, UITabBarControllerDelegate {
 class TabBarRestorer: NSObject, UITabBarControllerDelegate {
 
 
-    private let userDefaultKey = "last_active_tab"
+    private let lastActiveTabKey = "last_active_tab"
+    private let offsetKey = 10
+
+    // UserDefaults returns 0 by default which conflicts with tab 0 -> therefore we map our tab indexes by adding an offsetKey
+
+    private enum Tab: Int {
+        case qrTab = 10
+        case chatTab = 11
+        case settingsTab = 12
+        case firstLaunch = 0
+    }
 
 
     func restoreLastActiveTab() -> Int {
     func restoreLastActiveTab() -> Int {
-        return UserDefaults.standard.integer(forKey: userDefaultKey)
+
+        let restoredTab = UserDefaults.standard.integer(forKey: lastActiveTabKey)
+
+        guard let lastTab = Tab(rawValue: restoredTab) else {
+            safe_fatalError("invalid restored tab")
+            return -1
+        }
+
+        switch lastTab {
+        case .qrTab, .chatTab, .settingsTab:
+            return lastTab.rawValue - offsetKey
+        case .firstLaunch:
+            return -1
+        }
     }
     }
 
 
     func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
     func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
-        let activeTab = tabBarController.selectedIndex
-        UserDefaults.standard.set(activeTab, forKey: userDefaultKey)
+        let activeTab = tabBarController.selectedIndex + offsetKey
+        UserDefaults.standard.set(activeTab, forKey: lastActiveTabKey)
         UserDefaults.standard.synchronize()
         UserDefaults.standard.synchronize()
     }
     }
+
+
 }
 }