| | 1 | | using System; |
| | 2 | | using UnityEngine; |
| | 3 | |
|
| | 4 | | namespace DCL.SettingsCommon |
| | 5 | | { |
| | 6 | | public class PlayerPrefsAudioSettingsRepository : ISettingsRepository<AudioSettings> |
| | 7 | | { |
| | 8 | | private const string CHAT_SFX_ENABLED = "chatSFXEnabled"; |
| | 9 | | private const string MASTER_VOLUME = "masterVolume"; |
| | 10 | | private const string MUSIC_VOLUME = "musicVolume"; |
| | 11 | | private const string VOICE_CHAT_VOLUME = "voiceChatVolume"; |
| | 12 | | private const string AVATAR_SFX_VOLUME = "avatarSFXVolume"; |
| | 13 | | private const string SCENE_SFX_VOLUME = "sceneSFXVolume"; |
| | 14 | | private const string UI_SFX_VOLUME = "uiSFXVolume"; |
| | 15 | |
|
| | 16 | | private readonly IPlayerPrefsSettingsByKey settingsByKey; |
| | 17 | | private readonly AudioSettings defaultSettings; |
| | 18 | | private AudioSettings currentSettings; |
| | 19 | |
|
| | 20 | | public event Action<AudioSettings> OnChanged; |
| | 21 | |
|
| 640 | 22 | | public PlayerPrefsAudioSettingsRepository( |
| | 23 | | IPlayerPrefsSettingsByKey settingsByKey, |
| | 24 | | AudioSettings defaultSettings) |
| | 25 | | { |
| 640 | 26 | | this.settingsByKey = settingsByKey; |
| 640 | 27 | | this.defaultSettings = defaultSettings; |
| 640 | 28 | | currentSettings = Load(); |
| 640 | 29 | | } |
| | 30 | |
|
| 136 | 31 | | public AudioSettings Data => currentSettings; |
| | 32 | |
|
| | 33 | | public void Apply(AudioSettings settings) |
| | 34 | | { |
| 74 | 35 | | if (currentSettings.Equals(settings)) return; |
| 2 | 36 | | currentSettings = settings; |
| 2 | 37 | | OnChanged?.Invoke(currentSettings); |
| 2 | 38 | | } |
| | 39 | |
|
| | 40 | | public void Reset() |
| | 41 | | { |
| 25 | 42 | | Apply(defaultSettings); |
| 25 | 43 | | } |
| | 44 | |
|
| | 45 | | public void Save() |
| | 46 | | { |
| 1 | 47 | | settingsByKey.SetBool(CHAT_SFX_ENABLED, currentSettings.chatSFXEnabled); |
| 1 | 48 | | settingsByKey.SetFloat(MASTER_VOLUME, currentSettings.masterVolume); |
| 1 | 49 | | settingsByKey.SetFloat(MUSIC_VOLUME, currentSettings.musicVolume); |
| 1 | 50 | | settingsByKey.SetFloat(VOICE_CHAT_VOLUME, currentSettings.voiceChatVolume); |
| 1 | 51 | | settingsByKey.SetFloat(AVATAR_SFX_VOLUME, currentSettings.avatarSFXVolume); |
| 1 | 52 | | settingsByKey.SetFloat(SCENE_SFX_VOLUME, currentSettings.sceneSFXVolume); |
| 1 | 53 | | settingsByKey.SetFloat(UI_SFX_VOLUME, currentSettings.uiSFXVolume); |
| 1 | 54 | | } |
| | 55 | |
|
| 136 | 56 | | public bool HasAnyData() => !Data.Equals(defaultSettings); |
| | 57 | |
|
| | 58 | | private AudioSettings Load() |
| | 59 | | { |
| 640 | 60 | | var settings = defaultSettings; |
| | 61 | |
|
| | 62 | | try |
| | 63 | | { |
| 640 | 64 | | settings.chatSFXEnabled = settingsByKey.GetBool(CHAT_SFX_ENABLED, defaultSettings.chatSFXEnabled); |
| 640 | 65 | | settings.masterVolume = settingsByKey.GetFloat(MASTER_VOLUME, defaultSettings.masterVolume); |
| 640 | 66 | | settings.musicVolume = settingsByKey.GetFloat(MUSIC_VOLUME, defaultSettings.musicVolume); |
| 640 | 67 | | settings.voiceChatVolume = settingsByKey.GetFloat(VOICE_CHAT_VOLUME, defaultSettings.voiceChatVolume); |
| 640 | 68 | | settings.avatarSFXVolume = settingsByKey.GetFloat(AVATAR_SFX_VOLUME, defaultSettings.avatarSFXVolume); |
| 640 | 69 | | settings.sceneSFXVolume = settingsByKey.GetFloat(SCENE_SFX_VOLUME, defaultSettings.sceneSFXVolume); |
| 640 | 70 | | settings.uiSFXVolume = settingsByKey.GetFloat(UI_SFX_VOLUME, defaultSettings.uiSFXVolume); |
| 640 | 71 | | } |
| | 72 | | catch (Exception e) |
| | 73 | | { |
| 0 | 74 | | Debug.LogException(e); |
| 0 | 75 | | } |
| | 76 | |
|
| 640 | 77 | | return settings; |
| | 78 | | } |
| | 79 | | } |
| | 80 | | } |