Port Feedbin credential update too SwfitUI.

This commit is contained in:
Maurice Parker 2019-06-15 16:03:41 -05:00
parent 83be0e5d97
commit 8dcf4966fb
2 changed files with 57 additions and 22 deletions

View File

@ -9,10 +9,12 @@
import SwiftUI
import Combine
import Account
import RSWeb
struct SettingsDetailAccountView : View {
@ObjectBinding var viewModel: ViewModel
@State private var verifyDelete = false
@State private var showFeedbinCredentials = false
var body: some View {
List {
@ -26,17 +28,21 @@ struct SettingsDetailAccountView : View {
Text("Active")
}
}
if viewModel.isCreditialsAvailable {
Section {
HStack {
Spacer()
Button(action: {
self.showFeedbinCredentials = true
}) {
Text("Credentials")
}
.presentation(showFeedbinCredentials ? feedbinCredentialsModal : nil)
.onDisappear() { self.showFeedbinCredentials = false }
Spacer()
}
}
}
if viewModel.isDeletable {
Section {
HStack {
@ -62,6 +68,11 @@ struct SettingsDetailAccountView : View {
}
var feedbinCredentialsModal: Modal {
let feedbinViewModel = SettingsFeedbinAccountView.ViewModel(account: viewModel.account)
return Modal(SettingsFeedbinAccountView(viewModel: feedbinViewModel))
}
class ViewModel: BindableObject {
let didChange = PassthroughSubject<ViewModel, Never>()
let account: Account
@ -94,6 +105,10 @@ struct SettingsDetailAccountView : View {
}
}
var isCreditialsAvailable: Bool {
return account.type != .onMyMac
}
var isDeletable: Bool {
return AccountManager.shared.defaultAccount != account
}

View File

@ -16,7 +16,6 @@ struct SettingsFeedbinAccountView : View {
@ObjectBinding var viewModel: ViewModel
@State var busy: Bool = false
@State var error: Text = Text("")
var account: Account? = nil
var body: some View {
NavigationView {
@ -25,15 +24,15 @@ struct SettingsFeedbinAccountView : View {
SettingsAccountLabelView(accountImage: "accountFeedbin", accountLabel: "Feedbin").padding()
) {
HStack {
Spacer()
TextField($viewModel.email, placeholder: Text("Email"))
Text("Email:")
Divider()
TextField($viewModel.email)
.textContentType(.username)
Spacer()
}
HStack {
Spacer()
SecureField($viewModel.password, placeholder: Text("Password"))
Spacer()
Text("Password:")
Divider()
SecureField($viewModel.password)
}
}
Section(footer:
@ -46,8 +45,12 @@ struct SettingsFeedbinAccountView : View {
HStack {
Spacer()
Button(action: { self.addAccount() }) {
if viewModel.isUpdate {
Text("Update Account")
} else {
Text("Add Account")
}
}
.disabled(!viewModel.isValid)
Spacer()
}
@ -65,6 +68,7 @@ struct SettingsFeedbinAccountView : View {
private func addAccount() {
busy = true
error = Text("")
let emailAddress = viewModel.email.trimmingCharacters(in: .whitespaces)
let credentials = Credentials.basic(username: emailAddress, password: viewModel.password)
@ -80,11 +84,11 @@ struct SettingsFeedbinAccountView : View {
var newAccount = false
let workAccount: Account
if self.account == nil {
if self.viewModel.account == nil {
workAccount = AccountManager.shared.createAccount(type: .feedbin)
newAccount = true
} else {
workAccount = self.account!
workAccount = self.viewModel.account!
}
do {
@ -122,6 +126,18 @@ struct SettingsFeedbinAccountView : View {
class ViewModel: BindableObject {
let didChange = PassthroughSubject<ViewModel, Never>()
var account: Account? = nil
init() {
}
init(account: Account) {
self.account = account
if case .basic(let username, let password) = try? account.retrieveBasicCredentials() {
self.email = username
self.password = password
}
}
var email: String = "" {
didSet {
@ -134,6 +150,10 @@ struct SettingsFeedbinAccountView : View {
}
}
var isUpdate: Bool {
return account != nil
}
var isValid: Bool {
return !email.isEmpty && !password.isEmpty
}