< 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:35
Coverable lines:35
Total lines:116
Line coverage:0% (0 of 35)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:15
Method coverage:0% (0 of 15)

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%
SaveStringToLocalStorage(...)0%2100%
LoadStringFromLocalStorage(...)0%2100%
HasKeyInLocalStorage(...)0%2100%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Runtime.InteropServices;
 3using UnityEngine;
 4
 5namespace DCL.Helpers
 6{
 7    /// <summary>
 8    ///     Provider class to use LocalStorage instead of IndexedDB because it was not reliably saving settings.
 9    ///     For all the gets, we have to check that the PlayerPref key does exist. We may lose information that was
 10    ///     succesfully stored in IndexedDB. That check should only go once per user per key.
 11    /// </summary>
 12    public class PlayerPrefsProviderLocalStorage : IPlayerPrefsProvider
 13    {
 14        //Nothing to save when using local storage
 015        public void Save() { }
 16
 17        public bool HasKey(string key) =>
 018            HasKeyInLocalStorage(key) == 1;
 19
 20        public int GetInt(string key) =>
 021            GetInt(key, 0);
 22
 23        public int GetInt(string key, int defaultValue)
 24        {
 025            var valueGotFromLocalStorage = int.Parse(GetLocalStorageValue(key, defaultValue.ToString()));
 26
 027            if (valueGotFromLocalStorage.Equals(defaultValue) && PlayerPrefs.HasKey(key))
 28            {
 29                // We get the saved value in PlayerPrefs and move it to LocalStorage
 030                valueGotFromLocalStorage = PlayerPrefs.GetInt(key);
 031                SetInt(key, valueGotFromLocalStorage);
 032                PlayerPrefs.DeleteKey(key);
 33            }
 34
 035            return valueGotFromLocalStorage;
 36        }
 37
 38        public void SetInt(string key, int value)
 39        {
 040            SaveStringToLocalStorage(key, value.ToString());
 041        }
 42
 43        public bool GetBool(string key, bool defaultValue) =>
 044             GetInt(key, defaultValue ? 1 : 0) == 1;
 45
 46        public void SetBool(string key, bool value) =>
 047            SetInt(key, value ? 1 : 0);
 48
 49        public string GetString(string key, string defaultValue)
 50        {
 051            string valueGotFromLocalStorage = GetLocalStorageValue(key, string.IsNullOrEmpty(defaultValue) ? "" : defaul
 52
 053            if (!string.IsNullOrEmpty(valueGotFromLocalStorage) && valueGotFromLocalStorage.Equals(defaultValue) && Play
 54            {
 55                // We get the saved value in PlayerPrefs and move it to LocalStorage
 056                valueGotFromLocalStorage = PlayerPrefs.GetString(key);
 057                SetString(key, valueGotFromLocalStorage);
 058                PlayerPrefs.DeleteKey(key);
 59            }
 60
 061            return valueGotFromLocalStorage;
 62        }
 63
 64        public void SetString(string key, string value)
 65        {
 066            SaveStringToLocalStorage(key, value);
 067        }
 68
 69        public float GetFloat(string key, float defaultValue)
 70        {
 071            var valueGotFromLocalStorage = float.Parse(GetLocalStorageValue(key, defaultValue.ToString()));
 72
 073            if (valueGotFromLocalStorage.Equals(defaultValue) && PlayerPrefs.HasKey(key))
 74            {
 75                // We get the saved value in PlayerPrefs and move it to LocalStorage
 076                valueGotFromLocalStorage = PlayerPrefs.GetFloat(key);
 077                SetFloat(key, valueGotFromLocalStorage);
 078                PlayerPrefs.DeleteKey(key);
 79            }
 80
 081            return valueGotFromLocalStorage;
 82        }
 83
 84        public void SetFloat(string key, float value)
 85        {
 086            SaveStringToLocalStorage(key, value.ToString());
 087        }
 88
 89        private string GetLocalStorageValue(string key, string defaultValue)
 90        {
 091            if (HasKey(key))
 092                return LoadStringFromLocalStorage(key);
 93
 094            return defaultValue;
 95        }
 96
 97#if UNITY_WEBGL
 98        [DllImport("__Internal")]
 99        private static extern void SaveStringToLocalStorage(string key, string value);
 100
 101        [DllImport("__Internal")]
 102        private static extern string LoadStringFromLocalStorage(string key);
 103
 104        [DllImport("__Internal")]
 105        private static extern int HasKeyInLocalStorage(string key);
 106#else
 0107    private static void SaveStringToLocalStorage(string key, string value) { }
 108
 109    private static string LoadStringFromLocalStorage(string key) =>
 0110        "";
 111
 112    private static int HasKeyInLocalStorage(string key) =>
 0113        0;
 114#endif
 115    }
 116}