import { Injectable } from '@angular/core'; import { Http } from '@angular/http'; import { AlertController, ToastController, Events } from 'ionic-angular'; import 'rxjs/add/operator/map'; import { DateTimeUtil, DateIdUtil } from '@canvuus-internal/mvp0-task-core'; import { AppUser, AppUserHelper } from '@canvuus-internal/mvp0-task-base'; import { UserProfile, UserPreference, UserProfileHelper, UserPreferenceHelper } from '@canvuus-internal/mvp0-task-data'; // import { AmNotice, PmNotice, AmNoticeHelper, PmNoticeHelper } from '@canvuus-internal/mvp0-task-data'; @Injectable() export class LocalUserService { // TBD: // If we are going to support multiple accounts (on a single device), // We will need UI for account selection (or, "login/logout"), etc. // For now, we just use a single user id. private _currentUserId: string; get currentUserId(): string { return this._currentUserId; } // set currentUserId(_currentUserId: string) { // this._currentUserId = _currentUserId; // } get todayId() { let today = DateIdUtil.getTodayId(); return today; } private _currentAppUser: AppUser; private _userProfile: UserProfile; private _userPreference: UserPreference; // To check if the user has been prompted at least once in this session (lifetime of this singleton). private _isNicknamePromptShown: boolean; constructor( private alertCtrl: AlertController, private toastCtrl: ToastController, private events: Events, private appUserHelper: AppUserHelper, private userProfileHelper: UserProfileHelper, private userPreferenceHelper: UserPreferenceHelper ) { // console.log('LocalUserService: Hello LocalUserService Provider'); // tbd: // for now, just hard coded: // this._currentUserId = AppUser.defaultUserId; this._currentUserId = AppUser.getDefaultUserId(); this._isNicknamePromptShown = false; // tbd: // We need to subscribe to data change events // .... } getNickname(): (string | null) { if (this._userProfile) { // based on last cache. return this._userProfile.nickname; } else { return null; } } // To be called by the Home page. doNicknameBasedProcessing(): Promise { let docId = UserProfile.getDocId(this.currentUserId); return this.userProfileHelper.retrieve(docId, (value: UserProfile) => { console.log('LocalUserService: doNicknameBasedProcessing(). value returned = ' + value); // Cache the result. this._userProfile = value; if (value && value.nickname) { // Validate nickname? } else { // if (!value) { // // create userprofile? // } // Prompt for nickname? // Do this only once in a while? Not every time? this._presentPromptForUserNickname(); } return this._userProfile; }).then((userProfile: UserProfile) => { return (userProfile) ? userProfile.nickname : null; }); } // testing private _presentPromptForUserNickname(): void { // console.log('LocalUserService: presentPromptForUserNickname()'); // We only show this once per "session". if (this._isNicknamePromptShown == true) { return; } this._isNicknamePromptShown = true; let alert = this.alertCtrl.create({ title: 'Welcome!', message: 'Please indicate how you want to be called for more personalized app experience.', inputs: [ { name: 'userNickname', placeholder: 'Nickname' } ], buttons: [ { text: 'Cancel', role: 'cancel', handler: data => { console.log('LocalUserService: Cancel clicked'); } }, { text: 'OK', handler: data => { if (data.userNickname) { // TBD: // Save the nickname. let userProfile: UserProfile; if(this._userProfile) { userProfile = this._userProfile.clone(); } else { userProfile = new UserProfile(this.currentUserId); } userProfile.setNickname(data.userNickname); this.userProfileHelper.update(userProfile, (value: UserProfile) => { if (value) { this._userProfile = value; // ??? } else { // ???? } // ??? return value; }); // return true; } else { // tbd: // just cancel? console.log('LocalUserService: Nickname not specified.'); // return false; } return true; } } ] }); alert.present(); } }