Initial commit

This commit is contained in:
oscarz
2024-08-12 10:49:20 +08:00
parent 3002510aaf
commit 00fd0adf89
331 changed files with 53210 additions and 130 deletions

View File

@ -6,13 +6,226 @@
//
import SwiftUI
import ToastUI
struct SettingsView: View {
var body: some View {
Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
import SafariServices
struct FullScreenSafariView: UIViewControllerRepresentable {
let url: URL
var onDismiss: (() -> Void)?
func makeUIViewController(context: UIViewControllerRepresentableContext<FullScreenSafariView>) -> SFSafariViewController {
let safariViewController = SFSafariViewController(url: url)
safariViewController.delegate = context.coordinator
return safariViewController
}
func updateUIViewController(_ uiViewController: SFSafariViewController, context: UIViewControllerRepresentableContext<FullScreenSafariView>) {
//
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, SFSafariViewControllerDelegate {
var parent: FullScreenSafariView
init(_ parent: FullScreenSafariView) {
self.parent = parent
}
func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
parent.onDismiss?()
}
}
}
struct SettingsView: View {
@EnvironmentObject var globalEnv: GlobalEnvironment //
@State private var isLoading = false //
@State private var showingToast = false // toast
@State private var toastText = ""
@State private var showVIPPaymentView = false
@EnvironmentObject var iapManager: IAPManager // IAPManager
@State private var showingFullSafari = false
@State private var showingAdvancedSettings = false //
@State private var useSandboxEnvironment = false
@State private var useDevelopmentEnvironment = false
var body: some View {
NavigationView {
ScrollView {
VStack(spacing: 0) {
// Upgrade to Premium
settingItem(icon: "arrow.up.circle", text: "Upgrade to Premium", isBold: true)
.onTapGesture {
showVIPPaymentView = true
}
// Feedback
settingItem(icon: "bubble.left", text: "Feedback")
.onTapGesture {
// Assuming there's a way to open App Store Feedback
openAppStoreFeedback()
}
// About
settingItem(icon: "info.circle", text: "About")
.onTapGesture {
// Code to show About View
self.showingFullSafari = true
}
// Restore Purchases
settingItem(icon: "arrow.clockwise.circle", text: "Restore Purchases", isBold: true)
.onTapGesture {
restorePurchase()
}
}
.fullScreenCover(isPresented: $showingFullSafari) {
FullScreenSafariView(url: URL(string: globalEnvironment.userTermsURL)!, onDismiss: {
self.showingFullSafari = false
})
}
// UI
gestureArea
if showingAdvancedSettings {
VStack {
Toggle("Sandbox", isOn: $useSandboxEnvironment)
Toggle("TestEnv", isOn: $useDevelopmentEnvironment)
Button("Save") {
saveSettings()
showingAdvancedSettings = false
}
}
.padding(.vertical,10)
.padding(.horizontal, 20)
.background(Color.white) // Ensure background fills the view
.cornerRadius(5)
}
}
.background(Color.pink.opacity(0.2)) // Ensure background fills the view
.navigationBarTitle("Settings", displayMode: .inline)
.fullScreenCover(isPresented: $showVIPPaymentView) {
VIPPaymentView()
}
.onDisappear {
self.showingAdvancedSettings = false //
}
}
.toast(isPresented: $showingToast, dismissAfter: globalEnvironment.toastPresentMsNormal) {
HStack {
Image(systemName: "exclamationmark.bubble")
.foregroundColor(.yellow)
Text(toastText)
.foregroundColor(.black)
}
.padding()
.background(Color.white)
.cornerRadius(8)
.shadow(radius: 10)
}
//
if isLoading {
LoadingView()
}
}
//
private var gestureArea: some View {
Color.clear
.contentShape(Rectangle()) //
.frame(height: 150) //
.gesture(
DragGesture(minimumDistance: 50, coordinateSpace: .global)
.onEnded { value in
//
if abs(value.translation.width) > 50 && abs(value.translation.height) < 100 {
//
logger.info("Advanced Settings.")
showingAdvancedSettings.toggle()
}
}
)
}
//
private func saveSettings() {
globalEnvironment.SetEnv(isSandBox: useSandboxEnvironment, isTestEnv: useDevelopmentEnvironment)
}
@ViewBuilder
private func settingItem(icon: String, text: String, isBold: Bool = false) -> some View {
HStack {
Image(systemName: icon) // Icon on the left side
.foregroundColor(.gray)
Text(text)
.font(.subheadline)
.fontWeight(isBold ? .bold : .regular) // Conditional bold based on isBold parameter
.padding(4)
Spacer()
}
.padding()
.background(Color.white) // Background for each setting item
.padding(.horizontal, 0) // Add some horizontal padding
// Indent divider to align with the text and extend to the edge
Divider()
.padding(.leading, 30)
.padding(.trailing, 10)
}
private func openAppStoreFeedback() {
let appID = globalEnvironment.APPID // ID
let urlStr = "https://apps.apple.com/app/id\(appID)?action=write-review"
if let url = URL(string: urlStr) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
func restorePurchase(){
Task {
await iapManager.restorePurchases() { result in
switch result {
case .success(_):
self.toastText = "Restored Sucess!"
self.showingToast = true
case .failure(_):
self.toastText = "Oh, something went wrong, please try again later."
self.showingToast = true
}
}
}
}
//
func loadData() {
isLoading = true
}
func loadComplete(){
isLoading = false
}
}
struct SettingsView_Previews: PreviewProvider {
static var previews: some View {
SettingsView()
}
}
#Preview {
SettingsView()
}