AppDelegate.swift 2.91 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
Guillaume Roguez's avatar
Guillaume Roguez committed
24 25 26 27 28

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
29 30
    static let daemonService = DaemonService(dRingAdaptor: DRingAdapter())
    static let accountService = AccountsService(withAccountAdapter: AccountAdapter())
31
    static let nameService = NameService(withNameRegistrationAdapter: NameRegistrationAdapter())
32
    static let conversationsService = ConversationsService(withMessageAdapter: MessagesAdapter())
Guillaume Roguez's avatar
Guillaume Roguez committed
33

34
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Romain Bertozzi's avatar
Romain Bertozzi committed
35
        SystemAdapter().registerConfigurationHandler()
36
        self.startDaemon()
Guillaume Roguez's avatar
Guillaume Roguez committed
37 38
        return true
    }
Edric Milaret's avatar
Edric Milaret committed
39

40
    func applicationWillResignActive(_ application: UIApplication) {
Guillaume Roguez's avatar
Guillaume Roguez committed
41 42
    }

43
    func applicationDidEnterBackground(_ application: UIApplication) {
Guillaume Roguez's avatar
Guillaume Roguez committed
44 45
    }

46
    func applicationWillEnterForeground(_ application: UIApplication) {
Guillaume Roguez's avatar
Guillaume Roguez committed
47 48
    }

49
    func applicationDidBecomeActive(_ application: UIApplication) {
Guillaume Roguez's avatar
Guillaume Roguez committed
50 51
    }

52
    func applicationWillTerminate(_ application: UIApplication) {
53
        self.stopDaemon()
Guillaume Roguez's avatar
Guillaume Roguez committed
54 55
    }

56 57
    // MARK: - Ring Daemon
    fileprivate func startDaemon() {
58

59
        do {
60
            try AppDelegate.daemonService.startDaemon()
61
            AppDelegate.accountService.loadAccounts()
62
        } catch StartDaemonError.initializationFailure {
63
            print("Daemon failed to initialize.")
64
        } catch StartDaemonError.startFailure {
65
            print("Daemon failed to start.")
66
        } catch StartDaemonError.daemonAlreadyRunning {
67 68 69 70 71 72 73 74
            print("Daemon already running.")
        } catch {
            print("Unknown error in Daemon start.")
        }
    }

    fileprivate func stopDaemon() {
        do {
75
            try AppDelegate.daemonService.stopDaemon()
76
        } catch StopDaemonError.daemonNotRunning {
77 78 79 80 81
            print("Daemon failed to stop because it was not already running.")
        } catch {
            print("Unknown error in Daemon stop.")
        }
    }
Guillaume Roguez's avatar
Guillaume Roguez committed
82
}