148 lines
4.1 KiB
C#
148 lines
4.1 KiB
C#
using System.Globalization;
|
|
using System.Threading.Tasks;
|
|
using asap.core;
|
|
using Game;
|
|
using UnityEngine;
|
|
|
|
namespace game
|
|
{
|
|
public interface ISettingService
|
|
{
|
|
bool Music { get; set; }
|
|
bool Sound { get; set; }
|
|
bool Vibration { get; set; }
|
|
bool Notice { get; }
|
|
string Lang { get; set; }
|
|
int UnitOfWeight { get; set; }
|
|
}
|
|
|
|
public class DefaultSettingService : ISettingService
|
|
{
|
|
bool _music;
|
|
bool _sound;
|
|
bool _vibration;
|
|
string _lang;
|
|
int _unitOfWeight = 1;
|
|
|
|
public bool Music
|
|
{
|
|
set
|
|
{
|
|
if (value != _music)
|
|
{
|
|
PlayerPrefs.SetInt("Music", value ? 1 : 0);
|
|
_music = value;
|
|
GContext.Publish(new SoundMuteEvent(0));
|
|
}
|
|
}
|
|
get { return _music; }
|
|
}
|
|
|
|
public bool Sound
|
|
{
|
|
set
|
|
{
|
|
if (value != _sound)
|
|
{
|
|
PlayerPrefs.SetInt("Sound", value ? 1 : 0);
|
|
_sound = value;
|
|
GContext.Publish(new SoundMuteEvent(1));
|
|
}
|
|
}
|
|
get { return _sound; }
|
|
}
|
|
|
|
public bool Vibration
|
|
{
|
|
set
|
|
{
|
|
if (value != _vibration)
|
|
{
|
|
PlayerPrefs.SetInt("Vibration", value ? 1 : 0);
|
|
_vibration = value;
|
|
}
|
|
}
|
|
|
|
get { return _vibration; }
|
|
}
|
|
|
|
public bool Notice
|
|
{
|
|
get
|
|
{
|
|
return GContext.container.Resolve<ILocalNotificationService>().IsNoticeOpen;
|
|
}
|
|
}
|
|
|
|
public string Lang
|
|
{
|
|
set
|
|
{
|
|
if (value != _lang)
|
|
{
|
|
PlayerPrefs.SetString("Lang", value);
|
|
_lang = value;
|
|
}
|
|
}
|
|
get { return _lang; }
|
|
}
|
|
public int UnitOfWeight
|
|
{
|
|
set
|
|
{
|
|
if (value != _unitOfWeight)
|
|
{
|
|
PlayerPrefs.SetInt("UnitOfWeight", value);
|
|
_unitOfWeight = value;
|
|
}
|
|
}
|
|
get { return _unitOfWeight; }
|
|
}
|
|
|
|
public void Init(cfg.Tables tables)
|
|
{
|
|
if (!PlayerPrefs.HasKey("Music"))
|
|
{
|
|
var globalConfig = tables.TbGlobalConfig;
|
|
Music = globalConfig.MusicSetting;
|
|
Sound = globalConfig.SoundSetting;
|
|
Vibration = true;
|
|
CultureInfo info = System.Globalization.CultureInfo.CurrentUICulture;
|
|
string Language = info.TwoLetterISOLanguageName;
|
|
if (Language == "zh")
|
|
{
|
|
if (info.DisplayName.Contains("Simplified"))
|
|
{
|
|
Language = "zh_Hans";
|
|
}
|
|
else
|
|
{
|
|
Language = "zh_Hant";
|
|
}
|
|
}
|
|
Lang = Language;
|
|
|
|
}
|
|
else
|
|
{
|
|
_music = PlayerPrefs.GetInt("Music") == 1;
|
|
_sound = PlayerPrefs.GetInt("Sound") == 1;
|
|
_vibration = PlayerPrefs.GetInt("Vibration") == 1;
|
|
_lang = PlayerPrefs.GetString("Lang");
|
|
_unitOfWeight = PlayerPrefs.GetInt("UnitOfWeight");
|
|
}
|
|
}
|
|
|
|
private async Task<bool> AskPermission()
|
|
{
|
|
var result = new TaskCompletionSource<bool>();
|
|
var callbacks = new UnityEngine.Android.PermissionCallbacks();
|
|
callbacks.PermissionGranted += (x) => result.SetResult(true);
|
|
callbacks.PermissionDeniedAndDontAskAgain += (x) => result.SetResult(false);
|
|
callbacks.PermissionDenied += (x) => result.SetResult(false);
|
|
UnityEngine.Android.Permission.RequestUserPermission("android.permission.POST_NOTIFICATIONS", callbacks);
|
|
return await result.Task;
|
|
}
|
|
}
|
|
}
|