AppDelegate.swift 4.57 KB
Newer Older
Guillaume Roguez's avatar
Guillaume Roguez committed
1 2 3 4
/*
 *  Copyright (C) 2016 Savoir-faire Linux Inc.
 *
 *  Author: Edric Ladent-Milaret <edric.ladent-milaret@savoirfairelinux.com>
5
 *  Author: Romain Bertozzi <romain.bertozzi@savoirfairelinux.com>
Guillaume Roguez's avatar
Guillaume Roguez committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */

import UIKit
23
import RealmSwift
24
import SwiftyBeaver
Hadrien De Sousa's avatar
Hadrien De Sousa committed
25
import RxSwift
Thibault Wittemberg's avatar
Thibault Wittemberg committed
26
import Chameleon
Guillaume Roguez's avatar
Guillaume Roguez committed
27 28 29 30 31

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
32 33
    static let daemonService = DaemonService(dRingAdaptor: DRingAdapter())
    static let accountService = AccountsService(withAccountAdapter: AccountAdapter())
34
    static let nameService = NameService(withNameRegistrationAdapter: NameRegistrationAdapter())
35
    static let conversationsService = ConversationsService(withMessageAdapter: MessagesAdapter())
36
    private let log = SwiftyBeaver.self
Guillaume Roguez's avatar
Guillaume Roguez committed
37

Hadrien De Sousa's avatar
Hadrien De Sousa committed
38 39
    fileprivate let disposeBag = DisposeBag()

40
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
41

Hadrien De Sousa's avatar
Hadrien De Sousa committed
42 43
        self.window = UIWindow(frame: UIScreen.main.bounds)

44 45 46 47 48
        // initialize log format
        let console = ConsoleDestination()
        console.format = "$Dyyyy-MM-dd HH:mm:ss.SSS$d $C$L$c: $M"
        log.addDestination(console)

Romain Bertozzi's avatar
Romain Bertozzi committed
49
        SystemAdapter().registerConfigurationHandler()
50
        self.startDaemon()
Thibault Wittemberg's avatar
Thibault Wittemberg committed
51 52 53 54

        Chameleon.setGlobalThemeUsingPrimaryColor(UIColor.ringMain, withSecondaryColor: UIColor.ringSecondary, andContentStyle: .light)
        Chameleon.setRingThemeUsingPrimaryColor(UIColor.ringMain, withSecondaryColor: UIColor.ringSecondary, andContentStyle: .light)

Hadrien De Sousa's avatar
Hadrien De Sousa committed
55
        self.loadAccounts()
Guillaume Roguez's avatar
Guillaume Roguez committed
56 57
        return true
    }
Edric Milaret's avatar
Edric Milaret committed
58

59
    func applicationWillResignActive(_ application: UIApplication) {
Guillaume Roguez's avatar
Guillaume Roguez committed
60 61
    }

62
    func applicationDidEnterBackground(_ application: UIApplication) {
Guillaume Roguez's avatar
Guillaume Roguez committed
63 64
    }

65
    func applicationWillEnterForeground(_ application: UIApplication) {
Guillaume Roguez's avatar
Guillaume Roguez committed
66 67
    }

68
    func applicationDidBecomeActive(_ application: UIApplication) {
Guillaume Roguez's avatar
Guillaume Roguez committed
69 70
    }

71
    func applicationWillTerminate(_ application: UIApplication) {
72
        self.stopDaemon()
Guillaume Roguez's avatar
Guillaume Roguez committed
73 74
    }

75 76
    // MARK: - Ring Daemon
    fileprivate func startDaemon() {
77

78
        do {
79
            try AppDelegate.daemonService.startDaemon()
Hadrien De Sousa's avatar
Hadrien De Sousa committed
80

81
        } catch StartDaemonError.initializationFailure {
82
            log.error("Daemon failed to initialize.")
83
        } catch StartDaemonError.startFailure {
84
            log.error("Daemon failed to start.")
85
        } catch StartDaemonError.daemonAlreadyRunning {
86
            log.error("Daemon already running.")
87
        } catch {
88
            log.error("Unknown error in Daemon start.")
89 90 91 92 93
        }
    }

    fileprivate func stopDaemon() {
        do {
94
            try AppDelegate.daemonService.stopDaemon()
95
        } catch StopDaemonError.daemonNotRunning {
96
            log.error("Daemon failed to stop because it was not already running.")
97
        } catch {
98
            log.error("Unknown error in Daemon stop.")
99 100
        }
    }
Hadrien De Sousa's avatar
Hadrien De Sousa committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

    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()
    }
Guillaume Roguez's avatar
Guillaume Roguez committed
130
}