@@ -106,15 +106,20 @@ private async Task<bool> LoadCachedForm()
106106 if ( cachedFormData != null && cachedFormData . LogSteps . Count != 0 )
107107 {
108108 WorkInstructionStatus = Status . InProgress ;
109+
109110 ProductionLog = new ProductionLog
110111 {
111112 Id = cachedFormData . ProductionLogId ,
112113 LogSteps = cachedFormData . LogSteps . Select ( step => new ProductionLogStep
113114 {
114115 WorkInstructionStepId = step . WorkInstructionStepId ,
115- Success = step . Success ,
116- Notes = step . Notes ?? "" ,
117- SubmitTime = step . SubmitTime
116+ ProductionLogId = step . ProductionLogId ,
117+ Attempts = step . Attempts . Select ( a => new ProductionLogStepAttempt
118+ {
119+ Success = a . Success ,
120+ Notes = a . Notes ?? "" ,
121+ SubmitTime = a . SubmitTime
122+ } ) . ToList ( )
118123 } ) . ToList ( )
119124 } ;
120125 }
@@ -123,71 +128,75 @@ private async Task<bool> LoadCachedForm()
123128 return false ;
124129 }
125130
126- if ( ProductionLog . LogSteps . TrueForAll ( p => p . SubmitTime != DateTimeOffset . MinValue ) )
131+ if ( ProductionLog . LogSteps . All ( step =>
132+ step . Attempts . Any ( a => a . SubmitTime != DateTimeOffset . MinValue ) ) )
127133 {
128134 WorkInstructionStatus = Status . Completed ;
129135 }
130136
131137 return true ;
132138 }
133139
134-
135140 private async Task SetActiveWorkInstruction ( int workInstructionId )
136141 {
137142 if ( workInstructionId <= 0 )
138143 {
139144 ActiveWorkInstruction = null ;
140145 await SetSelectedWorkInstructionId ( null ) ;
141146 ProductionLogEventService . SetCurrentWorkInstructionName ( string . Empty ) ;
147+ return ;
142148 }
149+
143150 if ( ActiveProductWorkInstructionList != null )
144151 {
145152 var workInstruction = await WorkInstructionService . GetByIdAsync ( workInstructionId ) ;
146-
147153 if ( workInstruction ? . Products == null )
148- {
149154 return ;
150- }
151-
152-
153- await SetSelectedWorkInstructionId ( workInstructionId ) ;
154155
156+ // Reset the cached log and internal state
157+ await LocalCacheManager . SetNewProductionLogFormAsync ( null ) ;
158+ ProductionLog = new ProductionLog ( ) ; // clear current log
159+ await ProductionLogEventService . SetCurrentProductionLog ( ProductionLog ) ;
160+
161+ // Proceed with setting new state
162+ await SetSelectedWorkInstructionId ( workInstructionId ) ;
155163 ActiveWorkInstruction = workInstruction ;
156- ProductionLogEventService . SetCurrentWorkInstructionName ( ActiveWorkInstruction . Title ) ;
157-
164+ ProductionLogEventService . SetCurrentWorkInstructionName ( workInstruction . Title ) ;
158165 await LocalCacheManager . SetActiveWorkInstructionIdAsync ( workInstruction . Id ) ;
159166 }
160167 }
161168
162- private async Task SetActiveProduct ( int productId )
169+ private async Task SetActiveProduct ( int productId )
163170 {
164- if ( Products != null )
165- {
166- if ( productId < 0 )
167- {
168- ActiveWorkInstruction = null ;
169- ActiveProductWorkInstructionList = null ;
170- await SetActiveWorkInstruction ( - 1 ) ;
171- await LocalCacheManager . SetActiveProductAsync ( null ) ;
172- return ;
173- }
174-
175- var product = Products . FirstOrDefault ( p => p . Id == productId ) ;
176-
177- // The chosen product does not have any Work Instructions
178- if ( product ? . WorkInstructions == null )
179- {
180- return ;
181- }
171+ if ( Products == null )
172+ return ;
182173
183- ActiveProduct = product ;
184- ActiveProductWorkInstructionList = ActiveProduct . WorkInstructions . Where ( w => w . IsActive ) . ToList ( ) ;
185- ProductionLogEventService . SetCurrentProductName ( ActiveProduct . Name ) ;
174+ if ( productId < 0 )
175+ {
176+ ActiveWorkInstruction = null ;
177+ ActiveProductWorkInstructionList = null ;
186178 await SetActiveWorkInstruction ( - 1 ) ;
187-
188- await LocalCacheManager . SetActiveProductAsync ( product ) ;
179+ await LocalCacheManager . SetActiveProductAsync ( null ) ;
180+ return ;
189181 }
182+
183+ var product = Products . FirstOrDefault ( p => p . Id == productId ) ;
184+ if ( product ? . WorkInstructions == null )
185+ return ;
186+
187+ // Reset the cached log and internal state
188+ await LocalCacheManager . SetNewProductionLogFormAsync ( null ) ;
189+ ProductionLog = new ProductionLog ( ) ; // clear current log
190+ await ProductionLogEventService . SetCurrentProductionLog ( ProductionLog ) ;
191+
192+ // Proceed with setting new state
193+ ActiveProduct = product ;
194+ ActiveProductWorkInstructionList = product . WorkInstructions . Where ( w => w . IsActive ) . ToList ( ) ;
195+ ProductionLogEventService . SetCurrentProductName ( product . Name ) ;
196+ await SetActiveWorkInstruction ( - 1 ) ;
197+ await LocalCacheManager . SetActiveProductAsync ( product ) ;
190198 }
199+
191200
192201 private async Task GetCachedActiveProductAsync ( )
193202 {
@@ -411,13 +420,7 @@ private async Task OnStepCompleted(ProductionLogStep step, bool? success)
411420 {
412421 if ( ActiveWorkInstruction == null )
413422 return ;
414-
415- var currentTime = DateTimeOffset . UtcNow ;
416-
417- // If success is null, that means the button was unselected thus set time to default
418- step . SubmitTime = success == null ? DateTimeOffset . MinValue : currentTime ;
419-
420- step . Success = success ;
423+
421424 await ProductionLogEventService . SetCurrentProductionLog ( ProductionLog ) ;
422425 var currentStatus = await GetWorkInstructionStatus ( ) ;
423426 WorkInstructionStatus = currentStatus ? Status . Completed : Status . InProgress ;
@@ -473,20 +476,14 @@ private async Task<bool> GetWorkInstructionStatus()
473476 var result = false ;
474477 await Task . Run ( ( ) =>
475478 {
476- var t = ProductionLog . LogSteps . Find ( s => s . SubmitTime == default ) ;
477-
478- // If t is null then all steps have been completed
479- if ( t == null )
480- {
481- result = true ;
482- }
479+ result = ProductionLog . LogSteps . All ( step =>
480+ step . Attempts . Any ( a => a . SubmitTime != DateTimeOffset . MinValue ) ) ;
483481 } ) ;
484482
485483 return result ;
486484 }
487485 catch ( Exception e )
488486 {
489- Console . WriteLine ( e ) ;
490487 Log . Error ( "Error checking work instruction status: {Message}" , e . Message ) ;
491488 return false ;
492489 }
0 commit comments