< Summary

Class:DCL.Helpers.PlayerPrefsProviderLocalStorage
Assembly:Utils
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/Utils/PlayerPrefsUtils/PlayerPrefsProviderLocalStorage.cs
Covered lines:0
Uncovered lines:29
Coverable lines:29
Total lines:112
Line coverage:0% (0 of 29)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Save()0%2100%
HasKey(...)0%2100%
GetInt(...)0%2100%
GetInt(...)0%12300%
SetInt(...)0%2100%
GetBool(...)0%12300%
SetBool(...)0%12300%
GetString(...)0%42600%
SetString(...)0%2100%
GetFloat(...)0%12300%
SetFloat(...)0%2100%
GetLocalStorageValue(...)0%6200%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/Utils/PlayerPrefsUtils/PlayerPrefsProviderLocalStorage.cs

#LineLine coverage
 1using System.Runtime.InteropServices;
 2using UnityEngine;
 3
 4namespace DCL.Helpers
 5{
 6    /// <summary>
 7    ///     Provider class to use LocalStorage instead of IndexedDB because it was not reliably saving settings.
 8    ///     For all the gets, we have to check that the PlayerPref key does exist. We may lose information that was
 9    ///     succesfully stored in IndexedDB. That check should only go once per user per key.
 10    /// </summary>
 11    public class PlayerPrefsProviderLocalStorage : IPlayerPrefsProvider
 12    {
 13        //Nothing to save when using local storage
 014        public void Save() { }
 15
 16        public bool HasKey(string key) =>
 017            HasKeyInLocalStorage(key) == 1;
 18
 19        public int GetInt(string key) =>
 020            GetInt(key, 0);
 21
 22        public int GetInt(string key, int defaultValue)
 23        {
 024            var valueGotFromLocalStorage = int.Parse(GetLocalStorageValue(key, defaultValue.ToString()));
 25
 026            if (valueGotFromLocalStorage.Equals(defaultValue) && PlayerPrefs.HasKey(key))
 27            {
 28                // We get the saved value in PlayerPrefs and move it to LocalStorage
 029                valueGotFromLocalStorage = PlayerPrefs.GetInt(key);
 030                SetInt(key, valueGotFromLocalStorage);
 31            }
 32
 033            return valueGotFromLocalStorage;
 34        }
 35
 36        public void SetInt(string key, int value)
 37        {
 038            SaveStringToLocalStorage(key, value.ToString());
 039        }
 40
 41        public bool GetBool(string key, bool defaultValue) =>
 042            GetInt(key, defaultValue ? 1 : 0) == 1;
 43
 44        public void SetBool(string key, bool value) =>
 045            SetInt(key, value ? 1 : 0);
 46
 47        public string GetString(string key, string defaultValue)
 48        {
 049            string valueGotFromLocalStorage = GetLocalStorageValue(key, string.IsNullOrEmpty(defaultValue) ? "" : defaul
 50
 051            if (!string.IsNullOrEmpty(valueGotFromLocalStorage) && valueGotFromLocalStorage.Equals(defaultValue) && Play
 52            {
 53                // We get the saved value in PlayerPrefs and move it to LocalStorage
 054                valueGotFromLocalStorage = PlayerPrefs.GetString(key);
 055                SetString(key, valueGotFromLocalStorage);
 56            }
 57
 058            return valueGotFromLocalStorage;
 59        }
 60
 61        public void SetString(string key, string value)
 62        {
 063            SaveStringToLocalStorage(key, value);
 064        }
 65
 66        public float GetFloat(string key, float defaultValue)
 67        {
 068            var valueGotFromLocalStorage = float.Parse(GetLocalStorageValue(key, defaultValue.ToString()));
 69
 070            if (valueGotFromLocalStorage.Equals(defaultValue) && PlayerPrefs.HasKey(key))
 71            {
 72                // We get the saved value in PlayerPrefs and move it to LocalStorage
 073                valueGotFromLocalStorage = PlayerPrefs.GetFloat(key);
 074                SetFloat(key, valueGotFromLocalStorage);
 75            }
 76
 077            return valueGotFromLocalStorage;
 78        }
 79
 80        public void SetFloat(string key, float value)
 81        {
 082            SaveStringToLocalStorage(key, value.ToString());
 083        }
 84
 85        private string GetLocalStorageValue(string key, string defaultValue)
 86        {
 087            if (HasKey(key))
 088                return LoadStringFromLocalStorage(key);
 89
 090            return defaultValue;
 91        }
 92
 93#if UNITY_WEBGL
 94        [DllImport("__Internal")]
 95        private static extern void SaveStringToLocalStorage(string key, string value);
 96
 97        [DllImport("__Internal")]
 98        private static extern string LoadStringFromLocalStorage(string key);
 99
 100        [DllImport("__Internal")]
 101        private static extern int HasKeyInLocalStorage(string key);
 102#else
 103    private static void SaveStringToLocalStorage(string key, string value) { }
 104
 105    private static string LoadStringFromLocalStorage(string key) =>
 106        "";
 107
 108    private static int HasKeyInLocalStorage(string key) =>
 109        0;
 110#endif
 111    }
 112}