Skip to content

Commit

Permalink
Finally fix the first launch flow
Browse files Browse the repository at this point in the history
  • Loading branch information
robbiet480 committed Jan 9, 2017
1 parent efc6bc2 commit 42e9214
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 138 deletions.
13 changes: 0 additions & 13 deletions HomeAssistant.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@
B64D09AF1D7D561E008EB487 /* InputSlider.swift in Sources */ = {isa = PBXBuildFile; fileRef = B64D09AE1D7D561E008EB487 /* InputSlider.swift */; };
B657A8EA1CA646EB00121384 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B657A8E91CA646EB00121384 /* AppDelegate.swift */; };
B657A8EE1CA646EB00121384 /* SettingsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B657A8ED1CA646EB00121384 /* SettingsViewController.swift */; };
B657A8F11CA646EB00121384 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B657A8EF1CA646EB00121384 /* Main.storyboard */; };
B657A8F31CA646EB00121384 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B657A8F21CA646EB00121384 /* Assets.xcassets */; };
B657A8F61CA646EB00121384 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B657A8F41CA646EB00121384 /* LaunchScreen.storyboard */; };
B657A9011CA646EB00121384 /* HomeAssistantTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B657A9001CA646EB00121384 /* HomeAssistantTests.swift */; };
Expand Down Expand Up @@ -428,7 +427,6 @@
B657A8E61CA646EB00121384 /* HomeAssistant.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = HomeAssistant.app; sourceTree = BUILT_PRODUCTS_DIR; };
B657A8E91CA646EB00121384 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
B657A8ED1CA646EB00121384 /* SettingsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsViewController.swift; sourceTree = "<group>"; };
B657A8F01CA646EB00121384 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
B657A8F21CA646EB00121384 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
B657A8F51CA646EB00121384 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
B657A8F71CA646EB00121384 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -851,7 +849,6 @@
B657A8F71CA646EB00121384 /* Info.plist */,
B657A8F41CA646EB00121384 /* LaunchScreen.storyboard */,
B69933931E232AEA0054453D /* Localizable.strings */,
B657A8EF1CA646EB00121384 /* Main.storyboard */,
);
path = Resources;
sourceTree = "<group>";
Expand Down Expand Up @@ -1195,7 +1192,6 @@
B60616881D1F117700249C11 /* US-EN-Alexa-Mail-Has-Arrived.wav in Resources */,
B60616BB1D1F117800249C11 /* US-EN-Daisy-Water-Heater-Leak.wav in Resources */,
B60616801D1F117700249C11 /* US-EN-Alexa-Daughter-Is-Arriving.wav in Resources */,
B657A8F11CA646EB00121384 /* Main.storyboard in Resources */,
B60616421D1F117700249C11 /* US-EN-Morgan-Freeman-Turning-Off-The-Hallway-Lights.wav in Resources */,
B60616571D1F117700249C11 /* US-EN-Morgan-Freeman-Wife-Is-Arriving.wav in Resources */,
B60616391D1F117700249C11 /* US-EN-Morgan-Freeman-Son-Is-Arriving.wav in Resources */,
Expand Down Expand Up @@ -1643,15 +1639,6 @@
path = .;
sourceTree = "<group>";
};
B657A8EF1CA646EB00121384 /* Main.storyboard */ = {
isa = PBXVariantGroup;
children = (
B657A8F01CA646EB00121384 /* Base */,
);
name = Main.storyboard;
path = .;
sourceTree = "<group>";
};
B657A8F41CA646EB00121384 /* LaunchScreen.storyboard */ = {
isa = PBXVariantGroup;
children = (
Expand Down
37 changes: 6 additions & 31 deletions HomeAssistant/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,39 +48,14 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}
}

initAPI()
window = UIWindow.init(frame: UIScreen.main.bounds)
window?.backgroundColor = .white

return true
}
let tabView = RootTabBarViewController()
self.window!.rootViewController = tabView
self.window!.makeKeyAndVisible()

