Skip to content

Commit

Permalink
Tune flow
Browse files Browse the repository at this point in the history
  • Loading branch information
yury authored and Carlos Cabanero committed Feb 6, 2023
1 parent d48e09c commit 36aaf44
Show file tree
Hide file tree
Showing 9 changed files with 969 additions and 282 deletions.
20 changes: 14 additions & 6 deletions Blink.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@
D2771511287F0EA200D31F4E /* libbuild_cli.a in Frameworks */ = {isa = PBXBuildFile; fileRef = D2771510287F0EA200D31F4E /* libbuild_cli.a */; };
D27AD9BC222FDD3D00379872 /* xcall.m in Sources */ = {isa = PBXBuildFile; fileRef = D27AD9BB222FDD3D00379872 /* xcall.m */; };
D27BBA1C20529FFF00AEA303 /* TermStream.m in Sources */ = {isa = PBXBuildFile; fileRef = D27BBA1B20529FFF00AEA303 /* TermStream.m */; };
D27C4DA42987F124008427F2 /* BuildHelp.swift in Sources */ = {isa = PBXBuildFile; fileRef = D27C4DA32987F124008427F2 /* BuildHelp.swift */; };
D27D0118261202A400128C23 /* KeyUIError.swift in Sources */ = {isa = PBXBuildFile; fileRef = D27D0117261202A400128C23 /* KeyUIError.swift */; };
D27D01272615F1BD00128C23 /* developer_setup.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = D27D01232615F1BD00128C23 /* developer_setup.xcconfig */; };
D2887A5622DC676F00701BD5 /* SpaceController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2887A5522DC676F00701BD5 /* SpaceController.swift */; };
Expand All @@ -331,6 +332,7 @@
D2A0C2172600D16300F0DF97 /* Protobuf_C_.xcframework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = D2334ECB25C1C04700385378 /* Protobuf_C_.xcframework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
D2A5221E230D279B0010AC04 /* SmarterTermInput.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2A5221D230D279B0010AC04 /* SmarterTermInput.swift */; };
D2A52227231304FF0010AC04 /* UIGestureRecognizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2A52226231304FE0010AC04 /* UIGestureRecognizer.swift */; };
D2A54CB129801062009D79FE /* BuildAccountModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2A54CB029801062009D79FE /* BuildAccountModel.swift */; };
D2A6398928CFA0B90066FD18 /* SwiftCBOR in Frameworks */ = {isa = PBXBuildFile; productRef = D2A6398828CFA0B90066FD18 /* SwiftCBOR */; };
D2A80979270713D200CD0FAF /* FeatureFlags.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2A80978270713D200CD0FAF /* FeatureFlags.swift */; };
D2A9B2F7272E6F26009FCBDE /* BlinkCode.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BDBFA3052728914F00C77798 /* BlinkCode.framework */; };
Expand Down Expand Up @@ -980,6 +982,7 @@
D27AD9BD222FDD5700379872 /* xcall.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = xcall.h; sourceTree = "<group>"; };
D27BBA1A20529FFF00AEA303 /* TermStream.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TermStream.h; sourceTree = "<group>"; };
D27BBA1B20529FFF00AEA303 /* TermStream.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = TermStream.m; sourceTree = "<group>"; };
D27C4DA32987F124008427F2 /* BuildHelp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BuildHelp.swift; sourceTree = "<group>"; };
D27D0117261202A400128C23 /* KeyUIError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyUIError.swift; sourceTree = "<group>"; };
D27D01232615F1BD00128C23 /* developer_setup.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = developer_setup.xcconfig; sourceTree = "<group>"; };
D2887A5522DC676F00701BD5 /* SpaceController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpaceController.swift; sourceTree = "<group>"; };
Expand All @@ -1000,6 +1003,7 @@
D2A0C63E20AAD98D001CF38F /* ios_error.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ios_error.h; sourceTree = "<group>"; };
D2A5221D230D279B0010AC04 /* SmarterTermInput.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SmarterTermInput.swift; sourceTree = "<group>"; };
D2A52226231304FE0010AC04 /* UIGestureRecognizer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIGestureRecognizer.swift; sourceTree = "<group>"; };
D2A54CB029801062009D79FE /* BuildAccountModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BuildAccountModel.swift; sourceTree = "<group>"; };
D2A80978270713D200CD0FAF /* FeatureFlags.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeatureFlags.swift; sourceTree = "<group>"; };
D2AB611D23AB5ACD00BE6585 /* UIApplication+Version.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UIApplication+Version.m"; sourceTree = "<group>"; };
D2AB611F23AB5AE000BE6585 /* UIApplication+Version.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UIApplication+Version.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2011,6 +2015,8 @@
isa = PBXGroup;
children = (
D2B788872949E8CA00F19E4F /* BuildRegion.swift */,
D2A54CB029801062009D79FE /* BuildAccountModel.swift */,
D27C4DA32987F124008427F2 /* BuildHelp.swift */,
);
path = Build;
sourceTree = "<group>";
Expand Down Expand Up @@ -2972,6 +2978,7 @@
BD9EA212271F824900874007 /* Publisher.swift in Sources */,
D241CBDF23040734003D64A5 /* KBKeyAccessibilityElement.swift in Sources */,
D2DE0DDE260331F300A69B6F /* NewKeyView.swift in Sources */,
D27C4DA42987F124008427F2 /* BuildHelp.swift in Sources */,
D2AD8E7A27A2BAFA00DED28D /* ShakeDetector.swift in Sources */,
D23890BD2900175100B5CEA6 /* FeatureColorPalette.swift in Sources */,
D2D75EE021AFDA10007336B6 /* LayoutManager.m in Sources */,
Expand Down Expand Up @@ -3079,6 +3086,7 @@
BD74A7A7290061DE00ED01CF /* WhatsNewInfo.swift in Sources */,
D264D2B228F84592002B1B14 /* GridView.swift in Sources */,
D2EFE1F520B7FAFC0087888B /* link_files.m in Sources */,
D2A54CB129801062009D79FE /* BuildAccountModel.swift in Sources */,
D2C24418238E44AB0082C69C /* KBConfigView.swift in Sources */,
D2BF5F7F265BA0A80070F839 /* UserDefaults.swift in Sources */,
D2F330CA20A6CB840074ADD7 /* help.m in Sources */,
Expand Down Expand Up @@ -3661,7 +3669,7 @@
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 665;
CURRENT_PROJECT_VERSION = 681;
GCC_C_LANGUAGE_STANDARD = gnu11;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
Expand Down Expand Up @@ -3706,7 +3714,7 @@
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 665;
CURRENT_PROJECT_VERSION = 681;
GCC_C_LANGUAGE_STANDARD = gnu11;
INFOPLIST_FILE = BlinkFileProvider/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
Expand Down Expand Up @@ -3743,7 +3751,7 @@
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 665;
CURRENT_PROJECT_VERSION = 681;
GCC_C_LANGUAGE_STANDARD = gnu11;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
Expand Down Expand Up @@ -3789,7 +3797,7 @@
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 665;
CURRENT_PROJECT_VERSION = 681;
GCC_C_LANGUAGE_STANDARD = gnu11;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = BlinkFileProviderUI/Info.plist;
Expand Down Expand Up @@ -4398,7 +4406,7 @@
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 665;
CURRENT_PROJECT_VERSION = 681;
DEAD_CODE_STRIPPING = NO;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEFINES_MODULE = YES;
Expand Down Expand Up @@ -4443,7 +4451,7 @@
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 665;
CURRENT_PROJECT_VERSION = 681;
DEAD_CODE_STRIPPING = NO;
DEFINES_MODULE = YES;
ENABLE_BITCODE = NO;
Expand Down
77 changes: 66 additions & 11 deletions Blink/BuildApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,34 @@
import Foundation
import RevenueCat


