Merge pull request #672 from 0-Blu/develop

UI Improvements for the Anisette Servers View.
This commit is contained in:
Stern
2024-08-12 20:46:44 -04:00
committed by GitHub

View File

@@ -23,11 +23,6 @@ struct Server: Codable {
var address: String var address: String
} }
struct AniServer: Codable {
var name: String
var url: URL
}
class AnisetteViewModel: ObservableObject { class AnisetteViewModel: ObservableObject {
@Published var selected: String = "" @Published var selected: String = ""
@@ -35,29 +30,25 @@ class AnisetteViewModel: ObservableObject {
@Published var servers: [Server] = [] @Published var servers: [Server] = []
func getListOfServers() { func getListOfServers() {
URLSession.shared.dataTask(with: URL(string: source)!) { data, response, error in guard let url = URL(string: source) else { return }
URLSession.shared.dataTask(with: url) { data, response, error in
if let error = error { if let error = error {
return return
} }
if let data = data { if let data = data {
do { do {
let servers = try Foundation.JSONDecoder().decode(AnisetteServerData.self, from: data) let decoder = Foundation.JSONDecoder()
let servers = try decoder.decode(AnisetteServerData.self, from: data)
DispatchQueue.main.async { DispatchQueue.main.async {
self.servers = servers.servers.map { Server(name: $0.name, address: $0.address) } self.servers = servers.servers
} }
} catch { } catch {
// Handle decoding error
print("Failed to decode JSON: \(error)")
} }
} }
}.resume()
} }
.resume()
for server in servers {
print(server)
print(server.name.count)
print(server.name)
}
}
} }
struct AnisetteServers: View { struct AnisetteServers: View {
@@ -67,9 +58,9 @@ struct AnisetteServers: View {
var errorCallback: () -> () var errorCallback: () -> ()
var body: some View { var body: some View {
NavigationView {
ZStack { ZStack {
Color(UIColor(named: "SettingsBackground")!).ignoresSafeArea(.all) Color(UIColor.systemBackground)
.ignoresSafeArea()
.onAppear { .onAppear {
viewModel.getListOfServers() viewModel.getListOfServers()
} }
@@ -80,14 +71,17 @@ struct AnisetteServers: View {
VStack(alignment: .leading) { VStack(alignment: .leading) {
Text("\(server.name.wrappedValue)") Text("\(server.name.wrappedValue)")
.font(.headline) .font(.headline)
.underline(true, color: .white) .foregroundColor(.primary)
Text("\(server.address.wrappedValue)") Text("\(server.address.wrappedValue)")
.fontWeight(.thin) .font(.subheadline)
.foregroundColor(.secondary)
} }
Spacer()
if selected != nil { if selected != nil {
if server.address.wrappedValue == selected { if server.address.wrappedValue == selected {
Spacer() Spacer()
Image(systemName: "checkmark") Image(systemName: "checkmark.circle.fill")
.foregroundColor(.accentColor)
.onAppear { .onAppear {
UserDefaults.standard.menuAnisetteURL = server.address.wrappedValue UserDefaults.standard.menuAnisetteURL = server.address.wrappedValue
print(UserDefaults.synchronize(.standard)()) print(UserDefaults.synchronize(.standard)())
@@ -97,51 +91,76 @@ struct AnisetteServers: View {
} }
} }
} }
.backgroundStyle((selected == nil) ? Color(UIColor(named: "SettingsHighlighted")!) : Color(UIColor(named: "SettingsBackground")!)) .padding()
.listRowSeparatorTint(.white) .background(RoundedRectangle(cornerRadius: 10).fill(Color(UIColor.secondarySystemBackground)))
.listRowBackground((selected == nil) ? Color(UIColor(named: "SettingsHighlighted")!).ignoresSafeArea(.all) : Color(UIColor(named: "SettingsBackground")!).ignoresSafeArea(.all)) .shadow(color: Color.black.opacity(0.2), radius: 5, x: 0, y: 5)
} }
.listStyle(.plain) .listStyle(.plain)
.scrollContentBackground(.hidden) .scrollContentBackground(.hidden)
.listRowBackground(Color(UIColor(named: "SettingsBackground")!).ignoresSafeArea(.all)) .listRowBackground(Color(UIColor.systemBackground))
} else { } else {
List(selection: $selected) { List(selection: $selected) {
ForEach($viewModel.servers, id: \.name) { server in ForEach($viewModel.servers, id: \.name) { server in
VStack { VStack {
HStack { HStack {
Text("\(server.name.wrappedValue)") Text("\(server.name.wrappedValue)")
.foregroundColor(.white) .foregroundColor(.primary)
.frame(alignment: .center) .frame(alignment: .center)
Text("\(server.address.wrappedValue)") Text("\(server.address.wrappedValue)")
.foregroundColor(.white) .foregroundColor(.secondary)
.frame(alignment: .center) .frame(alignment: .center)
} }
} }
Spacer() Spacer()
} }
.padding()
.background(RoundedRectangle(cornerRadius: 10).fill(Color(UIColor.secondarySystemBackground)))
.shadow(color: Color.black.opacity(0.2), radius: 5, x: 0, y: 5)
} }
.listStyle(.plain) .listStyle(.plain)
// Fallback on earlier versions
} }
if #available(iOS 15.0, *) {
VStack(spacing: 16) {
TextField("Anisette Server List", text: $viewModel.source) TextField("Anisette Server List", text: $viewModel.source)
.padding(.leading, 5) .padding()
.padding(.vertical, 10) .background(RoundedRectangle(cornerRadius: 10).fill(Color(UIColor.secondarySystemFill)))
.frame(alignment: .center) .foregroundColor(.primary)
.textFieldStyle(.plain) .frame(height: 60)
.border(.white, width: 1) .shadow(color: Color.black.opacity(0.2), radius: 5, x: 0, y: 5)
.onSubmit { .onChange(of: viewModel.source) { newValue in
UserDefaults.standard.menuAnisetteList = viewModel.source UserDefaults.standard.menuAnisetteList = newValue
viewModel.getListOfServers() viewModel.getListOfServers()
} }
HStack(spacing: 16) {
SUIButton(action: {
presentationMode.wrappedValue.dismiss()
}) {
Text("Back")
.fontWeight(.semibold)
.frame(maxWidth: .infinity)
}
.buttonStyle(PlainButtonStyle())
.padding()
.background(RoundedRectangle(cornerRadius: 10).fill(Color.accentColor))
.foregroundColor(.white)
.shadow(color: Color.accentColor.opacity(0.4), radius: 10, x: 0, y: 5)
SUIButton(action: { SUIButton(action: {
viewModel.getListOfServers() viewModel.getListOfServers()
}, label: { }) {
Text("Refresh Servers") Text("Refresh Servers")
}) .fontWeight(.semibold)
.padding(.bottom, 20) .frame(maxWidth: .infinity)
SUIButton(role: .destructive, action: { }
.buttonStyle(PlainButtonStyle())
.padding()
.background(RoundedRectangle(cornerRadius: 10).fill(Color.accentColor))
.foregroundColor(.white)
.shadow(color: Color.accentColor.opacity(0.4), radius: 10, x: 0, y: 5)
}
SUIButton(action: {
#if !DEBUG #if !DEBUG
if Keychain.shared.adiPb != nil { if Keychain.shared.adiPb != nil {
Keychain.shared.adiPb = nil Keychain.shared.adiPb = nil
@@ -149,31 +168,23 @@ struct AnisetteServers: View {
#endif #endif
print("Cleared adi.pb from keychain") print("Cleared adi.pb from keychain")
errorCallback() errorCallback()
self.presentationMode.wrappedValue.dismiss() presentationMode.wrappedValue.dismiss()
}, label: { }) {
Text("Reset adi.pb") Text("Reset adi.pb")
// if (selected != nil) { .fontWeight(.semibold)
// Text("\(selected!.uuidString)") .frame(maxWidth: .infinity)
// } }
}) .buttonStyle(PlainButtonStyle())
.padding(.bottom, 20) .padding()
} else { .background(RoundedRectangle(cornerRadius: 10).fill(Color.red))
// Fallback on earlier versions .foregroundColor(.white)
.shadow(color: Color.red.opacity(0.4), radius: 10, x: 0, y: 5)
}
.padding(.horizontal)
.padding(.bottom)
} }
} }
.navigationBarHidden(true)
.navigationTitle("")
} }
} }
.frame(maxWidth: .infinity, maxHeight: .infinity)
.navigationTitle("Anisette Servers")
.onAppear {
if UserDefaults.standard.menuAnisetteList != "" {
viewModel.source = UserDefaults.standard.menuAnisetteList
} else {
viewModel.source = "https://servers.sidestore.io/servers.json"
}
print(UserDefaults.standard.menuAnisetteURL)
print(UserDefaults.standard.menuAnisetteList)
}
}
}