func initAPI() {
if let baseURL = prefs.string(forKey: "baseURL") {
print("Base URL is", baseURL)
var apiPass = ""
if let pass = prefs.string(forKey: "apiPassword") {
apiPass = pass
}
firstly {
HomeAssistantAPI.sharedInstance.Setup(baseAPIUrl: baseURL, APIPassword: apiPass)
}.then {_ in
HomeAssistantAPI.sharedInstance.Connect()
}.then { _ -> Void in
if HomeAssistantAPI.sharedInstance.notificationsEnabled {
UIApplication.shared.registerForRemoteNotifications()
}
print("Connected!")
return
}.catch {err -> Void in
print("ERROR", err)
let settingsView = SettingsViewController()
settingsView.title = "Settings"
settingsView.showErrorConnectingMessage = true
settingsView.showErrorConnectingMessageError = err
let navController = UINavigationController(rootViewController: settingsView)
self.window?.makeKeyAndVisible()
self.window?.rootViewController!.present(navController, animated: true, completion: nil)
}
}
return true
}

func applicationWillResignActive(_ application: UIApplication) {}
Expand Down
4 changes: 4 additions & 0 deletions HomeAssistant/HAAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public class HomeAssistantAPI {

let Location = LocationManager()

var Configured: Bool {
return self.baseAPIURL != "" && self.apiPassword != ""
}

func Setup(baseAPIUrl: String, APIPassword: String) -> Promise<StatusResponse> {
self.baseAPIURL = baseAPIUrl+"/api/"
self.apiPassword = APIPassword
Expand Down
44 changes: 0 additions & 44 deletions HomeAssistant/Resources/Base.lproj/Main.storyboard

This file was deleted.

2 changes: 0 additions & 2 deletions HomeAssistant/Resources/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@
<true/>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
Expand Down
75 changes: 40 additions & 35 deletions HomeAssistant/Views/RootTabBarViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import UIKit
import MBProgressHUD
import Whisper
import ObjectMapper
import PermissionScope
import PromiseKit

class RootTabBarViewController: UITabBarController, UITabBarControllerDelegate {

let prefs = UserDefaults(suiteName: "group.io.robbie.homeassistant")!

override func viewDidLoad() {
super.viewDidLoad()

Expand All @@ -24,33 +26,53 @@ class RootTabBarViewController: UITabBarController, UITabBarControllerDelegate {
object: nil)
}

// swiftlint:disable:next function_body_length
override func viewWillAppear(_ animated: Bool) {
}

override func viewDidAppear(_ animated: Bool) {
let hud = MBProgressHUD.showAdded(to: self.view, animated: true)
if let baseURL = prefs.string(forKey: "baseURL"), let apiPass = prefs.string(forKey: "apiPassword") {
firstly {
HomeAssistantAPI.sharedInstance.Setup(baseAPIUrl: baseURL, APIPassword: apiPass)
}.then {_ in
HomeAssistantAPI.sharedInstance.Connect()
}.then { _ -> Void in
if HomeAssistantAPI.sharedInstance.notificationsEnabled {
UIApplication.shared.registerForRemoteNotifications()
}
print("Connected!")
hud.hide(animated: true)
self.loadTabs()
return
}.catch {err -> Void in
print("ERROR on connect!!!", err)
hud.hide(animated: true)
let settingsView = SettingsViewController()
settingsView.showErrorConnectingMessage = true
settingsView.showErrorConnectingMessageError = err
settingsView.doneButton = true
let navController = UINavigationController(rootViewController: settingsView)
self.present(navController, animated: true, completion: nil)
}
} else {
let settingsView = SettingsViewController()
settingsView.doneButton = true
let navController = UINavigationController(rootViewController: settingsView)
self.present(navController, animated: true, completion: {
hud.hide(animated: true)
})
}
}

// swiftlint:disable:next function_body_length
func loadTabs() {

self.delegate = self

let tabBarIconColor = colorWithHexString("#44739E", alpha: 1)

var tabViewControllers: [UIViewController] = []

let firstGroupView = GroupViewController()
firstGroupView.title = "Loading..."

self.viewControllers = [firstGroupView]

if HomeAssistantAPI.sharedInstance.baseAPIURL == "" {
DispatchQueue.main.async(execute: {
let settingsView = SettingsViewController()
settingsView.title = "Settings"
settingsView.hidesBottomBarWhenPushed = true
let navController = UINavigationController(rootViewController: settingsView)
navController.hidesBottomBarWhenPushed = true
self.present(navController, animated: true, completion: nil)
})
}

let allGroups = realm.objects(Group.self).filter {
var shouldReturn = true
// if prefs.bool(forKey: "allowAllGroups") == false {
Expand Down Expand Up @@ -135,19 +157,13 @@ class RootTabBarViewController: UITabBarController, UITabBarControllerDelegate {
let settingsIcon = getIconForIdentifier("mdi:settings", iconWidth: 30, iconHeight: 30, color: tabBarIconColor)

let settingsView = SettingsViewController()
settingsView.title = "Settings"
settingsView.tabBarItem = UITabBarItem(title: "Settings", image: settingsIcon, tag: 1)
settingsView.hidesBottomBarWhenPushed = true

tabViewControllers.append(UINavigationController(rootViewController: settingsView))

self.viewControllers = tabViewControllers

tabViewControllers.removeLast()

self.customizableViewControllers = tabViewControllers

hud.hide(animated: true)
}

func tabBarController(_ tabBarController: UITabBarController,
Expand Down Expand Up @@ -225,15 +241,4 @@ class RootTabBarViewController: UITabBarController, UITabBarControllerDelegate {
self.present(alert, animated: true, completion: nil)
}
}

/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/

}
37 changes: 24 additions & 13 deletions HomeAssistant/Views/SettingsViewController.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// SecondViewController.swift
// SettingsViewController.swift
// HomeAssistant
//
// Created by Robbie Trencheny on 3/25/16.
Expand All @@ -20,6 +20,8 @@ class SettingsViewController: FormViewController {

let prefs = UserDefaults(suiteName: "group.io.robbie.homeassistant")!

var doneButton: Bool = false

var showErrorConnectingMessage = false
var showErrorConnectingMessageError: Error? = nil

Expand All @@ -36,17 +38,23 @@ class SettingsViewController: FormViewController {
self.discovery.stopPublish()
}

override func viewWillAppear(_ animated: Bool) {
self.title = "Settings"
}

// swiftlint:disable:next cyclomatic_complexity function_body_length
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

let aboutButton = UIBarButtonItem(title: "About",
style: .plain,
target: self,
action: #selector(SettingsViewController.openAbout(_:)))
if doneButton == false {
let aboutButton = UIBarButtonItem(title: "About",
style: .plain,
target: self,
action: #selector(SettingsViewController.openAbout(_:)))

self.navigationItem.setRightBarButton(aboutButton, animated: true)
self.navigationItem.setRightBarButton(aboutButton, animated: true)
}

if let baseURL = prefs.string(forKey: "baseURL") {
self.baseURL = URL(string: baseURL)!
Expand All @@ -58,7 +66,7 @@ class SettingsViewController: FormViewController {

self.configured = (self.baseURL != nil && self.password != nil)

checkForEmail()
// checkForEmail()

if showErrorConnectingMessage {
let errDesc = (showErrorConnectingMessageError?.localizedDescription)!
Expand Down Expand Up @@ -224,12 +232,11 @@ class SettingsViewController: FormViewController {
let resetSection: Section = self.form.sectionBy(tag: "reset")!
resetSection.hidden = false
resetSection.evaluateHidden()
let alert = UIAlertController(title: "Connected",
message: "Please force quit and re-open the app to continue.",
preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK",
style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
let closeSelector = #selector(SettingsViewController.closeSettings(_:))
let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self,
action: closeSelector)

self.navigationItem.setRightBarButton(doneButton, animated: true)
}.catch { error in
print("Connection error!", error)
var errorMessage = error.localizedDescription
Expand Down Expand Up @@ -560,4 +567,8 @@ class SettingsViewController: FormViewController {
self.show(navController, sender: nil)
// self.present(navController, animated: true, completion: nil)
}

func closeSettings(_ sender: UIButton) {
self.dismiss(animated: true, completion: nil)
}
}

0 comments on commit 42e9214

Please sign in to comment.