Skip to content

Commit

Permalink
Update: Initial load status
Browse files Browse the repository at this point in the history
  • Loading branch information
AmosHuKe committed Aug 15, 2024
1 parent 7150b35 commit 8314eee
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 109 deletions.
9 changes: 7 additions & 2 deletions lib/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import 'package:flutter/material.dart';

import 'package:flutter_zoom_drawer/flutter_zoom_drawer.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';
import 'package:remixicon/remixicon.dart';

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

import 'providers/statistic/statistic_provider.dart';

import 'views/menu_screen/menu_screen_left.dart';

Expand Down Expand Up @@ -71,6 +73,9 @@ class _HomeScreenState extends State<HomeScreen> with TickerProviderStateMixin {
/// Tab icon大小
final double _tabIconSize = 20;

final StatisticProvider statisticProvider =
context.read<StatisticProvider>();

/// 当前页下标
_currentIndex = widget.navigationShell.currentIndex;

Expand Down Expand Up @@ -141,7 +146,7 @@ class _HomeScreenState extends State<HomeScreen> with TickerProviderStateMixin {
switch (index) {
case 2:
// 统计菜单触发
statistic.init(context);
statisticProvider.load();
}
widget.navigationShell.goBranch(index);
},
Expand Down
22 changes: 1 addition & 21 deletions lib/init.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'package:awesome_notifications/awesome_notifications.dart';
import 'package:provider/provider.dart';

import 'themes/app_theme.dart';
import 'common/notification.dart';
Expand All @@ -12,9 +11,6 @@ import 'db/db.dart';

import 'widgets/lock_screen/lock_screen.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 @@ -46,9 +42,7 @@ class _InitState extends State<Init> {
);

/// 初始化
WidgetsBinding.instance.endOfFrame.then((_) {
if (mounted) init();
});
init();
}

