Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

split login scene up in seperate files for better overview #3

Merged
merged 1 commit into from
Apr 25, 2022
Merged
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
37 changes: 37 additions & 0 deletions Modules/Sources/Login/LoginEnvironment.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// File.swift
//
//
// Created by Nicolai Harbo on 20/04/2022.
//

import APIClient
import AppVersion
import CombineSchedulers
import Localizations

public struct LoginEnvironment {

var mainQueue: AnySchedulerOf<DispatchQueue>
var apiClient: APIClient
var date: () -> Date
var calendar: Calendar
var localizations: ObservableLocalizations
var appVersion: AppVersion

public init(
mainQueue: AnySchedulerOf<DispatchQueue>,
apiClient: APIClient,
date: @escaping () -> Date,
calendar: Calendar,
localizations: ObservableLocalizations,
appVersion: AppVersion
) {
self.mainQueue = mainQueue
self.apiClient = apiClient
self.date = date
self.calendar = calendar
self.localizations = localizations
self.appVersion = appVersion
}
}
97 changes: 1 addition & 96 deletions Modules/Sources/Login/LoginView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,107 +6,12 @@
//

import APIClient
import AppVersion
import Combine
import CombineSchedulers
import Localizations
import Model
import Style
import SwiftUI
import SwiftUINavigation

public struct LoginEnvironment {

var mainQueue: AnySchedulerOf<DispatchQueue>
var apiClient: APIClient
var date: () -> Date
var calendar: Calendar
var localizations: ObservableLocalizations
var appVersion: AppVersion

public init(
mainQueue: AnySchedulerOf<DispatchQueue>,
apiClient: APIClient,
date: @escaping () -> Date,
calendar: Calendar,
localizations: ObservableLocalizations,
appVersion: AppVersion
) {
self.mainQueue = mainQueue
self.apiClient = apiClient
self.date = date
self.calendar = calendar
self.localizations = localizations
self.appVersion = appVersion
}
}

public class LoginViewModel: ObservableObject {

var environment: LoginEnvironment
var onSuccess: (APITokens, Username) -> Void

enum Route {
case alert(AlertState)
}
@Published var route: Route?

@Published var email: String = ""
@Published var password: String = ""
@Published var appVersion: String = ""
@Published var isAPICallInFlight = false

var loginCancellable: AnyCancellable?
var cancellables: Set<AnyCancellable> = []

public init(
onSuccess: @escaping (APITokens, Username) -> Void,
environment: LoginEnvironment
) {
self.environment = environment
self.onSuccess = onSuccess
appVersion = "\(environment.appVersion.version())(\(environment.appVersion.build()))"
#if DEBUG
email = "[email protected]"
password = "1B57E5A0"
#endif
}

/// Simple heuristics for email pattern
var isButtonEnabled: Bool {
email.contains("@") && email.contains(".") && email.count >= 6
&& !password.isEmpty && !isAPICallInFlight
}

func loginButtonTapped() {
isAPICallInFlight = true
loginCancellable = environment.apiClient.authenticate(
Username(rawValue: email.trimmingCharacters(in: .whitespacesAndNewlines)),
Password(rawValue: password.trimmingCharacters(in: .whitespacesAndNewlines))
)
.receive(on: environment.mainQueue, options: nil)
.sink(
receiveCompletion: { [weak self, environment] in
if case .failure(let error) = $0 {
self?.route = .alert(
.withTitleAndMessage(
title: environment.localizations.error.errorTitle,
message: error.errorCode == "401"
? environment.localizations.login.errorInvalidCredentials
: environment.localizations.error.serverError,
action1: .primary(
title: environment.localizations.defaultSection.ok.uppercased(),
action: { self?.route = nil })
)
)
}
self?.isAPICallInFlight = false
},
receiveValue: { [unowned self] in onSuccess($0, .init(rawValue: email)) }
)
}

}
import Localizations

public struct LoginView: View {

Expand Down
83 changes: 83 additions & 0 deletions Modules/Sources/Login/LoginViewModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//
// File.swift
//
//
// Created by Nicolai Harbo on 20/04/2022.
//

import APIClient
import AppVersion
import Combine
import CombineSchedulers
import Localizations
import Model
import Style
import SwiftUI
import SwiftUINavigation

public class LoginViewModel: ObservableObject {

var environment: LoginEnvironment
var onSuccess: (APITokens, Username) -> Void

enum Route {
case alert(AlertState)
}
@Published var route: Route?

@Published var email: String = ""
@Published var password: String = ""
@Published var appVersion: String = ""
@Published var isAPICallInFlight = false

var loginCancellable: AnyCancellable?
var cancellables: Set<AnyCancellable> = []

public init(
onSuccess: @escaping (APITokens, Username) -> Void,
environment: LoginEnvironment
) {
self.environment = environment
self.onSuccess = onSuccess
appVersion = "\(environment.appVersion.version())(\(environment.appVersion.build()))"
#if DEBUG
email = "[email protected]"
password = "1B57E5A0"
#endif
}

/// Simple heuristics for email pattern
var isButtonEnabled: Bool {
email.contains("@") && email.contains(".") && email.count >= 6
&& !password.isEmpty && !isAPICallInFlight
}

func loginButtonTapped() {
isAPICallInFlight = true
loginCancellable = environment.apiClient.authenticate(
Username(rawValue: email.trimmingCharacters(in: .whitespacesAndNewlines)),
Password(rawValue: password.trimmingCharacters(in: .whitespacesAndNewlines))
)
.receive(on: environment.mainQueue, options: nil)
.sink(
receiveCompletion: { [weak self, environment] in
if case .failure(let error) = $0 {
self?.route = .alert(
.withTitleAndMessage(
title: environment.localizations.error.errorTitle,
message: error.errorCode == "401"
? environment.localizations.login.errorInvalidCredentials
: environment.localizations.error.serverError,
action1: .primary(
title: environment.localizations.defaultSection.ok.uppercased(),
action: { self?.route = nil })
)
)
}
self?.isAPICallInFlight = false
},
receiveValue: { [unowned self] in onSuccess($0, .init(rawValue: email)) }
)
}

}