Commit 799a4cad authored by Hadrien De Sousa's avatar Hadrien De Sousa

launch: fix account loading

This commit fixes accounts loading at launch that displayed the 
smartlist even if there was no accounts

Change-Id: I87e3aeffa9e780d7a1ff43f4511a331a0d5d987f
parent 1122c618
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
import UIKit import UIKit
import RealmSwift import RealmSwift
import SwiftyBeaver import SwiftyBeaver
import RxSwift
@UIApplicationMain @UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate { class AppDelegate: UIResponder, UIApplicationDelegate {
...@@ -33,8 +34,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate { ...@@ -33,8 +34,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
static let conversationsService = ConversationsService(withMessageAdapter: MessagesAdapter()) static let conversationsService = ConversationsService(withMessageAdapter: MessagesAdapter())
private let log = SwiftyBeaver.self private let log = SwiftyBeaver.self
fileprivate let disposeBag = DisposeBag()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
// initialize log format // initialize log format
let console = ConsoleDestination() let console = ConsoleDestination()
console.format = "$Dyyyy-MM-dd HH:mm:ss.SSS$d $C$L$c: $M" console.format = "$Dyyyy-MM-dd HH:mm:ss.SSS$d $C$L$c: $M"
...@@ -42,6 +47,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { ...@@ -42,6 +47,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
SystemAdapter().registerConfigurationHandler() SystemAdapter().registerConfigurationHandler()
self.startDaemon() self.startDaemon()
self.loadAccounts()
return true return true
} }
...@@ -66,7 +72,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { ...@@ -66,7 +72,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
do { do {
try AppDelegate.daemonService.startDaemon() try AppDelegate.daemonService.startDaemon()
AppDelegate.accountService.loadAccounts()
} catch StartDaemonError.initializationFailure { } catch StartDaemonError.initializationFailure {
log.error("Daemon failed to initialize.") log.error("Daemon failed to initialize.")
} catch StartDaemonError.startFailure { } catch StartDaemonError.startFailure {
...@@ -87,4 +93,33 @@ class AppDelegate: UIResponder, UIApplicationDelegate { ...@@ -87,4 +93,33 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
log.error("Unknown error in Daemon stop.") log.error("Unknown error in Daemon stop.")
} }
} }
fileprivate func loadAccounts() {
AppDelegate.accountService.loadAccounts()
.subscribe(onSuccess: { (accountList: [AccountModel]) in
self.checkAccount(accountList: accountList)
}, onError: { _ in
self.presentWalkthrough()
}).disposed(by: disposeBag)
}
fileprivate func checkAccount(accountList: [AccountModel]) {
if accountList.isEmpty {
self.presentWalkthrough()
} else {
self.presentMainTabBar()
}
}
fileprivate func presentWalkthrough() {
let storyboard = UIStoryboard(name: "WalkthroughStoryboard", bundle: nil)
self.window?.rootViewController = storyboard.instantiateInitialViewController()
self.window?.makeKeyAndVisible()
}
fileprivate func presentMainTabBar() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
self.window?.rootViewController = storyboard.instantiateInitialViewController()
self.window?.makeKeyAndVisible()
}
} }
...@@ -45,7 +45,7 @@ enum StoryboardScene { ...@@ -45,7 +45,7 @@ enum StoryboardScene {
enum LaunchScreen: StoryboardSceneType { enum LaunchScreen: StoryboardSceneType {
static let storyboardName = "LaunchScreen" static let storyboardName = "LaunchScreen"
} }
enum Main: StoryboardSceneType { enum Main: String, StoryboardSceneType {
static let storyboardName = "Main" static let storyboardName = "Main"
static func initialViewController() -> Ring.MainTabBarViewController { static func initialViewController() -> Ring.MainTabBarViewController {
...@@ -54,6 +54,15 @@ enum StoryboardScene { ...@@ -54,6 +54,15 @@ enum StoryboardScene {
} }
return vc return vc
} }
case mainStoryboardScene = "MainStoryboard"
static func instantiateMainStoryboard() -> Ring.MainTabBarViewController {
guard let vc = StoryboardScene.Main.mainStoryboardScene.viewController() as? Ring.MainTabBarViewController
else {
fatalError("ViewController 'MainStoryboard' is not of the expected class Ring.MainTabBarViewController.")
}
return vc
}
} }
enum WalkthroughStoryboard: StoryboardSceneType { enum WalkthroughStoryboard: StoryboardSceneType {
static let storyboardName = "WalkthroughStoryboard" static let storyboardName = "WalkthroughStoryboard"
......
...@@ -24,8 +24,6 @@ ...@@ -24,8 +24,6 @@
<true/> <true/>
<key>UILaunchStoryboardName</key> <key>UILaunchStoryboardName</key>
<string>LaunchScreen</string> <string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UIRequiredDeviceCapabilities</key> <key>UIRequiredDeviceCapabilities</key>
<array> <array>
<string>armv7</string> <string>armv7</string>
......
...@@ -31,14 +31,6 @@ class MainTabBarViewController: UITabBarController { ...@@ -31,14 +31,6 @@ class MainTabBarViewController: UITabBarController {
override func viewDidAppear(_ animated: Bool) { override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated) super.viewDidAppear(animated)
if !accountService.hasAccounts() {
self.presentWalkthrough()
}
}
fileprivate func presentWalkthrough() {
let storyboard = UIStoryboard(name: "WalkthroughStoryboard", bundle: nil)
let viewController = storyboard.instantiateInitialViewController()!
self.present(viewController, animated: false, completion: nil)
} }
} }
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11542" systemVersion="15G1108" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="01J-lp-oVM"> <document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12121" systemVersion="16F73" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
<device id="retina4_0" orientation="portrait"> <device id="retina4_0" orientation="portrait">
<adaptation id="fullscreen"/> <adaptation id="fullscreen"/>
</device> </device>
<dependencies> <dependencies>
<deployment identifier="iOS"/> <deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11524"/> <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12089"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/> <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies> </dependencies>
<scenes> <scenes>
......
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12121" systemVersion="16E195" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="qdG-Sd-QaE"> <document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12121" systemVersion="16F73" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="qdG-Sd-QaE">
<device id="retina4_0" orientation="portrait"> <device id="retina4_0" orientation="portrait">
<adaptation id="fullscreen"/> <adaptation id="fullscreen"/>
</device> </device>
...@@ -382,7 +382,7 @@ ...@@ -382,7 +382,7 @@
<!--Ring--> <!--Ring-->
<scene sceneID="oqo-zJ-m0o"> <scene sceneID="oqo-zJ-m0o">
<objects> <objects>
<tabBarController title="Ring" id="qdG-Sd-QaE" customClass="MainTabBarViewController" customModule="Ring" customModuleProvider="target" sceneMemberID="viewController"> <tabBarController storyboardIdentifier="MainStoryboard" title="Ring" id="qdG-Sd-QaE" customClass="MainTabBarViewController" customModule="Ring" customModuleProvider="target" sceneMemberID="viewController">
<tabBar key="tabBar" contentMode="scaleToFill" id="zN5-xb-CQh"> <tabBar key="tabBar" contentMode="scaleToFill" id="zN5-xb-CQh">
<rect key="frame" x="0.0" y="0.0" width="320" height="49"/> <rect key="frame" x="0.0" y="0.0" width="320" height="49"/>
<autoresizingMask key="autoresizingMask"/> <autoresizingMask key="autoresizingMask"/>
......
...@@ -131,7 +131,7 @@ class AccountsService: AccountAdapterDelegate { ...@@ -131,7 +131,7 @@ class AccountsService: AccountAdapterDelegate {
} }
func loadAccounts() { fileprivate func loadAccountsFromDaemon() {
for accountId in accountAdapter.getAccountList() { for accountId in accountAdapter.getAccountList() {
if let id = accountId as? String { if let id = accountId as? String {
self.accountList.append(AccountModel(withAccountId: id)) self.accountList.append(AccountModel(withAccountId: id))
...@@ -141,6 +141,13 @@ class AccountsService: AccountAdapterDelegate { ...@@ -141,6 +141,13 @@ class AccountsService: AccountAdapterDelegate {
reloadAccounts() reloadAccounts()
} }
func loadAccounts() -> Single<[AccountModel]> {
return Single<[AccountModel]>.just({
loadAccountsFromDaemon()
return accountList
}())
}
// MARK: - Methods // MARK: - Methods
func hasAccounts() -> Bool { func hasAccounts() -> Bool {
return !accountList.isEmpty return !accountList.isEmpty
......
...@@ -88,7 +88,10 @@ class CreateRingAccountViewController: UITableViewController { ...@@ -88,7 +88,10 @@ class CreateRingAccountViewController: UITableViewController {
case .success: case .success:
self.setCreateAccountAsIdle() self.setCreateAccountAsIdle()
self.showDeviceAddedAlert() self.showDeviceAddedAlert()
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MainStoryboard") as UIViewController
self.dismiss(animated: true, completion: nil) self.dismiss(animated: true, completion: nil)
self.present(vc, animated: true, completion: nil)
default: default:
return return
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment