11import { Injectable } from '@angular/core' ;
22import { Router } from '@angular/router' ;
3- import { Action } from '@ngrx/store' ;
3+ import { Action , Store } from '@ngrx/store' ;
44import { Actions , Effect , toPayload } from '@ngrx/effects' ;
55import { Observable } from 'rxjs/Observable' ;
66
7+ import 'rxjs/add/observable/of' ;
8+ import 'rxjs/add/observable/empty' ;
9+ import 'rxjs/add/observable/timer' ;
710import 'rxjs/add/operator/do' ;
811import 'rxjs/add/operator/map' ;
912import 'rxjs/add/operator/mergeMap' ;
13+ import 'rxjs/add/operator/withLatestFrom' ;
1014
1115import { NotificationType , IpcResponse } from 'uhk-common' ;
12- import { ActionTypes , ConnectionStateChangedAction , PermissionStateChangedAction } from '../actions/device' ;
16+ import {
17+ ActionTypes ,
18+ ConnectionStateChangedAction , HideSaveToKeyboardButton ,
19+ PermissionStateChangedAction ,
20+ SaveToKeyboardSuccessAction ,
21+ SaveToKeyboardSuccessFailed
22+ } from '../actions/device' ;
1323import { DeviceRendererService } from '../../services/device-renderer.service' ;
1424import { ShowNotificationAction } from '../actions/app' ;
25+ import { AppState } from '../index' ;
26+ import { UserConfiguration } from '../../config-serializer/config-items/user-configuration' ;
27+ import { UhkBuffer } from '../../config-serializer/uhk-buffer' ;
1528
1629@Injectable ( )
1730export class DeviceEffects {
18- @Effect ( { dispatch : false } )
31+ @Effect ( { dispatch : false } )
1932 deviceConnectionStateChange$ : Observable < Action > = this . actions$
2033 . ofType ( ActionTypes . CONNECTION_STATE_CHANGED )
2134 . map ( toPayload )
@@ -28,7 +41,7 @@ export class DeviceEffects {
2841 }
2942 } ) ;
3043
31- @Effect ( { dispatch : false } )
44+ @Effect ( { dispatch : false } )
3245 permissionStateChange$ : Observable < Action > = this . actions$
3346 . ofType ( ActionTypes . PERMISSION_STATE_CHANGED )
3447 . map ( toPayload )
@@ -41,7 +54,7 @@ export class DeviceEffects {
4154 }
4255 } ) ;
4356
44- @Effect ( { dispatch : false } )
57+ @Effect ( { dispatch : false } )
4558 setPrivilegeOnLinux$ : Observable < Action > = this . actions$
4659 . ofType ( ActionTypes . SET_PRIVILEGE_ON_LINUX )
4760 . do ( ( ) => {
@@ -67,35 +80,52 @@ export class DeviceEffects {
6780 ] ;
6881 } ) ;
6982
70- @Effect ( { dispatch : false } )
83+ @Effect ( { dispatch : false } )
7184 saveConfiguration$ : Observable < Action > = this . actions$
7285 . ofType ( ActionTypes . SAVE_CONFIGURATION )
73- . map ( toPayload )
74- . do ( ( buffer : Buffer ) => {
75- this . deviceRendererService . saveUserConfiguration ( buffer ) ;
76- } ) ;
86+ . withLatestFrom ( this . store )
87+ . map ( ( [ action , state ] ) => state . userConfiguration )
88+ . do ( ( userConfiguration : UserConfiguration ) => {
89+ setTimeout ( ( ) => this . sendUserConfigToKeyboard ( userConfiguration ) , 100 ) ;
90+ } )
91+ . switchMap ( ( ) => Observable . empty ( ) ) ;
7792
7893 @Effect ( )
7994 saveConfigurationReply$ : Observable < Action > = this . actions$
8095 . ofType ( ActionTypes . SAVE_CONFIGURATION_REPLY )
8196 . map ( toPayload )
82- . map ( ( response : IpcResponse ) => {
97+ . mergeMap ( ( response : IpcResponse ) => {
8398 if ( response . success ) {
84- return new ShowNotificationAction ( {
85- type : NotificationType . Success ,
86- message : 'Save configuration successful.'
87- } ) ;
99+ return [
100+ new SaveToKeyboardSuccessAction ( )
101+ ] ;
88102 }
89103
90- return new ShowNotificationAction ( {
91- type : NotificationType . Error ,
92- message : response . error . message
93- } ) ;
104+ return [
105+ new ShowNotificationAction ( {
106+ type : NotificationType . Error ,
107+ message : response . error . message
108+ } ) ,
109+ new SaveToKeyboardSuccessFailed ( )
110+ ] ;
94111 } ) ;
95112
113+ @Effect ( )
114+ autoHideSaveToKeyboardButton$ : Observable < Action > = this . actions$
115+ . ofType ( ActionTypes . SAVE_TO_KEYBOARD_SUCCESS )
116+ . switchMap ( ( ) => Observable . timer ( 1000 )
117+ . switchMap ( ( ) => Observable . of ( new HideSaveToKeyboardButton ( ) ) )
118+ ) ;
119+
96120 constructor ( private actions$ : Actions ,
97121 private router : Router ,
98- private deviceRendererService : DeviceRendererService ) {
122+ private deviceRendererService : DeviceRendererService ,
123+ private store : Store < AppState > ) {
99124 }
100125
126+ private sendUserConfigToKeyboard ( userConfiguration : UserConfiguration ) : void {
127+ const uhkBuffer = new UhkBuffer ( ) ;
128+ userConfiguration . toBinary ( uhkBuffer ) ;
129+ this . deviceRendererService . saveUserConfiguration ( uhkBuffer . getBufferContent ( ) ) ;
130+ }
101131}
0 commit comments