@override
Expand Down Expand Up @@ -82,24 +76,10 @@ class _InitState extends State<Init> {

/// 应用初始化
Future<void> init() async {
final MoodProvider moodProvider = context.read<MoodProvider>();
final ApplicationProvider applicationProvider =
context.read<ApplicationProvider>();

// 初始化数据库
await DB.instance.database;
// 锁屏
runLockScreen();
// 获取所有心情类别
moodProvider.loadMoodCategoryAllList();
// 触发获取APP主题深色模式
applicationProvider.loadThemeMode();
// 触发获取APP多主题模式
applicationProvider.loadMultipleThemesMode();
// 触发获取APP地区语言
applicationProvider.loadLocale();
// 触发获取APP地区语言是否跟随系统
applicationProvider.loadLocaleSystem();
// 通知权限判断显示
allowedNotification();

Expand Down
9 changes: 9 additions & 0 deletions lib/providers/application/application_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ import 'package:moodexample/db/preferences_db.dart';

/// App相关
class ApplicationProvider extends ChangeNotifier {
ApplicationProvider() {
loadThemeMode();
loadMultipleThemesMode();
loadLocaleSystem();
loadLocale();
loadKeyPassword();
loadKeyBiometric();
}

/// 主题模式
ThemeMode _themeMode = ThemeMode.system;

Expand Down
21 changes: 16 additions & 5 deletions lib/providers/mood/mood_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import 'package:moodexample/services/mood/mood_service.dart';

// 心情页相关
class MoodProvider extends ChangeNotifier {
MoodProvider() {
load();
}

/// 心情数据List
List<MoodData> _moodDataList = [];

Expand All @@ -26,8 +30,14 @@ class MoodProvider extends ChangeNotifier {
/// 所有心情数据List
List<MoodData>? _moodAllDataList = [];

Future<void> load() async {
await loadMoodCategoryAllList();
await loadMoodRecordDateAllList();
await loadMoodDataList();
}

/// 设置心情类别默认值
static Future<bool> setMoodCategoryDefault() async {
Future<bool> _setMoodCategoryDefault() async {
final bool initMoodCategoryDefaultType =
await PreferencesDB.instance.getInitMoodCategoryDefaultType();
print('心情类别默认值初始化:$initMoodCategoryDefaultType');
Expand All @@ -44,19 +54,20 @@ class MoodProvider extends ChangeNotifier {
/// 获取所有心情类别数据列表
Future<void> loadMoodCategoryAllList() async {
/// 设置心情类别默认值
final bool setMoodCategoryDefaultresult =
await MoodProvider.setMoodCategoryDefault();
final bool setMoodCategoryDefaultresult = await _setMoodCategoryDefault();
if (setMoodCategoryDefaultresult) {
/// 获取所有心情类别
moodCategoryList = await MoodService.getMoodCategoryAll();
}
}

/// 根据日期获取详细数据列表
Future<void> loadMoodDataList(String datetime) async {
Future<void> loadMoodDataList() async {
_moodDataLoading = true;
notifyListeners();
moodDataList = await MoodService.getMoodData(datetime);
moodDataList = await MoodService.getMoodData(
_nowDateTime.toString().substring(0, 10),
);
}

/// 所有心情详细数据列表
Expand Down
20 changes: 16 additions & 4 deletions lib/providers/statistic/statistic_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import 'package:moodexample/services/statistic/statistic_service.dart';

/// 统计相关
class StatisticProvider extends ChangeNotifier {
StatisticProvider() {
load();
}

/// 统计的天数
int _moodDays = 7;

Expand All @@ -23,6 +27,14 @@ class StatisticProvider extends ChangeNotifier {
/// 近日心情数量统计
List<StatisticDateMoodCountData> _dateMoodCount = [];

Future<void> load() async {
await loadDaysCount();
await loadMoodCount();
await loadMoodScoreAverage();
await loadMoodScoreAverageRecently();
await loadDateMoodCount();
}

/// 获取APP累计使用天数
Future<void> loadDaysCount() async {
daysCount = await StatisticService.getAPPUsageDays();
Expand All @@ -39,14 +51,14 @@ class StatisticProvider extends ChangeNotifier {
}

/// 获取近日情绪波动
Future<void> loadMoodScoreAverageRecently({int days = 7}) async {
Future<void> loadMoodScoreAverageRecently() async {
moodScoreAverageRecently =
await StatisticService.getMoodScoreAverageRecently(days: days);
await StatisticService.getMoodScoreAverageRecently(days: _moodDays);
}

/// 获取近日心情数量统计
Future<void> loadDateMoodCount({int days = 7}) async {
dateMoodCount = await StatisticService.getDateMoodCount(days: days);
Future<void> loadDateMoodCount() async {
dateMoodCount = await StatisticService.getDateMoodCount(days: _moodDays);
}

/// 赋值统计的天数
Expand Down
4 changes: 0 additions & 4 deletions lib/views/home/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,6 @@ class _OptionMoodState extends State<OptionMood> {
@override
void initState() {
super.initState();
final MoodProvider moodProvider = context.read<MoodProvider>();

/// 获取所有心情类别
moodProvider.loadMoodCategoryAllList();
}

@override
Expand Down
13 changes: 1 addition & 12 deletions lib/views/menu_screen/widgets/setting_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -179,18 +179,7 @@ class _ImportDatabaseBodyState extends State<ImportDatabaseBody> {
/// 更新心情数据
final MoodProvider moodProvider =
context.read<MoodProvider>();

/// 获取所有有记录心情的日期
moodProvider.loadMoodRecordDateAllList();

/// 处理日期
final String moodDatetime = moodProvider
.nowDateTime
.toString()
.substring(0, 10);

/// 获取心情数据
moodProvider.loadMoodDataList(moodDatetime);
moodProvider.load();
}
} catch (e) {
print('$e');
Expand Down
34 changes: 7 additions & 27 deletions lib/views/mood/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ class _MoodPageState extends State<MoodPage> {
@override
void initState() {
super.initState();

WidgetsBinding.instance.endOfFrame.then((_) {
if (mounted) init(context);
});
}

@override
Expand Down Expand Up @@ -107,21 +103,6 @@ class _MoodPageState extends State<MoodPage> {
}
}

/// 初始化
void init(BuildContext context) {
final MoodProvider moodProvider = context.read<MoodProvider>();

/// 获取所有记录心情的日期
moodProvider.loadMoodRecordDateAllList();

/// 处理日期
final String moodDatetime =
moodProvider.nowDateTime.toString().substring(0, 10);

/// 获取心情数据
moodProvider.loadMoodDataList(moodDatetime);
}

/// 主体
class MoodBody extends StatelessWidget {
const MoodBody({super.key});
Expand Down Expand Up @@ -168,7 +149,8 @@ class MoodBody extends StatelessWidget {
/// 下拉加载
CupertinoSliverRefreshControl(
onRefresh: () async {
init(context);
final MoodProvider moodProvider = context.read<MoodProvider>();
moodProvider.load();
},
),

Expand Down Expand Up @@ -402,15 +384,11 @@ class _CalendarState extends State<Calendar> {
/// 赋值当前选择的日期
moodProvider.nowDateTime = selectedDay;

/// 处理赋值新日期
final String moodDatetime =
selectedDay.toString().substring(0, 10);

/// 开启加载
moodProvider.moodDataLoading = true;

/// 获取心情数据
moodProvider.loadMoodDataList(moodDatetime);
moodProvider.loadMoodDataList();
},
onCalendarCreated: (pageController) {
/// 初始化触发一次
Expand Down Expand Up @@ -631,8 +609,10 @@ class MoodCard extends StatelessWidget {
final bool result =
await moodProvider.deleteMoodData(moodData);
if (result) {
// 重新初始化
init(context);
/// 重新初始化
final MoodProvider moodProvider =
context.read<MoodProvider>();
moodProvider.load();
context.pop();
}
},
Expand Down
3 changes: 0 additions & 3 deletions lib/views/mood/mood_category_select.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ class _MoodCategorySelectState extends State<MoodCategorySelect> {

/// 当前选择的时间
_nowDateTime = moodProvider.nowDateTime.toString().substring(0, 10);

/// 获取所有心情类别
moodProvider.loadMoodCategoryAllList();
}

@override
Expand Down
4 changes: 1 addition & 3 deletions lib/views/mood/mood_content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ class _MoodContentState extends State<MoodContent> {

/// 是否操作成功
late bool result = false;
final String nowDateTime =
moodProvider.nowDateTime.toString().substring(0, 10);

/// 存在ID的操作(代表修改)
if (_moodData.moodId != null) {
Expand All @@ -121,7 +119,7 @@ class _MoodContentState extends State<MoodContent> {
}
if (result) {
/// 获取心情数据
moodProvider.loadMoodDataList(nowDateTime);
moodProvider.loadMoodDataList();

/// 获取所有已记录心情的日期
moodProvider.loadMoodRecordDateAllList();
Expand Down
Loading

0 comments on commit 8314eee

Please sign in to comment.