Work around SwiftUI on bugs on iPadOS so that we can add accounts on the iPad

This commit is contained in:
Maurice Parker 2019-09-11 20:50:03 -05:00
parent efcbdd642a
commit 31b506b85a
5 changed files with 64 additions and 99 deletions

View File

@ -495,7 +495,6 @@ private extension MasterTimelineViewController {
var snapshot = NSDiffableDataSourceSnapshot<Int, Article>()
snapshot.appendSections([0])
snapshot.appendItems(coordinator.articles, toSection: 0)
print("********* article count: \(coordinator.articles.count)")
dataSource.apply(snapshot, animatingDifferences: animate) { [weak self] in
self?.restoreSelectionIfNecessary()

View File

@ -11,34 +11,16 @@ import Account
struct SettingsAddAccountView : View {
@Environment(\.presentationMode) var presentation
@State private var selectedAccountType: AccountType = nil
var body: some View {
Form {
Button(action: {
self.selectedAccountType = AccountType.onMyMac
}) {
NavigationLink(destination: SettingsLocalAccountView(name: "")) {
SettingsAccountLabelView(accountImage: "accountLocal", accountLabel: Account.defaultLocalAccountName)
}
Button(action: {
self.selectedAccountType = AccountType.feedbin
}) {
NavigationLink(destination: SettingsFeedbinAccountView(viewModel: SettingsFeedbinAccountView.ViewModel())) {
SettingsAccountLabelView(accountImage: "accountFeedbin", accountLabel: "Feedbin")
}
}
.sheet(item: $selectedAccountType) { accountType in
if accountType == .onMyMac {
SettingsLocalAccountView(name: "", onDismiss: { self.presentation.wrappedValue.dismiss() })
}
if accountType == .feedbin {
SettingsFeedbinAccountView(viewModel: SettingsFeedbinAccountView.ViewModel(), onDismiss: { self.presentation.wrappedValue.dismiss() })
}
}
.navigationBarTitle(Text("Add Account"), displayMode: .inline)
}
}

View File

@ -71,7 +71,7 @@ struct SettingsDetailAccountView : View {
var settingsFeedbinAccountView: SettingsFeedbinAccountView {
let feedbinViewModel = SettingsFeedbinAccountView.ViewModel(account: viewModel.account)
return SettingsFeedbinAccountView(viewModel: feedbinViewModel, onDismiss: {})
return SettingsFeedbinAccountView(viewModel: feedbinViewModel)
}
class ViewModel: ObservableObject {

View File

@ -17,49 +17,41 @@ struct SettingsFeedbinAccountView : View {
@State var busy: Bool = false
@State var error: String = ""
// This is a hack around the fact that onDismiss isn't being called by the sheet modifier.
var onDismiss: () -> Void
var body: some View {
NavigationView {
Form {
Section(header:
HStack {
Spacer()
SettingsAccountLabelView(accountImage: "accountFeedbin", accountLabel: "Feedbin").padding()
Spacer()
}
) {
TextField("Email", text: $viewModel.email).textContentType(.emailAddress)
SecureField("Password", text: $viewModel.password)
Form {
Section(header:
HStack {
Spacer()
SettingsAccountLabelView(accountImage: "accountFeedbin", accountLabel: "Feedbin").padding()
Spacer()
}
Section(footer:
HStack {
Spacer()
Text(verbatim: error).foregroundColor(.red)
Spacer()
}
) {
HStack {
Spacer()
Button(action: { self.addAccount() }) {
if viewModel.isUpdate {
Text("Update Account")
} else {
Text("Add Account")
}
) {
TextField("Email", text: $viewModel.email).textContentType(.emailAddress)
SecureField("Password", text: $viewModel.password)
}
Section(footer:
HStack {
Spacer()
Text(verbatim: error).foregroundColor(.red)
Spacer()
}
) {
HStack {
Spacer()
Button(action: { self.addAccount() }) {
if viewModel.isUpdate {
Text("Update Account")
} else {
Text("Add Account")
}
.disabled(!viewModel.isValid)
Spacer()
}
.disabled(!viewModel.isValid)
Spacer()
}
}
.disabled(busy)
.navigationBarTitle(Text(""), displayMode: .inline)
.navigationBarItems(leading:
Button(action: { self.dismiss() }) { Text("Cancel") }
)
}
// .disabled(busy) // Maybe someday we can do this, but right now it crashes on the iPad
.navigationBarTitle(Text(""), displayMode: .inline)
}
private func addAccount() {
@ -71,14 +63,14 @@ struct SettingsFeedbinAccountView : View {
let credentials = Credentials.basic(username: emailAddress, password: viewModel.password)
Account.validateCredentials(type: .feedbin, credentials: credentials) { result in
self.busy = false
switch result {
case .success(let authenticated):
if (authenticated != nil) {
var newAccount = false
let workAccount: Account
if self.viewModel.account == nil {
@ -87,39 +79,38 @@ struct SettingsFeedbinAccountView : View {
} else {
workAccount = self.viewModel.account!
}
do {
do {
try workAccount.removeCredentials()
} catch {}
try workAccount.storeCredentials(credentials)
if newAccount {
workAccount.refreshAll() { result in }
}
self.dismiss()
} catch {
self.error = "Keychain error while storing credentials."
}
} else {
self.error = "Invalid email/password combination."
}
case .failure:
self.error = "Network error. Try again later."
}
}
}
private func dismiss() {
presentation.wrappedValue.dismiss()
onDismiss()
}
class ViewModel: ObservableObject {
@ -164,7 +155,7 @@ struct SettingsFeedbinAccountView : View {
#if DEBUG
struct SettingsFeedbinAccountView_Previews : PreviewProvider {
static var previews: some View {
SettingsFeedbinAccountView(viewModel: SettingsFeedbinAccountView.ViewModel(), onDismiss: {})
SettingsFeedbinAccountView(viewModel: SettingsFeedbinAccountView.ViewModel())
}
}
#endif

View File

@ -13,36 +13,30 @@ struct SettingsLocalAccountView : View {
@Environment(\.presentationMode) var presentation
@State var name: String
// This is a hack around the fact that onDismiss isn't being called by the sheet modifier.
var onDismiss: () -> Void
var body: some View {
NavigationView {
Form {
Section(header:
HStack {
Spacer()
SettingsAccountLabelView(accountImage: "accountLocal", accountLabel: Account.defaultLocalAccountName).padding()
Spacer()
}
) {
HStack {
TextField("Name", text: $name)
}
Form {
Section(header:
HStack {
Spacer()
SettingsAccountLabelView(accountImage: "accountLocal", accountLabel: Account.defaultLocalAccountName).padding()
Spacer()
}
Section {
HStack {
Spacer()
Button(action: { self.addAccount() }) {
Text("Add Account")
}
Spacer()
}
) {
HStack {
TextField("Name", text: $name)
}
}
Section {
HStack {
Spacer()
Button(action: { self.addAccount() }) {
Text("Add Account")
}
Spacer()
}
}
.navigationBarTitle(Text(""), displayMode: .inline)
.navigationBarItems(leading: Button(action: { self.dismiss() }) { Text("Cancel") } )
}
.navigationBarTitle(Text(""), displayMode: .inline)
}
private func addAccount() {
@ -53,7 +47,6 @@ struct SettingsLocalAccountView : View {
private func dismiss() {
presentation.wrappedValue.dismiss()
onDismiss()
}
}
@ -61,7 +54,7 @@ struct SettingsLocalAccountView : View {
#if DEBUG
struct SettingsLocalAccountView_Previews : PreviewProvider {
static var previews: some View {
SettingsLocalAccountView(name: "", onDismiss: {})
SettingsLocalAccountView(name: "")
}
}
#endif