Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 41 additions & 41 deletions HabitRPG/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@ class HabiticaAppDelegate: UIResponder, MessagingDelegate, UIApplicationDelegate
var window: UIWindow?
var currentAuthorizationFlow: OIDExternalUserAgentSession?
var isBeingTested = false

var application: UIApplication?

private let userRepository = UserRepository()
private let contentRepository = ContentRepository()
let taskRepository = TaskRepository()
private let socialRepository = SocialRepository()
private let configRepository = ConfigRepository.shared

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
Measurements.start(identifier: "didFinishLaunchingWithOptions")
Measurements.start(identifier: "task list loaded")
logger = RemoteLogger()
self.application = application

if !isBeingTested {
setupLogging()
setupAnalytics()
Expand All @@ -57,11 +57,11 @@ class HabiticaAppDelegate: UIResponder, MessagingDelegate, UIApplicationDelegate
setupNetworkClient()
setupDatabase()
configureNotifications()

if let userInfo = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [AnyHashable: Any] {
handlePushnotification(identifier: nil, userInfo: userInfo)
}

handleInitialLaunch()
applySearchAdAttribution()
Measurements.stop(identifier: "didFinishLaunchingWithOptions")
Expand All @@ -76,7 +76,7 @@ class HabiticaAppDelegate: UIResponder, MessagingDelegate, UIApplicationDelegate
}
return RouterHandler.shared.handle(url: url)
}

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
URLContexts.forEach { context in
RouterHandler.shared.handle(url: context.url)
Expand All @@ -101,16 +101,16 @@ class HabiticaAppDelegate: UIResponder, MessagingDelegate, UIApplicationDelegate
if let apiKey = launchEnvironment["apikey"] {
AuthenticationManager.shared.currentUserKey = apiKey
}

if let stubs = launchEnvironment["STUB_DATA"]?.data(using: .utf8) {
// swiftlint:disable:next force_try
HabiticaServerConfig.stubs = try! JSONDecoder().decode([String: CallStub].self, from: stubs)
}
}

func setupFirebase() {
Messaging.messaging().delegate = self

let userDefaults = UserDefaults.standard
#if !targetEnvironment(macCatalyst)
Crashlytics.crashlytics().setCustomValue(-(NSTimeZone.local.secondsFromGMT() / 60), forKey: "timesoze_offset")
Expand All @@ -121,23 +121,23 @@ class HabiticaAppDelegate: UIResponder, MessagingDelegate, UIApplicationDelegate
Analytics.setUserProperty(userDefaults.string(forKey: "initialScreenURL"), forName: "launch_screen")
#endif
}

func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {

}

func saveDeviceToken(_ deviceToken: Data) {
let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
UserDefaults.standard.set(token, forKey: "PushNotificationDeviceToken")
}

func setupLogging() {
let userID = AuthenticationManager.shared.currentUserId
FirebaseApp.configure()
(logger as? RemoteLogger)?.setUserID(userID)
logger.isProduction = HabiticaAppDelegate.isRunningLive()
}

func setupAnalytics() {
Amplitude.instance().initializeApiKey(Secrets.amplitudeApiKey)
Amplitude.instance().setUserId(AuthenticationManager.shared.currentUserId)
Expand All @@ -146,11 +146,11 @@ class HabiticaAppDelegate: UIResponder, MessagingDelegate, UIApplicationDelegate
"launch_screen": userDefaults.string(forKey: "initialScreenURL") ?? ""
])
}

func setupPurchaseHandling() {
PurchaseHandler.shared.completionHandler()
}

func setupNetworkClient() {
NetworkAuthenticationManager.shared.currentUserId = AuthenticationManager.shared.currentUserId
NetworkAuthenticationManager.shared.currentUserKey = AuthenticationManager.shared.currentUserKey
Expand All @@ -165,15 +165,15 @@ class HabiticaAppDelegate: UIResponder, MessagingDelegate, UIApplicationDelegate
}
let configuration = URLSessionConfiguration.default
AuthenticatedCall.defaultConfiguration.urlConfiguration = configuration

let userDefaults = UserDefaults.standard
for (key, etag) in userDefaults.dictionaryRepresentation().filter({ (key, _) -> Bool in
return key.starts(with: "etag")
}) {
HabiticaServerConfig.etags[String(key.dropFirst(4))] = etag as? String
}
}

func updateServer() {
if isBeingTested {
AuthenticatedCall.defaultConfiguration = HabiticaServerConfig.stub
Expand Down Expand Up @@ -219,7 +219,7 @@ class HabiticaAppDelegate: UIResponder, MessagingDelegate, UIApplicationDelegate
}
}
}

func setupDatabase() {
var config = Realm.Configuration.defaultConfiguration
config.deleteRealmIfMigrationNeeded = true
Expand All @@ -237,30 +237,30 @@ class HabiticaAppDelegate: UIResponder, MessagingDelegate, UIApplicationDelegate
}
Realm.Configuration.defaultConfiguration = config
}

func setupRouter() {
RouterHandler.shared.register()
}

