1- import { Component , Element , State , h } from '@stencil/core' ;
1+ import { Component , Element , State , h , Listen } from '@stencil/core' ;
22import { DatetimeChangeEventDetail } from '@ionic/core' ;
33
44import { differenceInMilliseconds , isAfter , startOfDay } from 'date-fns' ;
55
6- import { BehaviorSubject , Subscription } from 'rxjs ' ;
6+ import timerStore from '../../stores/timer.store ' ;
77
88import { Comparator } from '../../services/utils/utils' ;
99
10- import { TimerInterval , TimerService } from '../../services/timer/timer.service' ;
1110import { NotificationService } from '../../services/notification/notification.service' ;
11+ import { TimerService } from '../../services/timer/timer.service' ;
1212
1313@Component ( {
1414 tag : 'app-timer' ,
15- styleUrl : 'app-timer.scss'
15+ styleUrl : 'app-timer.scss' ,
1616} )
1717export class AppTimer {
1818 @Element ( ) el : HTMLElement ;
1919
20- private watcherSubscription : Subscription ;
21- private timerSubscription : Subscription ;
22-
23- @State ( )
24- private timerRemaining : number ;
25-
26- @State ( )
27- private timerLength : number ;
28-
2920 @State ( )
3021 private timerRunning : boolean ;
3122
@@ -35,6 +26,8 @@ export class AppTimer {
3526 private timerService : TimerService ;
3627 private notificationService : NotificationService ;
3728
29+ private destroyListener ;
30+
3831 constructor ( ) {
3932 this . timerService = TimerService . getInstance ( ) ;
4033 this . notificationService = NotificationService . getInstance ( ) ;
@@ -43,8 +36,6 @@ export class AppTimer {
4336 async componentDidLoad ( ) {
4437 this . notificationService . init ( ) ;
4538
46- this . watchTimer ( ) ;
47-
4839 this . timerRunning = await this . timerService . isTimerStarted ( ) ;
4940
5041 if ( this . timerRunning ) {
@@ -53,36 +44,11 @@ export class AppTimer {
5344 }
5445
5546 async componentDidUnload ( ) {
56- if ( this . timerSubscription ) {
57- this . timerSubscription . unsubscribe ( ) ;
58- }
59-
60- if ( this . watcherSubscription ) {
61- this . watcherSubscription . unsubscribe ( ) ;
47+ if ( this . destroyListener ) {
48+ this . destroyListener ( ) ;
6249 }
6350 }
6451
65- private watchTimer ( ) {
66- this . watcherSubscription = this . timerService . watch ( ) . subscribe ( ( timer : BehaviorSubject < TimerInterval > ) => {
67- if ( timer ) {
68- this . timerSubscription = timer . subscribe (
69- ( interval : TimerInterval ) => {
70- if ( interval ) {
71- this . timerRemaining = interval . timerRemaining ;
72- this . timerLength = interval . timerLength ;
73- }
74- } ,
75- ( _err ) => {
76- // Do nothing
77- } ,
78- async ( ) => {
79- await this . clearStopwatch ( ) ;
80- }
81- ) ;
82- }
83- } ) ;
84- }
85-
8652 async startTimer ( length : number ) {
8753 await this . timerService . start ( length ) ;
8854 this . timerRunning = true ;
@@ -99,18 +65,10 @@ export class AppTimer {
9965 this . timerPause = pause ;
10066 }
10167
102- private async clearStopwatch ( ) {
103- if ( this . timerSubscription ) {
104- this . timerSubscription . unsubscribe ( ) ;
105- }
106-
107- // Reset timerRemaining
108- this . timerRemaining = 0 ;
109- this . timerLength = 0 ;
68+ @Listen ( 'stopTimer' , { target : 'document' } )
69+ async clearStopwatch ( ) {
11070 this . timerRunning = false ;
11171
112- await this . timerService . clearEndAt ( ) ;
113-
11472 await this . resetDatetime ( ) ;
11573 }
11674
@@ -245,15 +203,15 @@ export class AppTimer {
245203 value = { startOfDay ( new Date ( ) ) . toDateString ( ) }
246204 onIonCancel = { ( ) => this . toggleFabActivated ( ) }
247205 onIonChange = { ( e : CustomEvent < DatetimeChangeEventDetail > ) => this . initTimerLengthAndStartTimer ( e ) } > </ ion-datetime >
248- </ ion-content >
206+ </ ion-content > ,
249207 ] ;
250208 }
251209
252210 private renderContent ( ) {
253- if ( this . timerRemaining >= 0 ) {
211+ if ( timerStore . state . remainingTime >= 0 && timerStore . state . timer ) {
254212 return (
255213 < div class = "content" >
256- < app-stopwatch length = { this . timerLength } remaining = { this . timerRemaining } > </ app-stopwatch >
214+ < app-stopwatch length = { timerStore . state . timer . timerLength } remaining = { timerStore . state . remainingTime } > </ app-stopwatch >
257215 </ div >
258216 ) ;
259217 } else {
@@ -274,11 +232,11 @@ export class AppTimer {
274232 }
275233
276234 private renderActions ( ) {
277- if ( this . timerRunning === null || ( this . timerRunning && this . timerRemaining === null ) ) {
235+ if ( this . timerRunning === null || ( this . timerRunning && timerStore . state . remainingTime === null ) ) {
278236 return undefined ;
279237 }
280238
281- const style = { visibility : `${ this . timerRemaining === null || this . timerRemaining === undefined ? 'hidden' : 'inherit' } ` } ;
239+ const style = { visibility : `${ timerStore . state . remainingTime === null || timerStore . state . remainingTime === undefined ? 'hidden' : 'inherit' } ` } ;
282240
283241 return (
284242 < ion-fab vertical = "bottom" horizontal = "end" slot = "fixed" onClick = { ( e : UIEvent ) => e . stopPropagation ( ) } style = { style } >
0 commit comments