Skip to content

Commit

Permalink
Merge pull request #26 from reloni/develop
Browse files Browse the repository at this point in the history
Refactoring and bug fixes
  • Loading branch information
reloni authored Jul 2, 2017
2 parents 045adef + 696d81a commit 9aec155
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 76 deletions.
3 changes: 1 addition & 2 deletions Aika/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?

var hasAuthenticationData: Bool {
guard Keychain.userEmail.characters.count > 0,
Keychain.userPassword.characters.count > 0,
guard Keychain.authenticationType != nil,
Keychain.token.characters.count > 0,
Keychain.refreshToken.characters.count > 0,
Keychain.userUuid.characters.count > 0 else {
Expand Down
34 changes: 32 additions & 2 deletions Aika/Common/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,42 @@ extension IconBadgeStyle {
extension Keychain {
private static let keychain = Keychain()

static var userEmail: String {
static var authenticationType: AuthenticationType? {
get {
guard let type = keychain.stringForAccount(account: "authenticationType") else { return nil }

switch type {
case "db": return AuthenticationType.db(email: userEmail, password: userPassword)
case "facebook": return AuthenticationType.facebook
case "google": return AuthenticationType.google
default:return nil
}
}
set {
guard let type = newValue else {
userEmail = ""
userPassword = ""
keychain.setString(string: "", forAccount: "authenticationType")
return
}

switch type {
case .facebook: keychain.setString(string: "facebook", forAccount: "authenticationType")
case .google: keychain.setString(string: "google", forAccount: "authenticationType")
case let .db(email, password):
keychain.setString(string: "db", forAccount: "authenticationType")
userEmail = email
userPassword = password
}
}
}

private static var userEmail: String {
get { return keychain.stringForAccount(account: "userEmail") ?? "" }
set { keychain.setString(string: newValue, forAccount: "userEmail") }
}

static var userPassword: String {
private static var userPassword: String {
get { return keychain.stringForAccount(account: "userPassword") ?? "" }
set { keychain.setString(string: newValue, forAccount: "userPassword") }
}
Expand Down
44 changes: 21 additions & 23 deletions Aika/Controllers/Tasks/EditTaskController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import SnapKit
import Material
import RxSwift
import RxDataFlow
import RxGesture

final class EditTaskController : UIViewController {
enum DatePickerExpandMode {
Expand Down Expand Up @@ -73,6 +74,7 @@ final class EditTaskController : UIViewController {
picker.borderColor = Theme.Colors.romanSilver
picker.borderWidth = 0.5
picker.layoutMargins = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
picker.date = nil

return picker
}()
Expand Down Expand Up @@ -119,13 +121,11 @@ final class EditTaskController : UIViewController {
return text
}()

let saveSubject = PublishSubject<Void>()

init(viewModel: EditTaskViewModel) {
self.viewModel = viewModel

descriptionTextField.text = viewModel.taskDescription.value
notesTextField.text = viewModel.taskNotes.value
targetDatePickerView.date = viewModel.taskTargetDate.value

super.init(nibName: nil, bundle: nil)
}

Expand Down Expand Up @@ -153,10 +153,6 @@ final class EditTaskController : UIViewController {
updateViewConstraints()

bind()

if viewModel.task == nil {
descriptionTextField.becomeFirstResponder()
}
}

func bind() {
Expand All @@ -170,22 +166,24 @@ final class EditTaskController : UIViewController {
self?.scrollView.updatecontentInsetFor(keyboardHeight: 0)
}).disposed(by: bag)

descriptionTextField.rx.didChange.subscribe(onNext: { [weak self] _ in
self?.viewModel.taskDescription.value = self?.descriptionTextField.text ?? ""
}).disposed(by: bag)

notesTextField.rx.didChange.subscribe(onNext: { [weak self] _ in
self?.viewModel.taskNotes.value = self?.notesTextField.text
}).disposed(by: bag)

targetDatePickerView.currentDate.bind(to: viewModel.taskTargetDate).disposed(by: bag)
let state = viewModel.state.shareReplay(1)

viewModel.datePickerExpanded.skip(1).subscribe(onNext: { [weak self] isExpanded in self?.switchDatePickerExpandMode(isExpanded) }).disposed(by: bag)
let datePickerExpanded = targetDateView.calendarButton.rx.tap
.withLatestFrom(state.map { !$0.datePickerExpanded })

viewModel.taskTargetDateChanged.subscribe(onNext: { [weak self] next in self?.targetDatePickerView.date = next }).disposed(by: bag)
viewModel.subscribe(taskDescription: descriptionTextField.rx.didChange.map { [weak self] _ in return self?.descriptionTextField.text ?? "" }.distinctUntilChanged(),
taskNotes: notesTextField.rx.didChange.map { [weak self] _ in return self?.notesTextField.text }.distinctUntilChanged { $0.0 == $0.1 },
taskTargetDate: targetDatePickerView.currentDate.skip(1).distinctUntilChanged { $0.0 == $0.1 },
datePickerExpanded: datePickerExpanded,
clearTargetDate: targetDateView.clearButton.rx.tap.flatMap { Observable<Void>.just() },
saveChanges: saveSubject.asObservable())
.forEach { bag.insert($0) }

targetDateView.calendarButton.rx.tap.subscribe(onNext: { [weak self] _ in self?.viewModel.switchDatePickerExpansion() }).disposed(by: bag)
targetDateView.clearButton.rx.tap.subscribe(onNext: { [weak self] _ in self?.viewModel.clearTargetDate() }).disposed(by: bag)
state.take(1).map { $0.description }.bind(to: descriptionTextField.rx.text).disposed(by: bag)
state.take(1).map { $0.notes }.bind(to: notesTextField.rx.text).disposed(by: bag)
state.map { $0.targetDate }.distinctUntilChanged({ $0.0 == $0.1 }).do(onNext: { [weak self] in self?.targetDatePickerView.date = $0 }).subscribe().disposed(by: bag)
state.map { $0.datePickerExpanded }.distinctUntilChanged().do(onNext: { [weak self] in self?.switchDatePickerExpandMode($0) }).subscribe().disposed(by: bag)
state.take(1).map { $0.currentTask == nil }.filter { $0 }.do(onNext: { [weak self] _ in self?.descriptionTextField.becomeFirstResponder() }).subscribe().disposed(by: bag)

targetDatePickerView.currentDate.map { $0?.toString(withSpelling: false) ?? "" }.bind(to: targetDateView.textField.rx.text).disposed(by: bag)

Expand All @@ -195,7 +193,7 @@ final class EditTaskController : UIViewController {

func targetDateTextFieldTapped(_ gesture: UITapGestureRecognizer) {
guard gesture.state == .ended else { return }
viewModel.switchDatePickerExpansion()
targetDateView.calendarButton.sendActions(for: .touchUpInside)
}

func switchDatePickerExpandMode(_ expand: Bool) {
Expand All @@ -220,7 +218,7 @@ final class EditTaskController : UIViewController {
}

func done() {
viewModel.save()
saveSubject.onNext()
}

override func updateViewConstraints() {
Expand Down
11 changes: 6 additions & 5 deletions Aika/DataFlow/Reducers/AuthenticationReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ extension AuthenticationReducer {
}

func signOut() -> Observable<RxStateMutator<AppState>> {
Keychain.userPassword = ""
if case let AuthenticationType.db(email, _)? = Keychain.authenticationType {
Keychain.authenticationType = AuthenticationType.db(email: email, password: "")
} else {
Keychain.authenticationType = nil
}
Keychain.token = ""
Keychain.refreshToken = ""
Keychain.userUuid = ""
Expand All @@ -43,10 +47,7 @@ extension AuthenticationReducer {
func logIn(currentState state: AppState, authType: AuthenticationType) -> Observable<RxStateMutator<AppState>> {
return state.authenticationService.logIn(authType: authType)
.flatMapLatest { result -> Observable<RxStateMutator<AppState>> in
if case .db(let data) = authType {
Keychain.userEmail = data.email
Keychain.userPassword = data.password
}
Keychain.authenticationType = authType
Keychain.token = result.token
Keychain.refreshToken = result.refreshToken
Keychain.userUuid = result.uid
Expand Down
3 changes: 1 addition & 2 deletions Aika/DataFlow/Reducers/SystemReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ struct SystemReducer : RxReducerType {
}

func clearKeychain() {
Keychain.userEmail = ""
Keychain.userPassword = ""
Keychain.authenticationType = nil
Keychain.token = ""
Keychain.refreshToken = ""
Keychain.userUuid = ""
Expand Down
9 changes: 7 additions & 2 deletions Aika/ViewModels/AuthenticationViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,19 @@ final class AuthenticationViewModel: ViewModelType {
}
}

var dbAuthentication: (email: String, password: String)? {
guard case let AuthenticationType.db(email, password)? = Keychain.authenticationType else { return nil }
return (email: email, password: password)
}

var email: String {
guard mode == .logIn else { return "" }
return Keychain.userEmail
return dbAuthentication?.email ?? ""
}

var password: String {
guard mode == .logIn else { return "" }
return Keychain.userPassword
return dbAuthentication?.password ?? ""
}

func toggleShowPasswordOrRegistrationEnter() {
Expand Down
Loading

0 comments on commit 9aec155

Please sign in to comment.