Skip to content

Commit

Permalink
Merge pull request #62 from MrNocole/feature/enhance_tips
Browse files Browse the repository at this point in the history
Enhance tips when adding server
  • Loading branch information
Michael-128 authored Jan 27, 2025
2 parents a8fbeb9 + a38bc0e commit 42e5aeb
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 18 deletions.
51 changes: 45 additions & 6 deletions Localization/Localizations/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,6 @@
"state" : "translated",
"value" : "关于"
}
},
"zh-Hant" : {
"stringUnit" : {
"state" : "new",
"value" : "About"
}
}
}
},
Expand Down Expand Up @@ -807,6 +801,28 @@
}
}
},
"ADDING" : {
"localizations" : {
"ja" : {
"stringUnit" : {
"state" : "translated",
"value" : "追加する"
}
},
"pl" : {
"stringUnit" : {
"state" : "translated",
"value" : "DODAWANIE"
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "添加中"
}
}
}
},
"Advanced" : {
"localizations" : {
"ja" : {
Expand Down Expand Up @@ -4150,6 +4166,28 @@
}
}
},
"OK" : {
"localizations" : {
"ja" : {
"stringUnit" : {
"state" : "translated",
"value" : "OK"
}
},
"pl" : {
"stringUnit" : {
"state" : "translated",
"value" : "OK"
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "好的"
}
}
}
},
"Open Files.." : {
"localizations" : {
"ja" : {
Expand Down Expand Up @@ -5557,6 +5595,7 @@
}
},
"SAVE" : {
"extractionState" : "stale",
"localizations" : {
"ja" : {
"stringUnit" : {
Expand Down
51 changes: 44 additions & 7 deletions qBitControl/ViewModels/ServersView/ServerAddViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class ServerAddViewModel: ObservableObject {
@Published var isInvalidAlert = false;
@Published var invalidAlertMessage = "";

@Published var isCheckingConnection = false;

private var alertQueue: [String] = [];

init() { }
init(editServerId: String) {
self.editServerId = editServerId
Expand All @@ -31,11 +35,6 @@ class ServerAddViewModel: ObservableObject {
}
}

func showAlert(message: String) {
invalidAlertMessage = message;
isInvalidAlert = true;
}

func validateInputs() -> Bool {
if(!(url.contains("https://") || url.contains("http://"))) {
showAlert(message: "Include protocol in the URL - 'https://' or 'http://' depending on your setup.")
Expand All @@ -45,27 +44,65 @@ class ServerAddViewModel: ObservableObject {
return true;
}

func validateIsConnecting() -> Bool {
if (self.isCheckingConnection) {
showAlert(message: "Adding, Please wait")
return false;
}

return true;
}

func showAlert(message: String?) {
if let message = message {
alertQueue.append(message)
}

guard !isInvalidAlert, let message = alertQueue.first else {
return;
}

alertQueue.removeFirst()
invalidAlertMessage = message
isInvalidAlert = true
}

func alertDismissed() {
DispatchQueue.main.async {
self.showAlert(message: nil)
}
}

func addServer(server: Server) {
showAlert(message: "Add server success!")
serversHelper.addServer(server: server)
}

func addServer(dismiss: DismissAction) -> Void {
if(!validateInputs()) { return; }
if(!validateIsConnecting()) { return; }

let server = Server(name: friendlyName, url: url, username: username, password: password)

if(!isCheckConnection) {
if let editServerId = self.editServerId { serversHelper.removeServer(id: editServerId) }
serversHelper.addServer(server: server)
addServer(server: server)
dismiss()
}

self.isCheckingConnection = true

serversHelper.checkConnection(server: server, result: {
didConnect in
DispatchQueue.main.async {
if(didConnect) {
if let editServerId = self.editServerId { self.serversHelper.removeServer(id: editServerId) }
self.serversHelper.addServer(server: server)
self.addServer(server: server)
dismiss()
} else {
self.showAlert(message: "Can't connect to the server.")
}
self.isCheckingConnection = false
}
})
}
Expand Down
10 changes: 5 additions & 5 deletions qBitControl/Views/ServersViews/ServerAddView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ struct ServerAddView: View {
viewModel.addServer(dismiss: dismiss)
} label: {
Spacer()
if(viewModel.editServerId != nil) {
Text("SAVE")
.fontWeight(.bold)
if(viewModel.isCheckingConnection) {
Text("ADDING")
} else {
Text("ADD")
.fontWeight(.bold)
}
Spacer()
}.buttonStyle(.borderedProminent)
}.listRowBackground(Color.blue)
}
.alert(isPresented: $viewModel.isInvalidAlert) {
Alert(title: Text("Invalid server information"), message: Text(viewModel.invalidAlertMessage))
Alert(title: Text("Invalid server information"), message: Text(viewModel.invalidAlertMessage), dismissButton: .default(Text("OK"), action: {
viewModel.alertDismissed()
}))
}
.toolbar() {
Button {
Expand Down

0 comments on commit 42e5aeb

Please sign in to comment.