-
-
Notifications
You must be signed in to change notification settings - Fork 890
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Marino Faggiana <[email protected]>
- Loading branch information
1 parent
2f69aef
commit a2767a2
Showing
8 changed files
with
192 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// SPDX-FileCopyrightText: Nextcloud GmbH | ||
// SPDX-FileCopyrightText: 2024 Marino Faggiana | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
import Foundation | ||
import NextcloudKit | ||
|
||
/// A model that allows the user to configure the account | ||
class NCTermOfServiceModel: ObservableObject { | ||
/// Root View Controller | ||
var controller: NCMainTabBarController? | ||
/// Set true for dismiss the view | ||
@Published var dismissView = false | ||
// Data | ||
@Published var languages: [String: String] = [:] | ||
@Published var terms: [String: String] = [:] | ||
@Published var termsId: [String: Int] = [:] | ||
@Published var hasUserSigned: Bool = false | ||
|
||
/// Initialization code | ||
init(controller: NCMainTabBarController?, tos: NKTermsOfService?) { | ||
self.controller = controller | ||
|
||
if let terms = tos?.getTerms() { | ||
for term in terms { | ||
self.terms[term.languageCode] = term.body | ||
self.termsId[term.languageCode] = term.id | ||
} | ||
} else { | ||
languages = ["en": "English", "de": "Deutsch", "it": "Italiano"] | ||
} | ||
|
||
if let languages = tos?.getLanguages() { | ||
for language in languages { | ||
if self.terms[language.key] != nil { | ||
self.languages[language.key] = language.value | ||
} | ||
} | ||
} else { | ||
terms = [ | ||
"en": "These are the Terms of Service.", | ||
"de": "Dies sind die Allgemeinen Geschäftsbedingungen.", | ||
"it": "Questi sono i Termini di servizio." | ||
] | ||
} | ||
|
||
if let hasUserSigned = tos?.hasUserSigned() { | ||
self.hasUserSigned = hasUserSigned | ||
} | ||
} | ||
|
||
func signTermsOfService(termId: Int?) { | ||
guard let termId, | ||
let controller | ||
else { | ||
return | ||
} | ||
|
||
NextcloudKit.shared.signTermsOfService(termId: "\(termId)", account: controller.account) { _, _, error in | ||
if error == .success { | ||
NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterGetServerData) | ||
self.dismissView = true | ||
} else { | ||
NCContentPresenter().showError(error: error) | ||
} | ||
} | ||
} | ||
|
||
deinit { } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// SPDX-FileCopyrightText: Nextcloud GmbH | ||
// SPDX-FileCopyrightText: 2024 Marino Faggiana | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
import SwiftUI | ||
|
||
struct NCTermOfServiceModelView: View { | ||
@State private var selectedLanguage = Locale.preferredLanguages.first?.components(separatedBy: "-").first ?? "en" | ||
@State private var termsText = "Loading terms..." | ||
@ObservedObject var model: NCTermOfServiceModel | ||
|
||
@Environment(\.presentationMode) var presentationMode | ||
|
||
var body: some View { | ||
VStack { | ||
HStack { | ||
Text(NSLocalizedString("_terms_of_service_", comment: "Terms of Service")) | ||
.font(.headline) | ||
.frame(maxWidth: .infinity, alignment: .leading) | ||
|
||
Picker("Select Language", selection: $selectedLanguage) { | ||
ForEach(model.languages.keys.sorted(), id: \.self) { key in | ||
Text(model.languages[key] ?? "").tag(key) | ||
} | ||
} | ||
.pickerStyle(MenuPickerStyle()) | ||
.frame(maxWidth: .infinity, alignment: .trailing) | ||
.onChange(of: selectedLanguage) { newLanguage in | ||
if let terms = model.terms[newLanguage] { | ||
termsText = terms | ||
} else { | ||
selectedLanguage = model.languages.first?.key ?? "en" | ||
termsText = model.terms[selectedLanguage] ?? "Terms not available in selected language." | ||
} | ||
} | ||
} | ||
.padding(.horizontal) | ||
|
||
ScrollView { | ||
Text(termsText) | ||
.font(.body) | ||
.foregroundColor(.primary) | ||
.frame(maxWidth: .infinity) | ||
.padding(.horizontal) | ||
} | ||
.padding(.top) | ||
|
||
Button(action: { | ||
model.signTermsOfService(termId: model.termsId[selectedLanguage]) | ||
}) { | ||
Text(model.hasUserSigned ? NSLocalizedString("_terms_accepted_", comment: "Accepted terms") : NSLocalizedString("_terms_accept_", comment: "Accept terms")) | ||
.foregroundColor(.white) | ||
.padding() | ||
.background(model.hasUserSigned ? Color.green : Color.blue) | ||
.cornerRadius(10) | ||
.padding(.bottom) | ||
} | ||
.disabled(model.hasUserSigned) | ||
} | ||
.padding() | ||
.onAppear { | ||
if let item = model.terms[selectedLanguage] { | ||
termsText = item | ||
} else { | ||
selectedLanguage = model.languages.first?.key ?? "en" | ||
termsText = model.terms[selectedLanguage] ?? "Terms not available in selected language." | ||
} | ||
} | ||
.onReceive(model.$dismissView) { newValue in | ||
if newValue { | ||
presentationMode.wrappedValue.dismiss() | ||
} | ||
} | ||
} | ||
} | ||
|
||
#Preview { | ||
NCTermOfServiceModelView(model: NCTermOfServiceModel(controller: nil, tos: nil)) | ||
} |