struct BuildAccountInfo: Decodable {
let build_id: String
let region: String
let email: String
}

struct BuildUsageBalance: Decodable {
let build_id: String
let balance_id: String
let status: String
let credits_available: UInt64
let credits_consumed: UInt64
let period_start: UInt64
let period_end: UInt64
let initial_outstanding_debit: UInt64
let last_charges_timestamp: UInt64?
let previous_balance_id: String?

var periodStartDate: Date {
Date(timeIntervalSince1970: TimeInterval(period_start))
}

var periodEndDate: Date {
Date(timeIntervalSince1970: TimeInterval(period_end))
}
}

enum BuildAPIError: Error, LocalizedError {
case invalidResponse
case unexpectedResponseStatus(Int)
Expand Down Expand Up @@ -72,22 +100,38 @@ enum BuildAPI {

}

public static func accountInfo() async {

let (code, data) = try await requestService(.init(getJson: _path("/account")))
let s = String(data: data, encoding: .utf8)!;
public static func accountInfo() async throws -> BuildAccountInfo {
let (code, data) = await requestService(.init(getJson: _path("/account")))
if code == 200 {
return try JSONDecoder().decode(BuildAccountInfo.self, from: data)
}
print("Unexpected response: \(code) \(String(data: data, encoding: .utf8) as Any)")
throw BuildAPIError.unexpectedResponseStatus(Int(code))
}

public static func accountCurrentUsageBalance() async throws -> BuildUsageBalance {
let (code, data) = await requestService(.init(getJson: _path("/account/current_usage_balance")))
if code == 200 {
print(s)
return try JSONDecoder().decode(BuildUsageBalance.self, from: data)
}
throw BuildAPIError.unexpectedResponseStatus(Int(code))
}

public static func requestAccountDelete() async throws {
let (code, _) = await requestService(try .init(postJson: _path("/account/request_account_delete")))
if code == 200 {
return
}
throw BuildAPIError.unexpectedResponseStatus(Int(code))
}

private static func _baseURL() -> String {
let options = PublishingOptions.current;
if options.intersection([PublishingOptions.testFlight, PublishingOptions.developer]).isEmpty {
return "https://api.blink.build"
} else {
return "https://raw.api.blink.build"
if FeatureFlags.blinkBuildStaging {
if FileManager.default.fileExists(atPath: BlinkPaths.blinkBuildStagingMarkURL()!.path) {
return "https://raw.api.blink.build"
}
}
return "https://api.blink.build"
}

private static func _path(_ path: String) -> URL {
Expand Down Expand Up @@ -199,11 +243,20 @@ enum BuildAPI {


extension URLRequest {
init(postJson url: URL, params: [String: Any]) throws {
init(postJson url: URL, params: [String: Any] = [:]) throws {
self.init(url: url)
self.httpMethod = "POST"
self.addValue("application/json", forHTTPHeaderField: "Content-Type")
self.httpBody = try JSONSerialization.data(withJSONObject: params)
}

init(deleteJson url: URL, params: [String: Any]? = nil) throws {
self.init(url: url)
self.httpMethod = "DELETE"
self.addValue("application/json", forHTTPHeaderField: "Content-Type")
if let params = params {
self.httpBody = try JSONSerialization.data(withJSONObject: params)
}
}

init(getJson url: URL, params: [String: Any] = [:]) {
Expand All @@ -213,4 +266,6 @@ extension URLRequest {
self.httpMethod = "GET"
self.addValue("application/json", forHTTPHeaderField: "Content-Type")
}


}
1 change: 1 addition & 0 deletions Blink/FeatureFlags.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import Foundation
extension FeatureFlags {
@objc static let noSubscriptionNag = _enabled(for: .developer, .testFlight)
@objc static let blinkBuild = _enabled(for: .developer, .testFlight)
@objc static let blinkBuildStaging = _enabled(for: .developer, .testFlight)
@objc static let checkReceipt = _enabled(for: .legacy)
@objc static let earlyAccessFeatures = _enabled(for: .developer, .testFlight)
}
Expand Down
58 changes: 4 additions & 54 deletions Blink/Subscriptions/PurchasesUserModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ class PurchasesUserModel: ObservableObject {

// @Published var flow: Int = 0

// MARK: Signup
@Published var signupInProgress: Bool = false

// MARK: Migration states

@Published var receiptIsVerified: Bool = false
Expand All @@ -59,18 +56,6 @@ class PurchasesUserModel: ObservableObject {
@Published var alertErrorMessage: String = ""
@Published var migrationStatus: MigrationStatus = .validating

// MARK: Blink Build states

@Published var email: String = "" {
didSet {
emailIsValid = !email.isEmpty && _emailPredicate.evaluate(with: email)
}
}

@Published var emailIsValid: Bool = false
@Published var buildRegion: BuildRegion = BuildRegion.usEast1
@Published var hasBuildToken: Bool = false

// MARK: Paywall

@Published var paywallPageIndex: Int = 0
Expand All @@ -82,46 +67,13 @@ class PurchasesUserModel: ObservableObject {
static let shared = PurchasesUserModel()

func refresh() {
_checkBuildToken(animated: false)
BuildAccountModel.shared.checkBuildToken(animated: false)
if self.plusProduct == nil || self.classicProduct == nil || self.buildBasicProduct == nil {
self.fetchProducts()
}
}

private func _checkBuildToken(animated: Bool) {
let value = FileManager.default.fileExists(atPath: BlinkPaths.blinkBuildTokenURL().path)
guard self.hasBuildToken != value else {
return
}
if animated {
withAnimation {
self.hasBuildToken = value
}
} else {
self.hasBuildToken = value
}
}

func signup() async {
guard emailIsValid else {
self.alertErrorMessage = self.email.isEmpty ? "Email is Required" : "Valid email is Required"
return
}

self.signupInProgress = true

defer {
self.signupInProgress = false
}

do {
try await BuildAPI.signup(email: self.email, region: self.buildRegion)
self._checkBuildToken(animated: true)
} catch {
self.alertErrorMessage = error.localizedDescription
}
}

func purchaseBuildBasic() async {
guard let product = buildBasicProduct else {
self.alertErrorMessage = "Product should be loaded"
Expand All @@ -142,9 +94,8 @@ class PurchasesUserModel: ObservableObject {
if canceled {
return
}
// we have subscription. Lets try to signin first
try await BuildAPI.trySignin()
self._checkBuildToken(animated: true)

try await BuildAccountModel.shared.trySignIn()
} catch {
self.alertErrorMessage = error.localizedDescription
}
Expand Down Expand Up @@ -185,13 +136,12 @@ class PurchasesUserModel: ObservableObject {
if EntitlementsManager.shared.build.active {
Task {
do {
try await BuildAPI.signin()
try await BuildAccountModel.shared.singin();
} catch {
self.alertErrorMessage = error.localizedDescription
}
}
}
self._checkBuildToken(animated: false)
})
}

Expand Down
1 change: 1 addition & 0 deletions BlinkConfig/BlinkPaths.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
+ (NSURL *) blinkURL;
+ (NSURL *) blinkBuildURL;
+ (NSURL *) blinkBuildTokenURL;
+ (NSURL *)blinkBuildStagingMarkURL;
+ (NSURL *) sshURL;
+ (NSURL *) blinkSSHConfigFileURL;
+ (NSURL *) blinkGlobalSSHConfigFileURL;
Expand Down
7 changes: 7 additions & 0 deletions BlinkConfig/BlinkPaths.m
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ + (NSURL *)blinkBuildTokenURL
return [NSURL fileURLWithPath:[url stringByAppendingPathComponent:@".build.token"]];
}

+ (NSURL *)blinkBuildStagingMarkURL
{
NSString *url = [self blinkBuild];
return [NSURL fileURLWithPath:[url stringByAppendingPathComponent:@".staging"]];
}



+ (NSURL *)sshURL
{
Expand Down
Loading

0 comments on commit 36aaf44

Please sign in to comment.