Better AnisetteServerList.swift (NEEDS TESTING!!!!!!)

Signed-off-by: Stephen <158498287+0-Blu@users.noreply.github.com>
This commit is contained in:
Stephen
2024-08-12 15:14:57 -04:00
committed by GitHub
parent aab4e62e24
commit 8b782c9416

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,138 @@ struct AnisetteServers: View {
var errorCallback: () -> () var errorCallback: () -> ()
var body: some View { var body: some View {
NavigationView { ZStack {
ZStack { Color(UIColor.systemBackground).ignoresSafeArea()
Color(UIColor(named: "SettingsBackground")!).ignoresSafeArea(.all) .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) .foregroundColor(.primary)
.underline(true, color: .white) Text("\(server.address.wrappedValue)")
Text("\(server.address.wrappedValue)") .font(.subheadline)
.fontWeight(.thin) .foregroundColor(.secondary)
} }
if selected != nil { Spacer()
if server.address.wrappedValue == selected { if selected != nil {
Spacer() if server.address.wrappedValue == selected {
Image(systemName: "checkmark") Spacer()
.onAppear { Image(systemName: "checkmark.circle.fill")
UserDefaults.standard.menuAnisetteURL = server.address.wrappedValue .foregroundColor(.accentColor)
print(UserDefaults.synchronize(.standard)()) .onAppear {
print(UserDefaults.standard.menuAnisetteURL) UserDefaults.standard.menuAnisetteURL = server.address.wrappedValue
print(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.gray.opacity(0.4), radius: 5, x: 0, y: 5)
.listRowSeparatorTint(.white)
.listRowBackground(Color(UIColor.systemBackground).ignoresSafeArea())
} }
if #available(iOS 15.0, *) { .listStyle(.plain)
TextField("Anisette Server List", text: $viewModel.source) .scrollContentBackground(.hidden)
.padding(.leading, 5) .listRowBackground(Color(UIColor.systemBackground).ignoresSafeArea())
.padding(.vertical, 10)
.frame(alignment: .center) } else {
.textFieldStyle(.plain) List(selection: $selected) {
.border(.white, width: 1) ForEach($viewModel.servers, id: \.name) { server in
.onSubmit { VStack {
UserDefaults.standard.menuAnisetteList = viewModel.source HStack {
viewModel.getListOfServers() Text("\(server.name.wrappedValue)")
.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.1), radius: 5, x: 0, y: 5)
}
.listStyle(.plain)
// Fallback on earlier versions
}
VStack(spacing: 16) {
// TextField with gray background
TextField("Anisette Server List", text: $viewModel.source)
.padding()
.background(RoundedRectangle(cornerRadius: 10).fill(Color.gray))
.foregroundColor(.white)
.frame(height: 60)
.shadow(color: Color.gray.opacity(0.4), radius: 10, x: 0, y: 5)
.onChange(of: viewModel.source) { newValue in
UserDefaults.standard.menuAnisetteList = newValue
viewModel.getListOfServers()
}
// Back and Refresh Buttons next to each other
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
} }
// Reset Button below Back and Refresh
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)
} }
} }
.frame(maxWidth: .infinity, maxHeight: .infinity) .navigationBarHidden(true) // Hide the navigation bar if you don't need it
.navigationTitle("Anisette Servers") .navigationTitle("") // Remove title to avoid extra space
.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)
}
} }
} }