Skip to content

Commit

Permalink
Refactor: Update variable name
Browse files Browse the repository at this point in the history
  • Loading branch information
AmosHuKe committed Aug 6, 2024
1 parent 7a1eec2 commit c6883b4
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 60 deletions.
20 changes: 9 additions & 11 deletions lib/application.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ import 'package:flutter_zoom_drawer/flutter_zoom_drawer.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';

import 'package:moodexample/themes/app_theme.dart';
import 'package:moodexample/routes.dart';
import 'package:moodexample/home_screen.dart';
import 'package:moodexample/l10n/gen/app_localizations.dart';
import 'init.dart';
import 'routes.dart';
import 'home_screen.dart';
import 'themes/app_theme.dart';
import 'l10n/gen/app_localizations.dart';

import 'package:moodexample/widgets/will_pop_scope_route/will_pop_scope_route.dart';
import 'widgets/will_pop_scope_route/will_pop_scope_route.dart';

import 'package:moodexample/views/menu_screen/menu_screen_left.dart';
import 'views/menu_screen/menu_screen_left.dart';

import 'package:moodexample/providers/mood/mood_provider.dart';
import 'package:moodexample/providers/statistic/statistic_provider.dart';
import 'package:moodexample/providers/application/application_provider.dart';
import 'providers/mood/mood_provider.dart';
import 'providers/statistic/statistic_provider.dart';
import 'providers/application/application_provider.dart';

