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,113 +58,133 @@ struct AnisetteServers: View {
var errorCallback: () -> () var errorCallback: () -> ()
var body: some View { var body: some View {
NavigationView { ZStack {
ZStack { Color(UIColor.systemBackground)
Color(UIColor(named: "SettingsBackground")!).ignoresSafeArea(.all) .ignoresSafeArea()
.onAppear { .onAppear {
viewModel.getListOfServers() viewModel.getListOfServers()
} }
VStack { VStack {
if #available(iOS 16.0, *) { if #available(iOS 16.0, *) {
SwiftUI.List($viewModel.servers, id: \.address, selection: $selected) { server in SwiftUI.List($viewModel.servers, id: \.address, selection: $selected) { server in
HStack { HStack {
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)
if selected != nil { }
if server.address.wrappedValue == selected { Spacer()
Spacer() if selected != nil {
Image(systemName: "checkmark") if server.address.wrappedValue == selected {
.onAppear { Spacer()
UserDefaults.standard.menuAnisetteURL = server.address.wrappedValue Image(systemName: "checkmark.circle.fill")
print(UserDefaults.synchronize(.standard)()) .foregroundColor(.accentColor)
print(UserDefaults.standard.menuAnisetteURL) .onAppear {
print(server.address.wrappedValue) UserDefaults.standard.menuAnisetteURL = server.address.wrappedValue
} print(UserDefaults.synchronize(.standard)())
print(UserDefaults.standard.menuAnisetteURL)
print(server.address.wrappedValue)
} }
}
} }
.backgroundStyle((selected == nil) ? Color(UIColor(named: "SettingsHighlighted")!) : Color(UIColor(named: "SettingsBackground")!))
.listRowSeparatorTint(.white)
.listRowBackground((selected == nil) ? Color(UIColor(named: "SettingsHighlighted")!).ignoresSafeArea(.all) : Color(UIColor(named: "SettingsBackground")!).ignoresSafeArea(.all))
}
.listStyle(.plain)
.scrollContentBackground(.hidden)
.listRowBackground(Color(UIColor(named: "SettingsBackground")!).ignoresSafeArea(.all))
} else {
List(selection: $selected) {
ForEach($viewModel.servers, id: \.name) { server in
VStack {
HStack {
Text("\(server.name.wrappedValue)")
.foregroundColor(.white)
.frame(alignment: .center)
Text("\(server.address.wrappedValue)")
.foregroundColor(.white)
.frame(alignment: .center)
}
}
Spacer()
} }
} }
.listStyle(.plain) .padding()
// Fallback on earlier versions .background(RoundedRectangle(cornerRadius: 10).fill(Color(UIColor.secondarySystemBackground)))
.shadow(color: Color.black.opacity(0.2), radius: 5, x: 0, y: 5)
} }
if #available(iOS 15.0, *) { .listStyle(.plain)
TextField("Anisette Server List", text: $viewModel.source) .scrollContentBackground(.hidden)
.padding(.leading, 5) .listRowBackground(Color(UIColor.systemBackground))
.padding(.vertical, 10) } else {
.frame(alignment: .center) List(selection: $selected) {
.textFieldStyle(.plain) ForEach($viewModel.servers, id: \.name) { server in
.border(.white, width: 1) VStack {
.onSubmit { HStack {
UserDefaults.standard.menuAnisetteList = viewModel.source Text("\(server.name.wrappedValue)")
viewModel.getListOfServers() .foregroundColor(.primary)
.frame(alignment: .center)
Text("\(server.address.wrappedValue)")
.foregroundColor(.secondary)
.frame(alignment: .center)
}
} }
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)
}
VStack(spacing: 16) {
TextField("Anisette Server List", text: $viewModel.source)
.padding()
.background(RoundedRectangle(cornerRadius: 10).fill(Color(UIColor.secondarySystemFill)))
.foregroundColor(.primary)
.frame(height: 60)
.shadow(color: Color.black.opacity(0.2), radius: 5, x: 0, y: 5)
.onChange(of: viewModel.source) { newValue in
UserDefaults.standard.menuAnisetteList = newValue
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: { }
#if !DEBUG .buttonStyle(PlainButtonStyle())
if Keychain.shared.adiPb != nil { .padding()
Keychain.shared.adiPb = nil .background(RoundedRectangle(cornerRadius: 10).fill(Color.accentColor))
} .foregroundColor(.white)
#endif .shadow(color: Color.accentColor.opacity(0.4), radius: 10, x: 0, y: 5)
print("Cleared adi.pb from keychain")
errorCallback()
self.presentationMode.wrappedValue.dismiss()
}, label: {
Text("Reset adi.pb")
// if (selected != nil) {
// Text("\(selected!.uuidString)")
// }
})
.padding(.bottom, 20)
} else {
// Fallback on earlier versions
} }
SUIButton(action: {
#if !DEBUG
if Keychain.shared.adiPb != nil {
Keychain.shared.adiPb = nil
}
#endif
print("Cleared adi.pb from keychain")
errorCallback()
presentationMode.wrappedValue.dismiss()
}) {
Text("Reset adi.pb")
.fontWeight(.semibold)
.frame(maxWidth: .infinity)
}
.buttonStyle(PlainButtonStyle())
.padding()
.background(RoundedRectangle(cornerRadius: 10).fill(Color.red))
.foregroundColor(.white)
.shadow(color: Color.red.opacity(0.4), radius: 10, x: 0, y: 5)
} }
.padding(.horizontal)
.padding(.bottom)
} }
} }
.frame(maxWidth: .infinity, maxHeight: .infinity) .navigationBarHidden(true)
.navigationTitle("Anisette Servers") .navigationTitle("")
.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)
}
} }
} }