feat: show custom anisette server list if set (#717)

This commit is contained in:
Moonsn
2024-10-23 08:16:37 +08:00
committed by GitHub
parent 867a9c77e6
commit b21f80cdd7

View File

@@ -29,6 +29,13 @@ class AnisetteViewModel: ObservableObject {
@Published var source: String = "https://servers.sidestore.io/servers.json" @Published var source: String = "https://servers.sidestore.io/servers.json"
@Published var servers: [Server] = [] @Published var servers: [Server] = []
init() {
// using the custom Anisette list
if !UserDefaults.standard.menuAnisetteList.isEmpty {
self.source = UserDefaults.standard.menuAnisetteList
}
}
func getListOfServers() { func getListOfServers() {
guard let url = URL(string: source) else { return } guard let url = URL(string: source) else { return }
URLSession.shared.dataTask(with: url) { data, response, error in URLSession.shared.dataTask(with: url) { data, response, error in
@@ -55,6 +62,7 @@ struct AnisetteServers: View {
@Environment(\.presentationMode) var presentationMode @Environment(\.presentationMode) var presentationMode
@StateObject var viewModel: AnisetteViewModel = AnisetteViewModel() @StateObject var viewModel: AnisetteViewModel = AnisetteViewModel()
@State var selected: String? = nil @State var selected: String? = nil
@State private var showingConfirmation = false
var errorCallback: () -> () var errorCallback: () -> ()
var body: some View { var body: some View {
@@ -136,9 +144,13 @@ struct AnisetteServers: View {
SUIButton(action: { SUIButton(action: {
presentationMode.wrappedValue.dismiss() presentationMode.wrappedValue.dismiss()
}) { }) {
Text("Back") HStack{
.fontWeight(.semibold) Spacer()
.frame(maxWidth: .infinity) Text("Back")
.fontWeight(.semibold)
Spacer()
}
.contentShape(Rectangle())
} }
.buttonStyle(PlainButtonStyle()) .buttonStyle(PlainButtonStyle())
.padding() .padding()
@@ -149,26 +161,25 @@ struct AnisetteServers: View {
SUIButton(action: { SUIButton(action: {
viewModel.getListOfServers() viewModel.getListOfServers()
}) { }) {
Text("Refresh Servers") HStack{
.fontWeight(.semibold) Spacer()
.frame(maxWidth: .infinity) Text("Refresh Servers")
.fontWeight(.semibold)
.frame(maxWidth: .infinity)
Spacer()
}
.contentShape(Rectangle())
} }
.buttonStyle(PlainButtonStyle()) .buttonStyle(PlainButtonStyle())
.padding() .padding()
.background(RoundedRectangle(cornerRadius: 10).fill(Color.accentColor)) .background(RoundedRectangle(cornerRadius: 10).fill(Color.accentColor))
.foregroundColor(.white) .foregroundColor(.white)
.shadow(color: Color.accentColor.opacity(0.4), radius: 10, x: 0, y: 5) .shadow(color: Color.accentColor.opacity(0.4), radius: 10, x: 0, y: 5)
} }
SUIButton(action: { SUIButton(action: {
#if !DEBUG showingConfirmation = true
if Keychain.shared.adiPb != nil {
Keychain.shared.adiPb = nil
}
#endif
print("Cleared adi.pb from keychain")
errorCallback()
presentationMode.wrappedValue.dismiss()
}) { }) {
Text("Reset adi.pb") Text("Reset adi.pb")
.fontWeight(.semibold) .fontWeight(.semibold)
@@ -179,6 +190,25 @@ struct AnisetteServers: View {
.background(RoundedRectangle(cornerRadius: 10).fill(Color.red)) .background(RoundedRectangle(cornerRadius: 10).fill(Color.red))
.foregroundColor(.white) .foregroundColor(.white)
.shadow(color: Color.red.opacity(0.4), radius: 10, x: 0, y: 5) .shadow(color: Color.red.opacity(0.4), radius: 10, x: 0, y: 5)
.alert(isPresented: $showingConfirmation) {
Alert(
title: Text("Reset adi.pb"),
message: Text("are you sure to clear the adi.pb from keychain"),
primaryButton: .default(Text("do it")) {
#if !DEBUG
if Keychain.shared.adiPb != nil {
Keychain.shared.adiPb = nil
}
#endif
print("Cleared adi.pb from keychain")
errorCallback()
presentationMode.wrappedValue.dismiss()
},
secondaryButton: .cancel(Text("cancel")) {
print("canceled")
}
)
}
} }
.padding(.horizontal) .padding(.horizontal)
.padding(.bottom) .padding(.bottom)