Basic Edit View and components
This commit is contained in:
@@ -8,72 +8,98 @@
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
|
||||
fileprivate enum SettingsAlert {
|
||||
case LOG_OUT,
|
||||
DELETE_CACHE,
|
||||
NONE
|
||||
|
||||
func getTitle() -> String {
|
||||
switch self {
|
||||
case .LOG_OUT: return "Log out"
|
||||
case .DELETE_CACHE: return "Delete local data"
|
||||
default: return "Please confirm your action."
|
||||
}
|
||||
}
|
||||
|
||||
func getMessage() -> String {
|
||||
switch self {
|
||||
case .LOG_OUT: return "Are you sure that you want to log out of your account?"
|
||||
case .DELETE_CACHE: return "Are you sure that you want to delete the downloaded recipes? This action will not affect any recipes stored on your server."
|
||||
default: return ""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct SettingsView: View {
|
||||
@ObservedObject var userSettings: UserSettings
|
||||
@ObservedObject var viewModel: MainViewModel
|
||||
|
||||
@State fileprivate var alertType: SettingsAlert = .NONE
|
||||
@State var showAlert: Bool = false
|
||||
|
||||
var body: some View {
|
||||
List {
|
||||
SettingsSection(title: "Language", description: "Language settings coming soon.")
|
||||
SettingsSection(title: "Accent Color", description: "The accent color setting will be released in a future update.")
|
||||
SettingsSection(title: "Log out", description: "Log out of your Nextcloud account in this app. Your recipes will be removed from local storage.")
|
||||
{
|
||||
Form {
|
||||
Section() {
|
||||
Link("Visit the GitHub page", destination: URL(string: "https://github.com/VincentMeilinger/Nextcloud-Cookbook-iOS")!)
|
||||
} header: {
|
||||
Text("About")
|
||||
} footer: {
|
||||
Text("If you are interested in contributing to this project or simply wish to review its source code, we encourage you to visit the GitHub repository for this application.")
|
||||
}
|
||||
|
||||
Section() {
|
||||
Link("Get support", destination: URL(string: "https://vincentmeilinger.github.io/Nextcloud-Cookbook-Client-Support/")!)
|
||||
} header: {
|
||||
Text("Support")
|
||||
} footer: {
|
||||
Text("If you have any inquiries, feedback, or require assistance, please refer to the support page for contact information.")
|
||||
}
|
||||
|
||||
Section() {
|
||||
Button("Log out") {
|
||||
print("Log out.")
|
||||
userSettings.serverAddress = ""
|
||||
userSettings.username = ""
|
||||
userSettings.token = ""
|
||||
userSettings.onboarding = true
|
||||
alertType = .LOG_OUT
|
||||
showAlert = true
|
||||
|
||||
}
|
||||
.buttonStyle(.borderedProminent)
|
||||
.accentColor(.red)
|
||||
.padding()
|
||||
}
|
||||
|
||||
SettingsSection(title: "Clear local data", description: "Your recipes will be removed from local storage.")
|
||||
{
|
||||
Button("Clear Cache") {
|
||||
.tint(.red)
|
||||
|
||||
Button("Delete local data.") {
|
||||
print("Clear cache.")
|
||||
viewModel.deleteAllData()
|
||||
alertType = .DELETE_CACHE
|
||||
showAlert = true
|
||||
}
|
||||
.buttonStyle(.borderedProminent)
|
||||
.accentColor(.red)
|
||||
.padding()
|
||||
.tint(.red)
|
||||
|
||||
} header: {
|
||||
Text("Danger Zone")
|
||||
}
|
||||
|
||||
}.navigationTitle("Settings")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
struct SettingsSection<Content: View>: View {
|
||||
let title: String
|
||||
let description: String
|
||||
@ViewBuilder let content: () -> Content
|
||||
|
||||
init(title: String, description: String, content: @escaping () -> Content) {
|
||||
self.title = title
|
||||
self.description = description
|
||||
self.content = content
|
||||
}
|
||||
|
||||
init(title: String, description: String) where Content == EmptyView {
|
||||
self.title = title
|
||||
self.description = description
|
||||
self.content = { EmptyView() }
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
HStack {
|
||||
VStack(alignment: .leading) {
|
||||
Text(title)
|
||||
.font(.headline)
|
||||
Text(description)
|
||||
.font(.caption)
|
||||
}.padding()
|
||||
Spacer()
|
||||
content()
|
||||
}
|
||||
|
||||
.navigationTitle("Settings")
|
||||
.alert(alertType.getTitle(), isPresented: $showAlert) {
|
||||
Button("Cancel", role: .cancel) { }
|
||||
if alertType == .LOG_OUT {
|
||||
Button("Log out", role: .destructive) { logOut() }
|
||||
} else if alertType == .DELETE_CACHE {
|
||||
Button("Delete", role: .destructive) { deleteCache() }
|
||||
}
|
||||
} message: {
|
||||
Text(alertType.getMessage())
|
||||
}
|
||||
}
|
||||
|
||||
func logOut() {
|
||||
userSettings.serverAddress = ""
|
||||
userSettings.username = ""
|
||||
userSettings.token = ""
|
||||
viewModel.deleteAllData()
|
||||
userSettings.onboarding = true
|
||||
}
|
||||
|
||||
func deleteCache() {
|
||||
//viewModel.deleteAllData()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user