using Mogafa.App.Behaviours; using Mogafa.Common; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DidabuCloud.Unity.Core { public partial class Didabu : MogafaBase { private readonly string BehavioursLocalStoragePrefixKey = "DidabuBehavioursLocalStorageKey"; private UserBehaviour userBehaviour = null; public event UserBehaviourChangedHandler BehaviourChanged; internal async Task GetUserBehaviour() { if(userBehaviour != null) { return userBehaviour; } userBehaviour = await LocalStorage.Get(BehavioursLocalStoragePrefixKey); if(userBehaviour == null) { userBehaviour = new UserBehaviour(); userBehaviour.BehaviourChanged += BehaviourChangedInternal; await LocalStorage.Set(BehavioursLocalStoragePrefixKey, userBehaviour); } return userBehaviour; } private void BehaviourChangedInternal(string code, long newValue, long oldValue) { BehaviourChanged?.Invoke(code, newValue, oldValue); } private async Task SetUserBehaviour() { if(userBehaviour == null) { return; } await LocalStorage.Set(BehavioursLocalStoragePrefixKey, userBehaviour); } public async Task AddBehaviour(string code, long value = 1) { var behaviour = await GetUserBehaviour(); var newValue = behaviour.Add(code, value); await SetUserBehaviour(); return newValue; } public async Task GetBehaviour(string code) { var behaviour = await GetUserBehaviour(); if(behaviour == null) { return 0L; } return behaviour.GetBehaviour(code); } } }