func handleInitialLaunch() {
let defaults = UserDefaults.standard
if !defaults.bool(forKey: "wasLaunchedBefore") {
defaults.set(true, forKey: "wasLaunchedBefore")

var components = Calendar.current.dateComponents([.year, .month, .day], from: Date())
components.hour = 19
components.minute = 0
let newDate = Calendar.current.date(from: components)

defaults.set(true, forKey: "dailyReminderActive")
defaults.set(newDate, forKey: "dailyReminderTime")
defaults.set(true, forKey: "appBadgeActive")
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()

rescheduleDailyReminder()
}
}

func retrieveTasks(_ completed: @escaping ((Bool) -> Void)) {
taskRepository.retrieveTasks().observeResult { (result) in
switch result {
Expand All @@ -271,7 +271,7 @@ class HabiticaAppDelegate: UIResponder, MessagingDelegate, UIApplicationDelegate
}
}
}

func scoreTask(_ taskId: String, direction: TaskScoringDirection, completed: @escaping (() -> Void)) {
if let task = taskRepository.getEditableTask(id: taskId) {
taskRepository.score(task: task, direction: direction).observeCompleted {
Expand All @@ -281,7 +281,7 @@ class HabiticaAppDelegate: UIResponder, MessagingDelegate, UIApplicationDelegate
completed()
}
}

func acceptQuestInvitation(_ completed: @escaping ((Bool) -> Void)) {
userRepository.getUser().take(first: 1)
.map({ (user) -> String? in
Expand All @@ -296,7 +296,7 @@ class HabiticaAppDelegate: UIResponder, MessagingDelegate, UIApplicationDelegate
completed(true)
}).start()
}

func rejectQuestInvitation(_ completed: @escaping ((Bool) -> Void)) {
userRepository.getUser().take(first: 1)
.map({ (user) -> String? in
Expand All @@ -311,7 +311,7 @@ class HabiticaAppDelegate: UIResponder, MessagingDelegate, UIApplicationDelegate
completed(true)
}).start()
}

func sendPrivateMessage(toUserID: String, message: String, completed: @escaping ((Bool) -> Void)) {
socialRepository.post(inboxMessage: message, toUserID: toUserID).observeResult({ (result) in
switch result {
Expand All @@ -322,17 +322,17 @@ class HabiticaAppDelegate: UIResponder, MessagingDelegate, UIApplicationDelegate
}
})
}

func displayNotificationInApp(text: String) {
ToastManager.show(text: text, color: .purple)
UINotificationFeedbackGenerator.oneShotNotificationOccurred(.success)
}

func displayNotificationInApp(title: String, text: String) {
ToastManager.show(text: "\(title)\n\(text)", color: .purple)
UINotificationFeedbackGenerator.oneShotNotificationOccurred(.success)
}

static func isRunningLive() -> Bool {
#if targetEnvironment(simulator)
return false
Expand All @@ -346,7 +346,7 @@ class HabiticaAppDelegate: UIResponder, MessagingDelegate, UIApplicationDelegate
}
#endif
}

func applySearchAdAttribution() {
if UserDefaults.standard.bool(forKey: "userWasAttributed") {
return
Expand Down Expand Up @@ -389,7 +389,7 @@ class HabiticaAppDelegate: UIResponder, MessagingDelegate, UIApplicationDelegate
}
}
}

private func handleCampaign(_ data: [String: Any]) {
guard let campaignId = data["campaignId"] as? Int else {
return
Expand All @@ -413,21 +413,21 @@ class HabiticaAppDelegate: UIResponder, MessagingDelegate, UIApplicationDelegate
"searchAdConversionDate": data["iad-conversion-date"] ?? ""
])
}

static func isRunningScreenshots() -> Bool {
#if !targetEnvironment(simulator)
return false
#else
return UserDefaults.standard.bool(forKey: "FASTLANE_SNAPSHOT")
#endif
}

func messaging(_ messaging: MessagingDelegate, didReceiveRegistrationToken fcmToken: String) {
logger.log("Firebase registration token: \(fcmToken)")
let dataDict: [String: String] = ["token": fcmToken]
NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
}

func displayInAppNotification(taskID: String, text: String) {
let alertController = HabiticaAlertController(title: text)
alertController.addAction(title: L10n.complete, style: .default, isMainAction: true, closeOnTap: true, identifier: nil) {[weak self] _ in
Expand All @@ -451,7 +451,7 @@ extension HabiticaAppDelegate {
}
return false
}

func displayMaintenanceScreen(title: String, descriptionString: String) {
if findMaintenanceScreen() == nil {
let maintenanceController = MaintenanceViewController()
Expand All @@ -461,11 +461,11 @@ extension HabiticaAppDelegate {
UIApplication.topViewController()?.present(maintenanceController, animated: true, completion: nil)
}
}

func hideMaintenanceScreen() {
findMaintenanceScreen()?.dismiss(animated: true, completion: nil)
}

private func findMaintenanceScreen() -> MaintenanceViewController? {
var viewController: UIViewController? = UIApplication.shared.findKeyWindow()?.rootViewController
while viewController != nil {
Expand Down
Loading