AppDelegate.swift 5.73 KB
Newer Older
Guillaume Roguez's avatar
Guillaume Roguez committed
1
/*
2
 *  Copyright (C) 2017 Savoir-faire Linux Inc.
Guillaume Roguez's avatar
Guillaume Roguez committed
3 4
 *
 *  Author: Edric Ladent-Milaret <edric.ladent-milaret@savoirfairelinux.com>
5
 *  Author: Romain Bertozzi <romain.bertozzi@savoirfairelinux.com>
6
 *  Author: Thibault Wittemberg <thibault.wittemberg@savoirfairelinux.com>
Guillaume Roguez's avatar
Guillaume Roguez committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 *  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
24
import RealmSwift
25
import SwiftyBeaver
Hadrien De Sousa's avatar
Hadrien De Sousa committed
26
import RxSwift
Thibault Wittemberg's avatar
Thibault Wittemberg committed
27
import Chameleon
28
import Contacts
Guillaume Roguez's avatar
Guillaume Roguez committed
29 30 31 32 33

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
34 35 36 37 38
    private let daemonService = DaemonService(dRingAdaptor: DRingAdapter())
    private let accountService = AccountsService(withAccountAdapter: AccountAdapter())
    private let nameService = NameService(withNameRegistrationAdapter: NameRegistrationAdapter())
    private let conversationsService = ConversationsService(withMessageAdapter: MessagesAdapter())
    private let contactsService = ContactsService(withContactsAdapter: ContactsAdapter())
39
    private let presenceService = PresenceService(withPresenceAdapter: PresenceAdapter())
40
    private let networkService = NetworkService()
41 42 43 44 45 46

    public lazy var injectionBag: InjectionBag = {
        return InjectionBag(withDaemonService: self.daemonService,
                            withAccountService: self.accountService,
                            withNameService: self.nameService,
                            withConversationService: self.conversationsService,
47
                            withContactsService: self.contactsService,
48 49 50
                            withPresenceService: self.presenceService,
                            withNetworkService: self.networkService
                            )
51 52 53 54
    }()
    private lazy var appCoordinator: AppCoordinator = {
        return AppCoordinator(with: self.injectionBag)
    }()
55

56
    private let log = SwiftyBeaver.self
Guillaume Roguez's avatar
Guillaume Roguez committed
57

Hadrien De Sousa's avatar
Hadrien De Sousa committed
58 59
    fileprivate let disposeBag = DisposeBag()

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

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

64 65
        UserDefaults.standard.setValue(false, forKey: "_UIConstraintBasedLayoutLogUnsatisfiable")

66 67 68 69 70
        // initialize log format
        let console = ConsoleDestination()
        console.format = "$Dyyyy-MM-dd HH:mm:ss.SSS$d $C$L$c: $M"
        log.addDestination(console)

71
        // starts the daemon
Romain Bertozzi's avatar
Romain Bertozzi committed
72
        SystemAdapter().registerConfigurationHandler()
73
        self.startDaemon()
Thibault Wittemberg's avatar
Thibault Wittemberg committed
74

75 76
        self.networkService.monitorNetworkType()

77
        // themetize the app
Thibault Wittemberg's avatar
Thibault Wittemberg committed
78 79 80
        Chameleon.setGlobalThemeUsingPrimaryColor(UIColor.ringMain, withSecondaryColor: UIColor.ringSecondary, andContentStyle: .light)
        Chameleon.setRingThemeUsingPrimaryColor(UIColor.ringMain, withSecondaryColor: UIColor.ringSecondary, andContentStyle: .light)

81 82 83 84 85 86
        // load accounts during splashscreen
        // and ask the AppCoordinator to handle the first screen once loading is finished
        self.accountService.loadAccounts().subscribe { [unowned self] (_) in
            if let currentAccount = self.accountService.currentAccount {
                self.contactsService.loadContacts(withAccount: currentAccount)
                self.contactsService.loadContactRequests(withAccount: currentAccount)
87
                self.presenceService.subscribeBuddies(withAccount: currentAccount, withContacts: self.contactsService.contacts.value)
88 89
            }
        }.disposed(by: self.disposeBag)
90

91 92 93
        self.window?.rootViewController = self.appCoordinator.rootViewController
        self.window?.makeKeyAndVisible()
        self.appCoordinator.start()
94
        self.startDB()
Guillaume Roguez's avatar
Guillaume Roguez committed
95 96
        return true
    }
Edric Milaret's avatar
Edric Milaret committed
97

98
    func applicationWillTerminate(_ application: UIApplication) {
99
        self.stopDaemon()
Guillaume Roguez's avatar
Guillaume Roguez committed
100 101
    }

102 103
    // MARK: - Ring Daemon
    fileprivate func startDaemon() {
104

105
        do {
106
            try self.daemonService.startDaemon()
107
        } catch StartDaemonError.initializationFailure {
108
            log.error("Daemon failed to initialize.")
109
        } catch StartDaemonError.startFailure {
110
            log.error("Daemon failed to start.")
111
        } catch StartDaemonError.daemonAlreadyRunning {
112
            log.error("Daemon already running.")
113
        } catch {
114
            log.error("Unknown error in Daemon start.")
115 116 117 118 119
        }
    }

    fileprivate func stopDaemon() {
        do {
120
            try self.daemonService.stopDaemon()
121
        } catch StopDaemonError.daemonNotRunning {
122
            log.error("Daemon failed to stop because it was not already running.")
123
        } catch {
124
            log.error("Unknown error in Daemon stop.")
125 126
        }
    }
127 128 129 130 131 132 133 134 135 136 137 138 139 140

    private func startDB() {
        do {
            let dbManager = DBBridging(profileHepler: ProfileDataHelper(),
                                       conversationHelper: ConversationDataHelper(),
                                       interactionHepler: InteractionDataHelper())
            try dbManager.start()
        } catch {
            let time = DispatchTime.now() + 1
            DispatchQueue.main.asyncAfter(deadline: time) {
                self.appCoordinator.showDatabaseError()
            }
        }
    }
Guillaume Roguez's avatar
Guillaume Roguez committed
141
}