< Summary

Class:DCL.SettingsCommon.SettingsModule[T]
Assembly:Settings
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/Settings/SettingsModule.cs
Covered lines:9
Uncovered lines:13
Coverable lines:22
Total lines:55
Line coverage:40.9% (9 of 22)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SettingsModule(...)0%110100%
Preload()0%2.982037.5%
Reset()0%2100%
Apply(...)0%12300%
Save()0%2100%
HasAnyData()0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/Settings/SettingsModule.cs

#LineLine coverage
 1using System;
 2using DCL.Helpers;
 3using UnityEngine;
 4
 5namespace DCL.SettingsCommon
 6{
 7    public class SettingsModule<T> : ISettingsRepository<T> where T : struct
 8    {
 9        public event Action<T> OnChanged;
 10
 11        private readonly string playerPrefsKey;
 12        private readonly T defaultPreset;
 13
 014        public T Data => dataValue;
 15        private T dataValue;
 16
 192017        public SettingsModule(string playerPrefsKey, T defaultPreset)
 18        {
 192019            this.playerPrefsKey = playerPrefsKey;
 192020            this.defaultPreset = defaultPreset;
 192021            Preload();
 192022        }
 23
 24        private void Preload()
 25        {
 192026            dataValue = defaultPreset;
 192027            if (!PlayerPrefsUtils.HasKey(playerPrefsKey))
 192028                return;
 29
 30            try
 31            {
 032                dataValue = JsonUtility.FromJson<T>(PlayerPrefsUtils.GetString(playerPrefsKey));
 033            }
 34            catch (Exception e)
 35            {
 036                Debug.LogError(e.Message);
 037            }
 038        }
 39
 040        public void Reset() { Apply(defaultPreset); }
 41
 42        public void Apply(T newSettings)
 43        {
 044            if (dataValue.Equals(newSettings))
 045                return;
 46
 047            dataValue = newSettings;
 048            OnChanged?.Invoke(dataValue);
 049        }
 50
 051        public void Save() { PlayerPrefsUtils.SetString(playerPrefsKey, JsonUtility.ToJson(dataValue)); }
 52
 30853        public bool HasAnyData() => !Data.Equals(defaultPreset);
 54    }
 55}