@@ -28,37 +28,47 @@ final class ProductVariationsViewModel {
2828 /// Generates all missing variations for a product. Up to 100 variations.
2929 /// Parameters:
3030 /// - `Product`: Product on which we will be creating the variations
31- /// - `onConfirmation`: Closure invoked before creating the variation as a confirmation guard.
32- /// It provides another `onCompletion` closure to be invoked with the confirmation result.
33- /// - `onCompletion`: Closure invoked when the generate process has ended.
31+ /// - `onStateChanged`: Closure invoked every time there is a significant state change in the generation process.
3432 ///
35- func generateAllVariations( for product: Product ,
36- onConfirmation: @escaping ( _ numberOfVariations: Int , _ onCompletion: @escaping ( Bool ) -> Void ) -> Void ,
37- onCompletion: @escaping ( Result < Void , GenerationError > ) -> Void ) {
33+ func generateAllVariations( for product: Product , onStateChanged: @escaping ( GenerationState ) -> Void ) {
3834
35+ // Fetch Previous variations
36+ onStateChanged ( . fetching)
3937 fetchAllVariations ( of: product) { [ weak self] result in
40- guard let self else { return }
4138 switch result {
4239 case . success( let existingVariations) :
4340
41+ // Generate variations locally
4442 let variationsToGenerate = ProductVariationGenerator . generateVariations ( for: product, excluding: existingVariations)
4543
4644 // Guard for 100 variation limit
4745 guard variationsToGenerate. count <= 100 else {
48- return onCompletion ( . failure ( . tooManyVariations( variationCount: variationsToGenerate. count) ) )
46+ return onStateChanged ( . error ( . tooManyVariations( variationCount: variationsToGenerate. count) ) )
4947 }
5048
49+ // Guard for no variations to generate
5150 guard variationsToGenerate. count > 0 else {
52- // TODO: Inform user that no variation will be created
53- return onCompletion ( . success( ( ) ) )
51+ return onStateChanged ( . finished( false ) )
5452 }
5553
56- // Wait for user confirmation before continuing
57- onConfirmation ( variationsToGenerate. count) { confirmed in
58- if confirmed {
59- self . createVariationsRemotely ( for: product, variations: variationsToGenerate, onCompletion: onCompletion)
54+ // Confirm generation with merchant
55+ onStateChanged ( . confirmation( variationsToGenerate. count, { confirmed in
56+
57+ guard confirmed else {
58+ return onStateChanged ( . canceled)
6059 }
61- }
60+
61+ // Create variations remotely
62+ onStateChanged ( . creating)
63+ self ? . createVariationsRemotely ( for: product, variations: variationsToGenerate) { result in
64+ switch result {
65+ case . success:
66+ onStateChanged ( . finished( true ) )
67+ case . failure( let error) :
68+ onStateChanged ( . error( error) )
69+ }
70+ }
71+ } ) )
6272
6373 case . failure:
6474 // TODO: Log and inform error
@@ -118,7 +128,36 @@ extension ProductVariationsViewModel {
118128 }
119129}
120130
131+ // MARK: Definitions for Generate All Variations
121132extension ProductVariationsViewModel {
133+ /// Type that represents the possible states while all variations are being created.
134+ ///
135+ enum GenerationState {
136+ /// State while previous variations are being fetched
137+ ///
138+ case fetching
139+
140+ /// State to allow merchant to confirm the variation generation
141+ ///
142+ case confirmation( _ numberOfVariations: Int , _ onCompletion: ( _ confirmed: Bool ) -> Void )
143+
144+ /// State while the variations are being created remotely
145+ ///
146+ case creating
147+
148+ ///State when the merchant decides to not continue with the generation process.
149+ ///
150+ case canceled
151+
152+ /// State when the the process is finished. `variationsCreated` indicates if variations were created or not.
153+ ///
154+ case finished( _ variationsCreated: Bool )
155+
156+ /// Error state in any part of the process.
157+ ///
158+ case error( GenerationError )
159+ }
160+
122161 /// Type to represent known generation errors
123162 ///
124163 enum GenerationError : LocalizedError , Equatable {
0 commit comments