class Application extends StatelessWidget {
const Application({super.key});
Expand Down Expand Up @@ -59,7 +59,6 @@ class Application extends StatelessWidget {
.multipleThemesLightMode(),
darkTheme: AppTheme(getMultipleThemesMode(context))
.multipleThemesDarkMode(),
// 路由钩子
onGenerateRoute: router.generator,
// 国际化
supportedLocales: S.supportedLocales,
Expand All @@ -75,7 +74,6 @@ class Application extends StatelessWidget {
title: 'Mood',
navigatorObservers: [FlutterSmartDialog.observer],
builder: FlutterSmartDialog.init(),
// Home
home: const WillPopScopeRoute(
child: Init(
child: MenuPage(key: Key('widget_menu_page')),
Expand Down
8 changes: 4 additions & 4 deletions lib/db/db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import 'package:moodexample/db/database/table_mood_info_category.dart';

class DB {
DB._();
static final DB db = DB._();
Database? _db;
Future<Database> get database async => _db ??= await createDatabase();
static final DB instance = DB._();
Database? _instance;
Future<Database> get database async => _instance ??= await createDatabase();

/// 数据库版本号
static const int _version = 1;
Expand All @@ -32,7 +32,7 @@ class DB {
return db;
}

Future close() async => _db?.close();
Future close() async => _instance?.close();

/// 创建
Future<void> _onCreate(Database db, int newVersion) async {
Expand Down
30 changes: 15 additions & 15 deletions lib/db/preferences_db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class PreferencesDB {
PreferencesDB._();
static final PreferencesDB instance = PreferencesDB._();
SharedPreferencesAsync? _instance;
SharedPreferencesAsync get db => _instance ??= SharedPreferencesAsync();
SharedPreferencesAsync get sps => _instance ??= SharedPreferencesAsync();

/*** APP相关 ***/

Expand Down Expand Up @@ -47,45 +47,45 @@ class PreferencesDB {

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

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

/// 设置-主题外观模式
Future<void> setAppThemeDarkMode(ThemeMode themeMode) async {
await db.setString(appThemeDarkMode, themeMode.name);
await sps.setString(appThemeDarkMode, themeMode.name);
}

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

/// 设置-多主题模式
Future<void> setMultipleThemesMode(String value) async {
await db.setString(appMultipleThemesMode, value);
await sps.setString(appMultipleThemesMode, value);
}

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

/// 设置-APP地区语言
Future<void> setAppLocale(Locale locale) async {
print(locale.toLanguageTag());
await db.setString(appLocale, locale.toLanguageTag());
await sps.setString(appLocale, locale.toLanguageTag());
}

/// 获取-APP地区语言
Future<Locale> getAppLocale() async {
final String getAppLocale = await db.getString(appLocale) ?? 'zh';
final String getAppLocale = await sps.getString(appLocale) ?? 'zh';
final appLocaleList = getAppLocale.split('-');
return Locale(
appLocaleList[0],
Expand All @@ -95,31 +95,31 @@ class PreferencesDB {

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

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

/// 设置-安全-密码
Future<void> setAppKeyPassword(String keyPassword) async {
await db.setString(appKeyPassword, keyPassword);
await sps.setString(appKeyPassword, keyPassword);
}

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

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

/// 获取-安全-生物特征识别是否开启
Future<bool> getAppKeyBiometric() async {
return await db.getBool(appKeyBiometric) ?? false;
return await sps.getBool(appKeyBiometric) ?? false;
}
}
12 changes: 6 additions & 6 deletions lib/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_zoom_drawer/flutter_zoom_drawer.dart';
import 'package:remixicon/remixicon.dart';

import 'package:moodexample/themes/app_theme.dart';
import 'package:moodexample/views/statistic/index.dart' as statistic;
import 'package:moodexample/l10n/gen/app_localizations.dart';
import 'themes/app_theme.dart';
import 'l10n/gen/app_localizations.dart';
import 'views/statistic/index.dart' as statistic;

import 'package:moodexample/views/home/index.dart';
import 'package:moodexample/views/mood/index.dart';
import 'package:moodexample/views/statistic/index.dart';
import 'views/home/index.dart';
import 'views/mood/index.dart';
import 'views/statistic/index.dart';

/// 首页底部Tabbar
class HomeScreen extends StatefulWidget {
Expand Down
16 changes: 8 additions & 8 deletions lib/init.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import 'package:flutter/material.dart';
import 'package:awesome_notifications/awesome_notifications.dart';
import 'package:provider/provider.dart';

import 'package:moodexample/themes/app_theme.dart';
import 'package:moodexample/common/notification.dart';
import 'package:moodexample/l10n/gen/app_localizations.dart';
import 'package:moodexample/db/db.dart';
import 'themes/app_theme.dart';
import 'common/notification.dart';
import 'l10n/gen/app_localizations.dart';
import 'db/db.dart';

import 'package:moodexample/widgets/lock_screen/lock_screen.dart';
import 'widgets/lock_screen/lock_screen.dart';

import 'package:moodexample/providers/application/application_provider.dart';
import 'package:moodexample/providers/mood/mood_provider.dart';
import 'providers/application/application_provider.dart';
import 'providers/mood/mood_provider.dart';

class Init extends StatefulWidget {
const Init({super.key, required this.child});
Expand Down Expand Up @@ -87,7 +87,7 @@ class _InitState extends State<Init> {
context.read<ApplicationProvider>();

// 初始化数据库
await DB.db.database;
await DB.instance.database;
// 锁屏
runLockScreen();
// 获取所有心情类别
Expand Down
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/services.dart';
import 'package:flutter/material.dart';

import 'package:moodexample/common/notification.dart';
import 'common/notification.dart';

import 'application.dart';

Expand Down
4 changes: 2 additions & 2 deletions lib/routes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import 'package:flutter/material.dart';

import 'package:fluro/fluro.dart';

import 'package:moodexample/widgets/will_pop_scope_route/will_pop_scope_route.dart';
import 'widgets/will_pop_scope_route/will_pop_scope_route.dart';

import 'package:moodexample/models/mood/mood_model.dart';
import 'models/mood/mood_model.dart';

import 'views/mood/index.dart';
import 'views/home/index.dart';
Expand Down
16 changes: 8 additions & 8 deletions lib/services/mood/mood_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ class MoodService {
];

for (final value in moodCategoryData) {
DB.db.insertMoodCategoryDefault(value);
DB.instance.insertMoodCategoryDefault(value);
}
}

/// 获取所有心情类别
static Future<List<MoodCategoryData>> getMoodCategoryAll() async {
final moodCategoryData = await DB.db.selectMoodCategoryAll();
final moodCategoryData = await DB.instance.selectMoodCategoryAll();
final List<MoodCategoryData> moodCategoryDataList = [];
// 转换模型
for (final value in moodCategoryData) {
Expand All @@ -40,14 +40,14 @@ class MoodService {
/// 添加心情详情数据
static Future<bool> addMoodData(MoodData moodData) async {
// 添加数据
final bool result = await DB.db.insertMood(moodData);
final bool result = await DB.instance.insertMood(moodData);
return result;
}

/// 根据日期获取详情数据
static Future<List<MoodData>> getMoodData(String datetime) async {
// 查询心情数据
final moodData = await DB.db.selectMood(datetime);
final moodData = await DB.instance.selectMood(datetime);
final List<MoodData> MoodDataList = [];
for (final value in moodData) {
// 转换模型
Expand All @@ -59,7 +59,7 @@ class MoodService {
/// 获取所有已记录心情的日期
static Future<List<MoodRecordData>> getMoodRecordDate() async {
// 查询
final list = await DB.db.selectMoodRecordDate();
final list = await DB.instance.selectMoodRecordDate();
late final List<MoodRecordData> dataList = [];
for (final value in list) {
// 转换模型
Expand All @@ -71,21 +71,21 @@ class MoodService {
/// 修改心情详细数据
static Future<bool> editMood(MoodData moodData) async {
// 修改数据
final bool result = await DB.db.updateMood(moodData);
final bool result = await DB.instance.updateMood(moodData);
return result;
}

/// 删除心情详细数据
static Future<bool> delMood(MoodData moodData) async {
// 删除数据
final bool result = await DB.db.deleteMood(moodData);
final bool result = await DB.instance.deleteMood(moodData);
return result;
}

/// 获取所有心情详情数据
static Future<List<MoodData>> getMoodAllData() async {
// 查询心情数据
final moodData = await DB.db.selectAllMood();
final moodData = await DB.instance.selectAllMood();
final List<MoodData> moodDataList = [];
for (final value in moodData) {
// 转换模型
Expand Down
10 changes: 5 additions & 5 deletions lib/services/statistic/statistic_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ import 'package:moodexample/models/statistic/statistic_model.dart';
class StatisticService {
/// 获取APP累计记录天数
static Future<int> getAPPUsageDays() async {
final list = await DB.db.selectAPPUsageDays();
final list = await DB.instance.selectAPPUsageDays();
print('获取APP使用天数$list');
return list[0]['dayCount'] ?? 0;
}

/// 获取APP累计记录条数
static Future<int> getAPPMoodCount() async {
final list = await DB.db.selectAPPMoodCount();
final list = await DB.instance.selectAPPMoodCount();
print('APP累计记录条数$list');
return list[0]['moodCount'] ?? 0;
}

/// 获取平均情绪波动
static Future<int> getMoodScoreAverage() async {
final list = await DB.db.selectMoodScoreAverage();
final list = await DB.instance.selectMoodScoreAverage();
print('平均情绪波动$list');
return list[0]['moodScoreAverage'] ?? 0;
}
Expand All @@ -43,7 +43,7 @@ class StatisticService {
final String date =
DateFormat('yyyy-MM-dd').format(nowDate.subtract(Duration(days: i)));
// 查询
final list = await DB.db.selectDateMoodScoreAverage(date);
final list = await DB.instance.selectDateMoodScoreAverage(date);
final int count = list[0]['moodScoreAverage'] ?? 0;
dataList.add(
statisticMoodScoreAverageRecentlyDataFromJson(
Expand All @@ -69,7 +69,7 @@ class StatisticService {
final String endTime =
"${DateFormat("yyyy-MM-dd").format(nowDate)} 23:59:59";
// 查询
final list = await DB.db.selectDateMoodCount(startTime, endTime);
final list = await DB.instance.selectDateMoodCount(startTime, endTime);
for (final value in list) {
dataList.add(statisticDateMoodCountDataFromJson(json.encode(value)));
}
Expand Down

0 comments on commit c6883b4

Please sign in to comment.