Continue work on usermanagement
This commit is contained in:
parent
5c39d6a17c
commit
64e9763604
3 changed files with 114 additions and 45 deletions
|
|
@ -86,9 +86,9 @@ class Store: ObservableObject {
|
|||
try? context.save()
|
||||
}
|
||||
|
||||
func save(userBaseUrl baseUrl: String, username: String, password: String) {
|
||||
func saveUser(baseUrl: String, username: String, password: String) {
|
||||
do {
|
||||
let user = User(context: context)
|
||||
let user = getUser(baseUrl: baseUrl) ?? User(context: context)
|
||||
user.baseUrl = baseUrl
|
||||
user.username = username
|
||||
user.password = password
|
||||
|
|
@ -195,8 +195,8 @@ extension Store {
|
|||
}
|
||||
|
||||
// Users
|
||||
store.save(userBaseUrl: "https://ntfy.sh", username: "testuser", password: "testuser")
|
||||
store.save(userBaseUrl: "https://ntfy.example.com", username: "phil", password: "phil12")
|
||||
store.saveUser(baseUrl: "https://ntfy.sh", username: "testuser", password: "testuser")
|
||||
store.saveUser(baseUrl: "https://ntfy.example.com", username: "phil", password: "phil12")
|
||||
}
|
||||
return store
|
||||
}()
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ import SwiftUI
|
|||
|
||||
struct SettingsView: View {
|
||||
@EnvironmentObject private var store: Store
|
||||
@FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \User.baseUrl, ascending: true)]) var users: FetchedResults<User>
|
||||
|
||||
var body: some View {
|
||||
NavigationView {
|
||||
|
|
@ -13,27 +12,8 @@ struct SettingsView: View {
|
|||
Text("Manage users")
|
||||
}
|
||||
}*/
|
||||
Section(
|
||||
header: Text("Users")
|
||||
) {
|
||||
List {
|
||||
ForEach(users) { user in
|
||||
HStack {
|
||||
Image(systemName: "person.fill")
|
||||
VStack(alignment: .leading, spacing: 0) {
|
||||
Text(user.username ?? "?")
|
||||
|
||||
Text(user.baseUrl ?? "?")
|
||||
.font(.subheadline)
|
||||
.foregroundColor(.gray)
|
||||
}
|
||||
}
|
||||
}
|
||||
HStack {
|
||||
Image(systemName: "plus")
|
||||
Text("Add user")
|
||||
}
|
||||
}
|
||||
Section(header: Text("Users")) {
|
||||
UsersView()
|
||||
}
|
||||
Section(header: Text("About")) {
|
||||
HStack {
|
||||
|
|
@ -48,7 +28,6 @@ struct SettingsView: View {
|
|||
|
||||
}
|
||||
.navigationViewStyle(StackNavigationViewStyle())
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -56,26 +35,116 @@ struct UsersView: View {
|
|||
@EnvironmentObject private var store: Store
|
||||
@FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \User.baseUrl, ascending: true)]) var users: FetchedResults<User>
|
||||
|
||||
@State private var selectedUser: User?
|
||||
@State private var showDialog = false
|
||||
|
||||
@State private var baseUrl: String = ""
|
||||
@State private var username: String = ""
|
||||
@State private var password: String = ""
|
||||
|
||||
var body: some View {
|
||||
let _ = selectedUser?.username // Workaround for FB7823148, see https://developer.apple.com/forums/thread/652080
|
||||
List {
|
||||
ForEach(users) { user in
|
||||
Text(user.username ?? "")
|
||||
UserRowView(user: user)
|
||||
.onTapGesture {
|
||||
selectedUser = user
|
||||
baseUrl = user.baseUrl ?? "?"
|
||||
username = user.username ?? "?"
|
||||
showDialog = true
|
||||
}
|
||||
}
|
||||
.listStyle(PlainListStyle())
|
||||
.navigationTitle("Manage users")
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .navigationBarTrailing) {
|
||||
Button {
|
||||
//self.showingAddDialog = true
|
||||
} label: {
|
||||
HStack {
|
||||
Image(systemName: "plus")
|
||||
Text("Add user")
|
||||
}
|
||||
.onTapGesture {
|
||||
showDialog = true
|
||||
}
|
||||
}
|
||||
.sheet(isPresented: $showDialog) {
|
||||
NavigationView {
|
||||
Form {
|
||||
Section(footer:
|
||||
Text("You can add a user here. All topics for the given server will use this user.")
|
||||
) {
|
||||
if selectedUser == nil {
|
||||
TextField("Service URL, e.g. https://ntfy.example.com", text: $baseUrl)
|
||||
.disableAutocapitalization()
|
||||
.disableAutocorrection(true)
|
||||
}
|
||||
TextField("Username", text: $username)
|
||||
.disableAutocapitalization()
|
||||
.disableAutocorrection(true)
|
||||
TextField("Password", text: $password)
|
||||
.disableAutocapitalization()
|
||||
.disableAutocorrection(true)
|
||||
}
|
||||
}
|
||||
.navigationTitle(selectedUser == nil ? "Add user" : "Edit user")
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .navigationBarLeading) {
|
||||
Button(action: cancelAction) {
|
||||
Text("Cancel")
|
||||
}
|
||||
}
|
||||
ToolbarItem(placement: .navigationBarTrailing) {
|
||||
Button(action: saveAction) {
|
||||
Text("Save")
|
||||
}
|
||||
.disabled(!isValid())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func saveAction() {
|
||||
store.saveUser(baseUrl: baseUrl, username: username, password: password)
|
||||
resetAndHide()
|
||||
}
|
||||
|
||||
private func cancelAction() {
|
||||
resetAndHide()
|
||||
}
|
||||
|
||||
private func isValid() -> Bool {
|
||||
return true // FIXME: validate
|
||||
}
|
||||
|
||||
private func resetAndHide() {
|
||||
selectedUser = nil
|
||||
baseUrl = ""
|
||||
username = ""
|
||||
password = ""
|
||||
showDialog = false
|
||||
}
|
||||
}
|
||||
|
||||
struct UserRowView: View {
|
||||
@ObservedObject var user: User
|
||||
|
||||
var body: some View {
|
||||
HStack {
|
||||
Image(systemName: "person.fill")
|
||||
VStack(alignment: .leading, spacing: 0) {
|
||||
VStack(alignment: .leading, spacing: 0) {
|
||||
Text(user.username ?? "?")
|
||||
Text(user.baseUrl ?? "?")
|
||||
.font(.subheadline)
|
||||
.foregroundColor(.gray)
|
||||
}
|
||||
}
|
||||
Spacer()
|
||||
Image(systemName: "chevron.forward")
|
||||
.font(.system(size: 12.0))
|
||||
.foregroundColor(.gray)
|
||||
}
|
||||
.padding(.all, 4)
|
||||
}
|
||||
}
|
||||
|
||||
struct SettingsView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
let store = Store.preview // Store.previewEmpty
|
||||
|
|
|
|||
|
|
@ -127,7 +127,7 @@ struct SubscriptionAddView: View {
|
|||
ApiService.shared.checkAuth(baseUrl: selectedBaseUrl, topic: topic, user: user) { (response, error) in
|
||||
if response?.success == true {
|
||||
DispatchQueue.global(qos: .background).async {
|
||||
store.save(userBaseUrl: selectedBaseUrl, username: username, password: password)
|
||||
store.saveUser(baseUrl: selectedBaseUrl, username: username, password: password)
|
||||
subscriptionManager.subscribe(baseUrl: selectedBaseUrl, topic: sanitizedTopic)
|
||||
}
|
||||
isShowing = false
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue