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
}
struct AniServer: Codable {
var name: String
var url: URL
}
class AnisetteViewModel: ObservableObject {
@Published var selected: String = ""
@@ -35,29 +30,25 @@ class AnisetteViewModel: ObservableObject {
@Published var servers: [Server] = []
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 {
return
}
if let data = data {
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 {
self.servers = servers.servers.map { Server(name: $0.name, address: $0.address) }
self.servers = servers.servers
}
} 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 {
@@ -67,9 +58,8 @@ struct AnisetteServers: View {
var errorCallback: () -> ()
var body: some View {
NavigationView {
ZStack {
Color(UIColor(named: "SettingsBackground")!).ignoresSafeArea(.all)
Color(UIColor.systemBackground).ignoresSafeArea()
.onAppear {
viewModel.getListOfServers()
}
@@ -80,14 +70,17 @@ struct AnisetteServers: View {
VStack(alignment: .leading) {
Text("\(server.name.wrappedValue)")
.font(.headline)
.underline(true, color: .white)
.foregroundColor(.primary)
Text("\(server.address.wrappedValue)")
.fontWeight(.thin)
.font(.subheadline)
.foregroundColor(.secondary)
}
Spacer()
if selected != nil {
if server.address.wrappedValue == selected {
Spacer()
Image(systemName: "checkmark")
Image(systemName: "checkmark.circle.fill")
.foregroundColor(.accentColor)
.onAppear {
UserDefaults.standard.menuAnisetteURL = server.address.wrappedValue
print(UserDefaults.synchronize(.standard)())
@@ -97,13 +90,15 @@ struct AnisetteServers: View {
}
}
}
.backgroundStyle((selected == nil) ? Color(UIColor(named: "SettingsHighlighted")!) : Color(UIColor(named: "SettingsBackground")!))
.padding()
.background(RoundedRectangle(cornerRadius: 10).fill(Color(UIColor.secondarySystemBackground)))
.shadow(color: Color.gray.opacity(0.4), radius: 5, x: 0, y: 5)
.listRowSeparatorTint(.white)
.listRowBackground((selected == nil) ? Color(UIColor(named: "SettingsHighlighted")!).ignoresSafeArea(.all) : Color(UIColor(named: "SettingsBackground")!).ignoresSafeArea(.all))
.listRowBackground(Color(UIColor.systemBackground).ignoresSafeArea())
}
.listStyle(.plain)
.scrollContentBackground(.hidden)
.listRowBackground(Color(UIColor(named: "SettingsBackground")!).ignoresSafeArea(.all))
.listRowBackground(Color(UIColor.systemBackground).ignoresSafeArea())
} else {
List(selection: $selected) {
@@ -111,69 +106,90 @@ struct AnisetteServers: View {
VStack {
HStack {
Text("\(server.name.wrappedValue)")
.foregroundColor(.white)
.foregroundColor(.primary)
.frame(alignment: .center)
Text("\(server.address.wrappedValue)")
.foregroundColor(.white)
.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
}
if #available(iOS 15.0, *) {
VStack(spacing: 16) {
// TextField with gray background
TextField("Anisette Server List", text: $viewModel.source)
.padding(.leading, 5)
.padding(.vertical, 10)
.frame(alignment: .center)
.textFieldStyle(.plain)
.border(.white, width: 1)
.onSubmit {
UserDefaults.standard.menuAnisetteList = 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: {
viewModel.getListOfServers()
}, label: {
}) {
Text("Refresh Servers")
})
.padding(.bottom, 20)
SUIButton(role: .destructive, action: {
#if !DEBUG
.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)
}
// Reset Button below Back and Refresh
SUIButton(action: {
#if !DEBUG
if Keychain.shared.adiPb != nil {
Keychain.shared.adiPb = nil
}
#endif
#endif
print("Cleared adi.pb from keychain")
errorCallback()
self.presentationMode.wrappedValue.dismiss()
}, label: {
presentationMode.wrappedValue.dismiss()
}) {
Text("Reset adi.pb")
// if (selected != nil) {
// Text("\(selected!.uuidString)")
// }
})
.padding(.bottom, 20)
} else {
// Fallback on earlier versions
.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)
.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)
}
.navigationBarHidden(true) // Hide the navigation bar if you don't need it
.navigationTitle("") // Remove title to avoid extra space
}
}