Skip to content

Commit

Permalink
Upgrade: Packages
Browse files Browse the repository at this point in the history
  • Loading branch information
AmosHuKe committed Aug 6, 2024
1 parent 961f94f commit 7a1eec2
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 130 deletions.
64 changes: 25 additions & 39 deletions lib/db/preferences_db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import 'package:moodexample/themes/app_theme.dart';
/// shared_preferences
class PreferencesDB {
PreferencesDB._();
static final PreferencesDB db = PreferencesDB._();
SharedPreferences? _db;
Future<SharedPreferences> get database async =>
_db ??= await SharedPreferences.getInstance();
static final PreferencesDB instance = PreferencesDB._();
SharedPreferencesAsync? _instance;
SharedPreferencesAsync get db => _instance ??= SharedPreferencesAsync();

/*** APP相关 ***/

Expand Down Expand Up @@ -47,53 +46,46 @@ class PreferencesDB {
/*** shared_preferences ***/

/// 设置-是否填充完成【心情类别】表默认值
Future<bool> setInitMoodCategoryDefaultType(bool value) async {
final SharedPreferences prefs = await database;
return prefs.setBool(initMoodCategoryDefaultType, value);
Future<void> setInitMoodCategoryDefaultType(bool value) async {
await db.setBool(initMoodCategoryDefaultType, value);
}

/// 获取-是否填充完成【心情类别】表默认值
Future<bool> getInitMoodCategoryDefaultType() async {
final SharedPreferences prefs = await database;
return prefs.getBool(initMoodCategoryDefaultType) ?? false;
return await db.getBool(initMoodCategoryDefaultType) ?? false;
}

/// 设置-主题外观模式
Future<bool> setAppThemeDarkMode(ThemeMode themeMode) async {
final SharedPreferences prefs = await database;
return prefs.setString(appThemeDarkMode, themeMode.name);
Future<void> setAppThemeDarkMode(ThemeMode themeMode) async {
await db.setString(appThemeDarkMode, themeMode.name);
}

/// 获取-主题外观模式
Future<ThemeMode> getAppThemeDarkMode() async {
final SharedPreferences prefs = await database;
final String themeDarkMode = prefs.getString(appThemeDarkMode) ?? 'system';
final String themeDarkMode =
await db.getString(appThemeDarkMode) ?? 'system';
return darkThemeMode(themeDarkMode);
}

/// 设置-多主题模式
Future<bool> setMultipleThemesMode(String value) async {
final SharedPreferences prefs = await database;
return prefs.setString(appMultipleThemesMode, value);
Future<void> setMultipleThemesMode(String value) async {
await db.setString(appMultipleThemesMode, value);
}

/// 获取-多主题模式
Future<String> getMultipleThemesMode() async {
final SharedPreferences prefs = await database;
return prefs.getString(appMultipleThemesMode) ?? 'default';
return await db.getString(appMultipleThemesMode) ?? 'default';
}

/// 设置-APP地区语言
Future<bool> setAppLocale(Locale locale) async {
final SharedPreferences prefs = await database;
Future<void> setAppLocale(Locale locale) async {
print(locale.toLanguageTag());
return prefs.setString(appLocale, locale.toLanguageTag());
await db.setString(appLocale, locale.toLanguageTag());
}

/// 获取-APP地区语言
Future<Locale> getAppLocale() async {
final SharedPreferences prefs = await database;
final String getAppLocale = prefs.getString(appLocale) ?? 'zh';
final String getAppLocale = await db.getString(appLocale) ?? 'zh';
final appLocaleList = getAppLocale.split('-');
return Locale(
appLocaleList[0],
Expand All @@ -102,38 +94,32 @@ class PreferencesDB {
}

/// 设置-APP地区语言是否跟随系统
Future<bool> setAppIsLocaleSystem(bool isLocaleSystem) async {
final SharedPreferences prefs = await database;
return prefs.setBool(appIsLocaleSystem, isLocaleSystem);
Future<void> setAppIsLocaleSystem(bool isLocaleSystem) async {
await db.setBool(appIsLocaleSystem, isLocaleSystem);
}

/// 获取-APP地区语言是否跟随系统
Future<bool> getAppIsLocaleSystem() async {
final SharedPreferences prefs = await database;
return prefs.getBool(appIsLocaleSystem) ?? true;
return await db.getBool(appIsLocaleSystem) ?? true;
}

/// 设置-安全-密码
Future<bool> setAppKeyPassword(String keyPassword) async {
final SharedPreferences prefs = await database;
return prefs.setString(appKeyPassword, keyPassword);
Future<void> setAppKeyPassword(String keyPassword) async {
await db.setString(appKeyPassword, keyPassword);
}

/// 获取-安全-密码
Future<String> getAppKeyPassword() async {
final SharedPreferences prefs = await database;
return prefs.getString(appKeyPassword) ?? '';
return await db.getString(appKeyPassword) ?? '';
}

/// 设置-安全-生物特征识别是否开启
Future<bool> setAppKeyBiometric(bool keyBiometric) async {
final SharedPreferences prefs = await database;
return prefs.setBool(appKeyBiometric, keyBiometric);
Future<void> setAppKeyBiometric(bool keyBiometric) async {
await db.setBool(appKeyBiometric, keyBiometric);
}

/// 获取-安全-生物特征识别是否开启
Future<bool> getAppKeyBiometric() async {
final SharedPreferences prefs = await database;
return prefs.getBool(appKeyBiometric) ?? false;
return await db.getBool(appKeyBiometric) ?? false;
}
}
24 changes: 12 additions & 12 deletions lib/providers/application/application_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,79 +27,79 @@ class ApplicationProvider extends ChangeNotifier {

/// 获取-主题模式
Future<void> loadThemeMode() async {
_themeMode = await PreferencesDB.db.getAppThemeDarkMode();
_themeMode = await PreferencesDB.instance.getAppThemeDarkMode();
notifyListeners();
}

/// 设置-主题模式
set themeMode(ThemeMode themeMode) {
PreferencesDB.db.setAppThemeDarkMode(themeMode);
PreferencesDB.instance.setAppThemeDarkMode(themeMode);
_themeMode = themeMode;
notifyListeners();
}

/// 获取-多主题模式
Future<void> loadMultipleThemesMode() async {
_multipleThemesMode = await PreferencesDB.db.getMultipleThemesMode();
_multipleThemesMode = await PreferencesDB.instance.getMultipleThemesMode();
notifyListeners();
}

/// 设置-多主题模式
set multipleThemesMode(String multipleThemesMode) {
PreferencesDB.db.setMultipleThemesMode(multipleThemesMode);
PreferencesDB.instance.setMultipleThemesMode(multipleThemesMode);
_multipleThemesMode = multipleThemesMode;
notifyListeners();
}

/// 获取-语言是否跟随系统
Future<void> loadLocaleSystem() async {
_localeSystem = await PreferencesDB.db.getAppIsLocaleSystem();
_localeSystem = await PreferencesDB.instance.getAppIsLocaleSystem();
notifyListeners();
}

/// 设置-语言是否跟随系统
set localeSystem(bool localeSystem) {
PreferencesDB.db.setAppIsLocaleSystem(localeSystem);
PreferencesDB.instance.setAppIsLocaleSystem(localeSystem);
_localeSystem = localeSystem;
notifyListeners();
}

/// 获取-语言
Future<void> loadLocale() async {
_locale = await PreferencesDB.db.getAppLocale();
_locale = await PreferencesDB.instance.getAppLocale();
notifyListeners();
}

/// 设置-语言
set locale(Locale locale) {
localeSystem = false;
PreferencesDB.db.setAppLocale(locale);
PreferencesDB.instance.setAppLocale(locale);
_locale = locale;
notifyListeners();
}

/// 获取-安全-密码内容
Future<void> loadKeyPassword() async {
_keyPassword = await PreferencesDB.db.getAppKeyPassword();
_keyPassword = await PreferencesDB.instance.getAppKeyPassword();
notifyListeners();
}

/// 设置-安全-密码内容
set keyPassword(String keyPassword) {
PreferencesDB.db.setAppKeyPassword(keyPassword);
PreferencesDB.instance.setAppKeyPassword(keyPassword);
_keyPassword = keyPassword;
notifyListeners();
}

/// 获取-安全-生物特征识别是否开启
Future<void> loadKeyBiometric() async {
_keyBiometric = await PreferencesDB.db.getAppKeyBiometric();
_keyBiometric = await PreferencesDB.instance.getAppKeyBiometric();
notifyListeners();
}

/// 设置-安全-生物特征识别是否开启
set keyBiometric(bool keyBiometric) {
PreferencesDB.db.setAppKeyBiometric(keyBiometric);
PreferencesDB.instance.setAppKeyBiometric(keyBiometric);
_keyBiometric = keyBiometric;
notifyListeners();
}
Expand Down
4 changes: 2 additions & 2 deletions lib/providers/mood/mood_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ class MoodProvider extends ChangeNotifier {
/// 设置心情类别默认值
static Future<bool> setMoodCategoryDefault() async {
final bool initMoodCategoryDefaultType =
await PreferencesDB.db.getInitMoodCategoryDefaultType();
await PreferencesDB.instance.getInitMoodCategoryDefaultType();
print('心情类别默认值初始化:$initMoodCategoryDefaultType');
if (!initMoodCategoryDefaultType) {
print('开始心情类别默认值初始化');
MoodService.setCategoryDefault();

/// 已赋值默认值标记
await PreferencesDB.db.setInitMoodCategoryDefaultType(true);
await PreferencesDB.instance.setInitMoodCategoryDefaultType(true);
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,19 @@ class HumanPlayer extends SimplePlayer
super.onJoystickChangeDirectional(event);
}

/// 处理攻击
@override
bool handleAttack(AttackOriginEnum attacker, double damage, identify) {
return super.handleAttack(attacker, damage, identify);
}

/// 受伤触发
@override
bool receiveDamage(AttackOriginEnum attacker, double damage, dynamic from) {
void onReceiveDamage(AttackOriginEnum attacker, double damage, dynamic from) {
if (!isDead) {
super.onReceiveDamage(attacker, damage, from);

/// 伤害显示
showDamage(
damage,
initVelocityVertical: -2,
Expand All @@ -138,7 +147,6 @@ class HumanPlayer extends SimplePlayer
// gameRef.lighting?.animateToColor(Colors.black.withOpacity(0.7));
// });
}
return super.receiveDamage(attacker, damage, from);
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,18 @@ class Orc extends SimpleEnemy
super.onDie();
}

/// 处理攻击
@override
bool handleAttack(AttackOriginEnum attacker, double damage, identify) {
return super.handleAttack(attacker, damage, identify);
}

/// 受伤触发
@override
bool receiveDamage(AttackOriginEnum attacker, double damage, identify) {
void onReceiveDamage(AttackOriginEnum attacker, double damage, identify) {
if (!isDead) {
super.onReceiveDamage(attacker, damage, identify);

/// 伤害显示
showDamage(
damage,
Expand All @@ -132,7 +140,6 @@ class Orc extends SimpleEnemy
/// 受伤动画
// _addDamageAnimation();
}
return super.receiveDamage(attacker, damage, identify);
}

/// 攻击动画
Expand Down
4 changes: 3 additions & 1 deletion lib/views/settings/laboratory/game/mini_fantasy/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ class Game extends StatelessWidget {
showCollisionArea: false,
playerControllers: [
Joystick(
directional: JoystickDirectional(),
directional: JoystickDirectional(
margin: const EdgeInsets.all(65),
),
actions: [
JoystickAction(
actionId: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,25 @@ class Boss extends SimpleEnemy
super.onDie();
}

/// 处理攻击
@override
bool handleAttack(AttackOriginEnum attacker, double damage, identify) {
return super.handleAttack(attacker, damage, identify);
}

/// 受伤触发
@override
bool receiveDamage(AttackOriginEnum attacker, double damage, identify) {
void onReceiveDamage(AttackOriginEnum attacker, double damage, identify) {
if (!isDead) {
super.onReceiveDamage(attacker, damage, identify);

/// 伤害显示
showDamage(
damage,
initVelocityVertical: -2,
config: TextStyle(color: Colors.amberAccent, fontSize: tileSize / 2),
);
}
return super.receiveDamage(attacker, damage, identify);
}

/// 攻击
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,34 @@ class HumanPlayer extends SimplePlayer
super.onJoystickChangeDirectional(event);
}

/// 处理攻击
@override
bool handleAttack(AttackOriginEnum attacker, double damage, identify) {
return super.handleAttack(attacker, damage, identify);
}

/// 受伤触发
@override
bool receiveDamage(AttackOriginEnum attacker, double damage, dynamic from) {
handleReceiveDamage(damage);
return super.receiveDamage(attacker, damage, from);
void onReceiveDamage(AttackOriginEnum attacker, double damage, dynamic from) {
if (!isDead) {
super.onReceiveDamage(attacker, damage, from);

/// 伤害显示
showDamage(
damage,
initVelocityVertical: -2,
config: TextStyle(color: Colors.white, fontSize: tileSize / 2),
);
// lockMove = true;
/// 屏幕变红
// gameRef.lighting
// ?.animateToColor(const Color.fromARGB(255, 26, 0, 0).withOpacity(0.7));
// idle();
// addDamageAnimation(() {
// lockMove = false;
// gameRef.lighting?.animateToColor(Colors.black.withOpacity(0.7));
// });
}
}

/// 死亡
Expand All @@ -204,24 +227,6 @@ class HumanPlayer extends SimplePlayer
super.onDie();
}

/// 受伤触发
void handleReceiveDamage(double damage) {
showDamage(
damage,
initVelocityVertical: -2,
config: TextStyle(color: Colors.white, fontSize: tileSize / 2),
);
// lockMove = true;
/// 屏幕变红
// gameRef.lighting
// ?.animateToColor(const Color.fromARGB(255, 26, 0, 0).withOpacity(0.7));
// idle();
// addDamageAnimation(() {
// lockMove = false;
// gameRef.lighting?.animateToColor(Colors.black.withOpacity(0.7));
// });
}

/// 第一次游玩对话
void firstPlayerSay() {
if (!firstPlayer) {
Expand Down
Loading

0 comments on commit 7a1eec2

Please sign in to comment.