This commit includes various code changes necessary to get a basic proof of concept of the feature working. This is NOT a full working feature yet, only a preliminary prototype/PoC. It includes: - [X] Basic Storekit configuration - [X] Basic purchase mechanism - [X] Basic layout and copywriting - [X] Basic design - [X] Manage button (To help user cancel their subscription) - [X] Thank you confirmation + special welcome view - [X] Star badge on profile (by checking the Damus Purple API) - [X] Connection to Damus purple API for fetching account info, registering for an account and sending over the App Store receipt data The feature sits behind a feature flag which is OFF by default (it can be turned ON via Settings --> Developer settings --> Enable experimental Purple API and restarting the app) Testing --------- PASS Device: iPhone 15 Pro simulator iOS: 17.0.1 Damus: This commit damus-api: 59ce44a92cff1c1aaed9886f9befbd5f1053821d Server: Ubuntu 22.04 (VM) Setup: 1. On the server, delete the `mdb` database files to start from scratch 2. In iOS, reinstall the app if necessary to make sure there are no in-app purchases 3. Enable subscriptions support via developer settings with localhost test mode and restart app 4. Start server with mock parameters (Run `npm run dev`) Steps: 1. Open top bar and click on "Purple" 2. Purple screen should appear and show both benefits and the purchase options. PASS 3. Click on "monthly". An Apple screen to confirm purchase should appear. PASS 4. Welcome screen with animation should appear. PASS 5. Click continue and restart app (Due to known issue tracked at https://github.com/damus-io/damus/issues/1814) 6. Post something 7. Gold star should appear beside your name 8. Look at the server logs. There should be some requests to create the account (POST), to send the receipt (POST), and to get account status 9. Go to purple view. There should be some information about the subscription, as well as a "manage" button. PASS 10. Click on "manage" button. An iOS sheet should appear allow the user to unsubscribe or manage their subscription to Damus Purple. Feature flag testing -------------------- PASS Preconditions: Continue from above test Steps: 1. Disable Damus Purple experiment support on developer settings. Restart the app. 2. Check your post. There should be no star beside your profile name. PASS 3. Check side menu. There should be no "Damus Purple" option. PASS 4. Check server logs. There should be no new requests being done to the server. PASS Closes: https://github.com/damus-io/damus/issues/1422
144 lines
5.3 KiB
Swift
144 lines
5.3 KiB
Swift
//
|
|
// damusApp.swift
|
|
// damus
|
|
//
|
|
// Created by William Casarin on 2022-04-01.
|
|
//
|
|
|
|
import SwiftUI
|
|
import StoreKit
|
|
|
|
@main
|
|
struct damusApp: App {
|
|
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
|
|
var body: some Scene {
|
|
WindowGroup {
|
|
MainView(appDelegate: appDelegate)
|
|
}
|
|
}
|
|
}
|
|
|
|
struct MainView: View {
|
|
@State var needs_setup = false;
|
|
@State var keypair: Keypair? = nil;
|
|
@StateObject private var orientationTracker = OrientationTracker()
|
|
var appDelegate: AppDelegate
|
|
|
|
var body: some View {
|
|
Group {
|
|
if let kp = keypair, !needs_setup {
|
|
ContentView(keypair: kp, appDelegate: appDelegate)
|
|
.environmentObject(orientationTracker)
|
|
} else {
|
|
SetupView()
|
|
.onReceive(handle_notify(.login)) { notif in
|
|
needs_setup = false
|
|
keypair = get_saved_keypair()
|
|
if keypair == nil, let tempkeypair = notif.to_full()?.to_keypair() {
|
|
keypair = tempkeypair
|
|
}
|
|
}
|
|
}
|
|
}
|
|
.dynamicTypeSize(.xSmall ... .xxxLarge)
|
|
.onReceive(handle_notify(.logout)) { () in
|
|
try? clear_keypair()
|
|
keypair = nil
|
|
}
|
|
.onReceive(NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)) { _ in
|
|
orientationTracker.setDeviceMajorAxis()
|
|
}
|
|
.onAppear {
|
|
orientationTracker.setDeviceMajorAxis()
|
|
keypair = get_saved_keypair()
|
|
appDelegate.keypair = keypair
|
|
}
|
|
}
|
|
}
|
|
|
|
class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate {
|
|
var keypair: Keypair? = nil
|
|
var settings: UserSettingsStore? = nil
|
|
|
|
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
|
|
UNUserNotificationCenter.current().delegate = self
|
|
|
|
SKPaymentQueue.default().add(StoreObserver.standard)
|
|
return true
|
|
}
|
|
|
|
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
|
|
// Return if this feature is disabled
|
|
guard let settings = self.settings else { return }
|
|
if !settings.enable_experimental_push_notifications {
|
|
return
|
|
}
|
|
|
|
// Send the device token and pubkey to the server
|
|
let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
|
|
|
|
print("Received device token: \(token)")
|
|
|
|
guard let pubkey = keypair?.pubkey else {
|
|
return
|
|
}
|
|
|
|
// Send those as JSON to the server
|
|
let json: [String: Any] = ["deviceToken": token, "pubkey": pubkey.hex()]
|
|
|
|
// create post request
|
|
let url = settings.send_device_token_to_localhost ? Constants.DEVICE_TOKEN_RECEIVER_TEST_URL : Constants.DEVICE_TOKEN_RECEIVER_PRODUCTION_URL
|
|
var request = URLRequest(url: url)
|
|
request.httpMethod = "POST"
|
|
|
|
// insert json data to the request
|
|
request.httpBody = try? JSONSerialization.data(withJSONObject: json, options: [])
|
|
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
|
|
|
let task = URLSession.shared.dataTask(with: request) { data, response, error in
|
|
guard let data = data, error == nil else {
|
|
print(error?.localizedDescription ?? "No data")
|
|
return
|
|
}
|
|
|
|
if let response = response as? HTTPURLResponse, !(200...299).contains(response.statusCode) {
|
|
print("Unexpected status code: \(response.statusCode)")
|
|
return
|
|
}
|
|
|
|
let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
|
|
if let responseJSON = responseJSON as? [String: Any] {
|
|
print(responseJSON)
|
|
}
|
|
}
|
|
|
|
task.resume()
|
|
}
|
|
|
|
// Handle the notification in the foreground state
|
|
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
|
|
// Display the notification in the foreground
|
|
completionHandler([.banner, .list, .sound, .badge])
|
|
}
|
|
|
|
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
|
|
let userInfo = response.notification.request.content.userInfo
|
|
guard let notification = LossyLocalNotification.from_user_info(user_info: userInfo) else {
|
|
return
|
|
}
|
|
notify(.local_notification(notification))
|
|
completionHandler()
|
|
}
|
|
}
|
|
|
|
class OrientationTracker: ObservableObject {
|
|
var deviceMajorAxis: CGFloat = 0
|
|
func setDeviceMajorAxis() {
|
|
let bounds = UIScreen.main.bounds
|
|
let height = max(bounds.height, bounds.width) /// device's longest dimension
|
|
let width = min(bounds.height, bounds.width) /// device's shortest dimension
|
|
let orientation = UIDevice.current.orientation
|
|
deviceMajorAxis = (orientation == .portrait || orientation == .unknown) ? height : width
|
|
}
|
|
}
|