2024-01-04 22:17:25 +01:00
|
|
|
//Made by Lumaa
|
|
|
|
|
|
|
|
import SwiftUI
|
2024-01-04 22:19:35 +01:00
|
|
|
import UIKit
|
2024-02-22 23:16:08 +01:00
|
|
|
import RevenueCat
|
2024-01-04 22:17:25 +01:00
|
|
|
|
2024-01-04 22:19:35 +01:00
|
|
|
@Observable
|
|
|
|
public class AppDelegate: NSObject, UIWindowSceneDelegate, Sendable, UIApplicationDelegate {
|
|
|
|
public var window: UIWindow?
|
|
|
|
public private(set) var windowWidth: CGFloat = UIScreen.main.bounds.size.width
|
|
|
|
public private(set) var windowHeight: CGFloat = UIScreen.main.bounds.size.height
|
2024-02-22 23:16:08 +01:00
|
|
|
public private(set) var secret: [String: String] = [:]
|
|
|
|
|
|
|
|
public static var premium: Bool = false
|
2024-01-04 22:19:35 +01:00
|
|
|
|
|
|
|
public func scene(_ scene: UIScene, willConnectTo _: UISceneSession, options _: UIScene.ConnectionOptions) {
|
|
|
|
guard let windowScene = scene as? UIWindowScene else { return }
|
|
|
|
window = windowScene.keyWindow
|
|
|
|
}
|
|
|
|
|
|
|
|
override public init() {
|
|
|
|
super.init()
|
2024-02-22 23:16:08 +01:00
|
|
|
|
|
|
|
if let path = Bundle.main.path(forResource: "Secret", ofType: "plist") {
|
|
|
|
let url = URL(fileURLWithPath: path)
|
|
|
|
let data = try! Data(contentsOf: url)
|
|
|
|
if let plist = try! PropertyListSerialization.propertyList(from: data, options: .mutableContainers, format: nil) as? [String: String] {
|
|
|
|
secret = plist
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let foundPremium = AppDelegate.hasPlus()
|
|
|
|
print("User has \(!foundPremium ? "no-" : "")access to Plus")
|
|
|
|
|
2024-01-04 22:19:35 +01:00
|
|
|
windowWidth = window?.bounds.size.width ?? UIScreen.main.bounds.size.width
|
|
|
|
windowHeight = window?.bounds.size.height ?? UIScreen.main.bounds.size.height
|
|
|
|
Self.observedSceneDelegate.insert(self)
|
|
|
|
_ = Self.observer // just for activating the lazy static property
|
|
|
|
}
|
|
|
|
|
2024-02-22 23:16:08 +01:00
|
|
|
static func readSecret() -> [String: String]? {
|
|
|
|
if let path = Bundle.main.path(forResource: "Secret", ofType: "plist") {
|
|
|
|
let url = URL(fileURLWithPath: path)
|
|
|
|
let data = try! Data(contentsOf: url)
|
|
|
|
if let plist = try! PropertyListSerialization.propertyList(from: data, options: .mutableContainers, format: nil) as? [String: String] {
|
|
|
|
return plist
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
static func hasPlus() -> Bool {
|
2024-02-24 13:19:57 +01:00
|
|
|
#if DEBUG
|
|
|
|
self.premium = true
|
|
|
|
return true
|
|
|
|
#else
|
2024-02-22 23:16:08 +01:00
|
|
|
Purchases.shared.getCustomerInfo { (customerInfo, error) in
|
|
|
|
self.premium = hasActuallyPlus(customerInfo: customerInfo)
|
|
|
|
}
|
|
|
|
return self.premium
|
2024-02-24 13:19:57 +01:00
|
|
|
#endif
|
2024-02-22 23:16:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static func hasActuallyPlus(customerInfo: CustomerInfo?) -> Bool {
|
|
|
|
return customerInfo?.entitlements[PlusEntitlements.lifetime.getEntitlementId()]?.isActive == true || customerInfo?.entitlements[PlusEntitlements.monthly.getEntitlementId()]?.isActive == true || customerInfo?.entitlements[PlusEntitlements.yearly.getEntitlementId()]?.isActive == true
|
|
|
|
}
|
|
|
|
|
2024-01-04 22:19:35 +01:00
|
|
|
deinit {
|
|
|
|
Task { @MainActor in
|
|
|
|
Self.observedSceneDelegate.remove(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static var observedSceneDelegate: Set<AppDelegate> = []
|
|
|
|
private static let observer = Task {
|
|
|
|
while true {
|
|
|
|
try? await Task.sleep(for: .seconds(0.1))
|
|
|
|
for delegate in observedSceneDelegate {
|
|
|
|
let newWidth = delegate.window?.bounds.size.width ?? UIScreen.main.bounds.size.width
|
|
|
|
if delegate.windowWidth != newWidth {
|
|
|
|
delegate.windowWidth = newWidth
|
|
|
|
}
|
|
|
|
let newHeight = delegate.window?.bounds.size.height ?? UIScreen.main.bounds.size.height
|
|
|
|
if delegate.windowHeight != newHeight {
|
|
|
|
delegate.windowHeight = newHeight
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2024-01-04 22:17:25 +01:00
|
|
|
}
|
|
|
|
}
|