Commit c35d70b9922190e4063cb078b3b5d6926f80fd3e

Authored by vlad.lopatynets
1 parent 471c93e5

code review

Showing 20 changed files with 110 additions and 43 deletions
@@ -695,6 +695,11 @@ @@ -695,6 +695,11 @@
695 19F65A722ACBFD7300B50F61 /* Common */ = { 695 19F65A722ACBFD7300B50F61 /* Common */ = {
696 isa = PBXGroup; 696 isa = PBXGroup;
697 children = ( 697 children = (
  698 + 19B739742AE275FF0073AA59 /* Models */,
  699 + 199DB1EC2AD3FCE1007E6A81 /* Services */,
  700 + 190DBA012AC45CDA000A7BF3 /* Extentions */,
  701 + 193B3B812ACAF36C002161ED /* DataBases */,
  702 + 190DB9FD2AC459CD000A7BF3 /* Managers */,
698 19F65A732ACBFDD100B50F61 /* Transitions */, 703 19F65A732ACBFDD100B50F61 /* Transitions */,
699 ); 704 );
700 path = Common; 705 path = Common;
@@ -732,14 +737,9 @@ @@ -732,14 +737,9 @@
732 19FCBF202AC1727800F83A7F /* browser */ = { 737 19FCBF202AC1727800F83A7F /* browser */ = {
733 isa = PBXGroup; 738 isa = PBXGroup;
734 children = ( 739 children = (
735 - 19B739742AE275FF0073AA59 /* Models */,  
736 - 19FCBF382AC17A4800F83A7F /* Modules */,  
737 - 199DB1EC2AD3FCE1007E6A81 /* Services */,  
738 19F65A722ACBFD7300B50F61 /* Common */, 740 19F65A722ACBFD7300B50F61 /* Common */,
739 - 190DBA012AC45CDA000A7BF3 /* Extentions */,  
740 - 193B3B812ACAF36C002161ED /* DataBases */, 741 + 19FCBF382AC17A4800F83A7F /* Modules */,
741 19F65A712ACBFCB500B50F61 /* Resources */, 742 19F65A712ACBFCB500B50F61 /* Resources */,
742 - 190DB9FD2AC459CD000A7BF3 /* Managers */,  
743 19FCBF352AC1779800F83A7F /* Navigation */, 743 19FCBF352AC1779800F83A7F /* Navigation */,
744 19FCBF212AC1727800F83A7F /* AppDelegate.swift */, 744 19FCBF212AC1727800F83A7F /* AppDelegate.swift */,
745 19FCBF2F2AC1727900F83A7F /* Info.plist */, 745 19FCBF2F2AC1727900F83A7F /* Info.plist */,
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  3 +<plist version="1.0">
  4 +<dict>
  5 + <key>SchemeUserState</key>
  6 + <dict>
  7 + <key>AdBlocker.xcscheme_^#shared#^_</key>
  8 + <dict>
  9 + <key>orderHint</key>
  10 + <integer>0</integer>
  11 + </dict>
  12 + <key>GettingStarted (Playground) 1.xcscheme</key>
  13 + <dict>
  14 + <key>isShown</key>
  15 + <false/>
  16 + <key>orderHint</key>
  17 + <integer>6</integer>
  18 + </dict>
  19 + <key>GettingStarted (Playground) 2.xcscheme</key>
  20 + <dict>
  21 + <key>isShown</key>
  22 + <false/>
  23 + <key>orderHint</key>
  24 + <integer>7</integer>
  25 + </dict>
  26 + <key>GettingStarted (Playground).xcscheme</key>
  27 + <dict>
  28 + <key>isShown</key>
  29 + <false/>
  30 + <key>orderHint</key>
  31 + <integer>5</integer>
  32 + </dict>
  33 + <key>SnapKitPlayground (Playground) 1.xcscheme</key>
  34 + <dict>
  35 + <key>isShown</key>
  36 + <false/>
  37 + <key>orderHint</key>
  38 + <integer>3</integer>
  39 + </dict>
  40 + <key>SnapKitPlayground (Playground) 2.xcscheme</key>
  41 + <dict>
  42 + <key>isShown</key>
  43 + <false/>
  44 + <key>orderHint</key>
  45 + <integer>4</integer>
  46 + </dict>
  47 + <key>SnapKitPlayground (Playground).xcscheme</key>
  48 + <dict>
  49 + <key>isShown</key>
  50 + <false/>
  51 + <key>orderHint</key>
  52 + <integer>2</integer>
  53 + </dict>
  54 + <key>browser.xcscheme_^#shared#^_</key>
  55 + <dict>
  56 + <key>orderHint</key>
  57 + <integer>1</integer>
  58 + </dict>
  59 + </dict>
  60 +</dict>
  61 +</plist>
@@ -13,14 +13,12 @@ fileprivate enum StoreError: Error { @@ -13,14 +13,12 @@ fileprivate enum StoreError: Error {
13 } 13 }
14 14
15 final class StoreKitManager: ObservableObject { 15 final class StoreKitManager: ObservableObject {
16 - @Published  
17 - var storeProducts: [Product] = []  
18 - @Published  
19 - var purchasedSubscriptions : [Product] = [] 16 + @Published var storeProducts: [Product] = []
  17 + @Published var purchasedSubscriptions : [Product] = []
20 18
21 private var updateListenerTask: Task<Void, Error>? = nil 19 private var updateListenerTask: Task<Void, Error>? = nil
22 20
23 - private let productDict: [String : String] 21 + private let productDict: [String : String] // TODO: change to enum
24 22
25 init() { 23 init() {
26 if let plistPath = Bundle.main.path(forResource: "ProductList", ofType: "plist"), 24 if let plistPath = Bundle.main.path(forResource: "ProductList", ofType: "plist"),
@@ -76,7 +74,6 @@ final class StoreKitManager: ObservableObject { @@ -76,7 +74,6 @@ final class StoreKitManager: ObservableObject {
76 } 74 }
77 } 75 }
78 76
79 - @MainActor  
80 func updateCustomerProductStatus() async { 77 func updateCustomerProductStatus() async {
81 var purchasedCourses: [Product] = [] 78 var purchasedCourses: [Product] = []
82 79
@@ -113,4 +110,8 @@ final class StoreKitManager: ObservableObject { @@ -113,4 +110,8 @@ final class StoreKitManager: ObservableObject {
113 func isPurchased(_ product: Product) async throws -> Bool { 110 func isPurchased(_ product: Product) async throws -> Bool {
114 return purchasedSubscriptions.contains(product) 111 return purchasedSubscriptions.contains(product)
115 } 112 }
  113 +
  114 + func restorePurchase() async throws {
  115 + try await AppStore.sync()
  116 + }
116 } 117 }
@@ -66,7 +66,7 @@ extension RemoveAdvertViewController { @@ -66,7 +66,7 @@ extension RemoveAdvertViewController {
66 66
67 private func presentSubscriptionHandler() { 67 private func presentSubscriptionHandler() {
68 if(storeKit.purchasedSubscriptions.isEmpty) { 68 if(storeKit.purchasedSubscriptions.isEmpty) {
69 - let subcriptionViewController = SubscriptionViewController() 69 + let subcriptionViewController = SubscriptionViewController(storeKitManager: storeKit, completion: testHandler)
70 subcriptionViewController.modalPresentationStyle = .fullScreen 70 subcriptionViewController.modalPresentationStyle = .fullScreen
71 subcriptionViewController.modalTransitionStyle = .crossDissolve 71 subcriptionViewController.modalTransitionStyle = .crossDissolve
72 72
@@ -74,6 +74,13 @@ extension RemoveAdvertViewController { @@ -74,6 +74,13 @@ extension RemoveAdvertViewController {
74 } 74 }
75 } 75 }
76 76
  77 + private func testHandler() {
  78 + let alert = UIAlertController(title: "Title", message: "Success by subscription", preferredStyle: .alert)
  79 + let okAction = UIAlertAction(title: "OK", style: .default)
  80 + alert.addAction(okAction)
  81 + present(alert, animated: true)
  82 + }
  83 +
77 private func setupTableView() { 84 private func setupTableView() {
78 mainView.adBlockTableView.dataSource = self 85 mainView.adBlockTableView.dataSource = self
79 mainView.adBlockTableView.delegate = self 86 mainView.adBlockTableView.delegate = self
@@ -13,15 +13,14 @@ final class SubscriptionViewController: UIViewController { @@ -13,15 +13,14 @@ final class SubscriptionViewController: UIViewController {
13 private let mainView = SubscriptionView() 13 private let mainView = SubscriptionView()
14 14
15 private let tableViewData: [String] 15 private let tableViewData: [String]
16 -  
17 16
18 - override func viewDidLoad() {  
19 - super.viewDidLoad()  
20 -  
21 - initViewController()  
22 - } 17 + private let storeKit: StoreKitManager
  18 +
  19 + private let successCompletion: () -> Void
23 20
24 - init() { 21 + init(storeKitManager: StoreKitManager, completion: @escaping () -> Void) {
  22 + storeKit = storeKitManager
  23 + successCompletion = completion
25 self.tableViewData = StringConstants.removeAdvertTableViewData 24 self.tableViewData = StringConstants.removeAdvertTableViewData
26 25
27 super.init(nibName: nil, bundle: nil) 26 super.init(nibName: nil, bundle: nil)
@@ -31,6 +30,12 @@ final class SubscriptionViewController: UIViewController { @@ -31,6 +30,12 @@ final class SubscriptionViewController: UIViewController {
31 fatalError("init(coder:) has not been implemented") 30 fatalError("init(coder:) has not been implemented")
32 } 31 }
33 32
  33 + override func viewDidLoad() {
  34 + super.viewDidLoad()
  35 +
  36 + initViewController()
  37 + }
  38 +
34 override func loadView() { 39 override func loadView() {
35 view = mainView 40 view = mainView
36 } 41 }
@@ -70,6 +75,17 @@ extension SubscriptionViewController { @@ -70,6 +75,17 @@ extension SubscriptionViewController {
70 mainView.advantagesTableView.delegate = self 75 mainView.advantagesTableView.delegate = self
71 mainView.advantagesTableView.register(SubscriptionTableViewCell.self, forCellReuseIdentifier: SubscriptionTableViewCell.cellID) 76 mainView.advantagesTableView.register(SubscriptionTableViewCell.self, forCellReuseIdentifier: SubscriptionTableViewCell.cellID)
72 } 77 }
  78 +
  79 + @MainActor
  80 + private func buySubscription() async throws {
  81 + if let adblockerProduct = storeKit.storeProducts.first(where: { $0.id == "com.browser.adblocker" }) {
  82 + if let trusaction = try await storeKit.purchase(adblockerProduct) {
  83 +// trusaction.expirationDate //TODO: save to user defaults
  84 + dismiss(animated: true)
  85 + successCompletion()
  86 + }
  87 + }
  88 + }
73 } 89 }
74 90
75 91
@@ -82,29 +98,11 @@ extension SubscriptionViewController { @@ -82,29 +98,11 @@ extension SubscriptionViewController {
82 98
83 @objc 99 @objc
84 private func buyAdblocker(_ sender: UIButton) { 100 private func buyAdblocker(_ sender: UIButton) {
85 - guard let navigationController = presentingViewController as? NavigationViewController else {  
86 - return  
87 - }  
88 - for viewController in navigationController.viewControllers {  
89 - guard let removeAdvertViewController = viewController as? RemoveAdvertViewController else {  
90 - return  
91 - }  
92 - Task {  
93 - do {  
94 - if let adblockerProduct = removeAdvertViewController.storeKit.storeProducts.first(where: { $0.id == "com.browser.adblocker" }) {  
95 - if (try await removeAdvertViewController.storeKit.purchase(adblockerProduct)) != nil {  
96 - removeAdvertViewController.successfullPurchaseHandler()  
97 -  
98 - self.dismiss(animated: true)  
99 - } else {  
100 - print("Purchase cancelled or pending.")  
101 - }  
102 - } else {  
103 - print("Adblocker product not found.")  
104 - }  
105 - } catch {  
106 - print("Failed to purchase Adblocker: \(error)")  
107 - } 101 + Task {
  102 + do {
  103 + try await buySubscription()
  104 + } catch {
  105 + //TODO: show alert failed
108 } 106 }
109 } 107 }
110 } 108 }
Please register or login to post a comment