From e87d380a9aab009df86762c75c30845efc1febb7 Mon Sep 17 00:00:00 2001 From: Laurent Date: Fri, 31 Mar 2023 15:28:36 +0200 Subject: [PATCH] Adds subscription button --- LeCountdown/Subscription/AppGuard.swift | 4 +- LeCountdown/Subscription/StoreView.swift | 32 ++++---- LeCountdown/Views/ContentView.swift | 76 ++++++++++++------- LeCountdown/Views/PresetsView.swift | 2 +- .../Views/Reusable/SoundSelectionView.swift | 4 +- 5 files changed, 66 insertions(+), 52 deletions(-) diff --git a/LeCountdown/Subscription/AppGuard.swift b/LeCountdown/Subscription/AppGuard.swift index 550a3a0..5130a4d 100644 --- a/LeCountdown/Subscription/AppGuard.swift +++ b/LeCountdown/Subscription/AppGuard.swift @@ -123,8 +123,8 @@ extension Notification.Name { return transaction } - var isAuthorized: Bool { - return self.currentPlan != .none + var isSubscriber: Bool { + return false // self.currentPlan != .none } var currentPlan: StorePlan { diff --git a/LeCountdown/Subscription/StoreView.swift b/LeCountdown/Subscription/StoreView.swift index b1d0a12..0542f0f 100644 --- a/LeCountdown/Subscription/StoreView.swift +++ b/LeCountdown/Subscription/StoreView.swift @@ -33,30 +33,26 @@ struct StoreView: View, StoreDelegate { @State var errorMessage: String? = nil - init() { - - self.store.delegate = self - -// if SKPaymentQueue.canMakePayments() { -// self._store = Store(delegate: self) -// } else { -// self.errorMessage = NSLocalizedString("In-app purchase disabled", comment: "") -// } - - } - var body: some View { - if !self.store.products.isEmpty { - PlanView() - .environmentObject(self.store) - } else { - ProgressView() - .progressViewStyle(.circular) + Group { + if !self.store.products.isEmpty { + PlanView() + .environmentObject(self.store) + } else { + ProgressView() + .progressViewStyle(.circular) + } + }.onAppear { + self._configure() } } + fileprivate func _configure() { + self.store.delegate = self + } + // MARK: - StoreDelegate func productsReceived() { diff --git a/LeCountdown/Views/ContentView.swift b/LeCountdown/Views/ContentView.swift index d5242a8..6dfd62d 100644 --- a/LeCountdown/Views/ContentView.swift +++ b/LeCountdown/Views/ContentView.swift @@ -10,7 +10,6 @@ import CoreData import Combine class BoringContext : ObservableObject { - @Published var isShowingNewData = false @Published var error: Error? @Published var showDefaultAlert: Bool = false @@ -18,35 +17,12 @@ class BoringContext : ObservableObject { @Published var siriTimer: AbstractTimer? = nil } -class TimerSpot : Identifiable, Equatable { - - var id: Int16 { return self.order } - - var order: Int16 - var timer: AbstractTimer? - - init(order: Int16, timer: AbstractTimer? = nil) { - self.order = order - self.timer = timer - } - - func setOrder(order: Int16) { - self.order = order - self.timer?.order = order - } - - static func == (lhs: TimerSpot, rhs: TimerSpot) -> Bool { - return lhs.order == rhs.order && lhs.timer?.stringId == rhs.timer?.stringId - } - -} - struct ContentView: View { - @StateObject var boringContext: BoringContext = BoringContext() - @EnvironmentObject var conductor: Conductor - @Environment(\.managedObjectContext) private var viewContext + @EnvironmentObject var conductor: Conductor + + @StateObject var boringContext: BoringContext = BoringContext() @State private var isEditing: Bool = false @@ -56,10 +32,25 @@ struct ContentView: View { @State private var showSettingsSheet: Bool = false + @State private var showSubscriptionSheet: Bool = false + var body: some View { VStack { + if !AppGuard.main.isSubscriber { + Button { + self.showSubscriptionSheet = true + } label: { + Text("Get fully enchanted") + .frame(maxWidth: .infinity) + } .background(Color.accentColor) + .cornerRadius(8.0) + .padding(.horizontal) + .buttonStyle(.bordered) + .foregroundColor(.white) + } + TimersView(isEditing: self.$isEditing, siriHandler: { timer in self._handleSiriTips(timer: timer) }) @@ -122,6 +113,9 @@ struct ContentView: View { SettingsView() .presentationDetents([.height(240.0)]) }) + .sheet(isPresented: self.$showSubscriptionSheet, content: { + StoreView() + }) .toolbar { ToolbarItem(placement: .navigationBarLeading) { Button { @@ -184,7 +178,7 @@ struct ContentView: View { print("_performActionIfPossible") let urlString = url.absoluteString - if let timer = viewContext.object(stringId: urlString) as? AbstractTimer { + if let timer = self.viewContext.object(stringId: urlString) as? AbstractTimer { if timer is T { print("performAction") @@ -261,9 +255,33 @@ fileprivate extension Countdown { } +class TimerSpot : Identifiable, Equatable { + + var id: Int16 { return self.order } + + var order: Int16 + var timer: AbstractTimer? + + init(order: Int16, timer: AbstractTimer? = nil) { + self.order = order + self.timer = timer + } + + func setOrder(order: Int16) { + self.order = order + self.timer?.order = order + } + + static func == (lhs: TimerSpot, rhs: TimerSpot) -> Bool { + return lhs.order == rhs.order && lhs.timer?.stringId == rhs.timer?.stringId + } + +} + struct ContentView_Previews: PreviewProvider { static var previews: some View { - ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext) .environmentObject(Conductor.maestro) + ContentView() + .environment(\.managedObjectContext, PersistenceController.preview.container.viewContext).environmentObject(Conductor.maestro) } } diff --git a/LeCountdown/Views/PresetsView.swift b/LeCountdown/Views/PresetsView.swift index de5ef77..e3fa023 100644 --- a/LeCountdown/Views/PresetsView.swift +++ b/LeCountdown/Views/PresetsView.swift @@ -119,7 +119,7 @@ struct PresetsView: View { } fileprivate func _showNewCountdown() { - if AppGuard.main.isAuthorized || viewContext.count(entityName: "AbstractTimer") < 4 { + if AppGuard.main.isSubscriber || viewContext.count(entityName: "AbstractTimer") < 4 { self.isShowingNewCountdown = true } else { self.isShowingSubscription = true diff --git a/LeCountdown/Views/Reusable/SoundSelectionView.swift b/LeCountdown/Views/Reusable/SoundSelectionView.swift index b37cb4e..2ae7949 100644 --- a/LeCountdown/Views/Reusable/SoundSelectionView.swift +++ b/LeCountdown/Views/Reusable/SoundSelectionView.swift @@ -46,7 +46,7 @@ struct PlaylistSectionView: View { self._playSound(sound) } Spacer() - if !sound.isRestricted || AppGuard.main.isAuthorized { + if !sound.isRestricted || AppGuard.main.isSubscriber { RightAlignToggleRow(item: sound, selected: self.model.binding(sound: sound), keyPath: \.formattedDuration) { selected in self.model.selectSound(sound, selected: selected) }.frame(width: 120.0) @@ -67,7 +67,7 @@ struct PlaylistSectionView: View { } fileprivate func _playSound(_ sound: Sound) { - if AppGuard.main.isAuthorized { + if AppGuard.main.isSubscriber { Conductor.maestro.playSound(sound) } else { Conductor.maestro.playSound(sound, duration: 30.0)