Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Providers and ViewModels #156

Merged
merged 11 commits into from
Apr 4, 2024
4 changes: 2 additions & 2 deletions lib/common/app_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/services.dart';
import 'package:package_info_plus/package_info_plus.dart';

import 'abc.dart';
import 'async.dart';

class AppInfo implements FutureInitializationABC {
class AppInfo implements AsyncInitialization {
static final AppInfo _singleton = AppInfo._internal();

AndroidDeviceInfo? _androidDeviceInfo;
Expand Down
2 changes: 1 addition & 1 deletion lib/common/abc.dart → lib/common/async.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

abstract mixin class FutureInitializationABC {
abstract interface class AsyncInitialization {
Future init();
}
5 changes: 0 additions & 5 deletions lib/common/consts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,3 @@ const skipReasonChipTextList = [
'😠',
];
//#endregion

//#region assets
const emptyHabitsImagePath = "assets/images/empty-habits.svg.template";
const notFoundImagePath = "assets/images/not-found.svg.template";
//#endregion
20 changes: 2 additions & 18 deletions lib/common/global.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,19 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import '../logging/level.dart';
import 'utils.dart';

//#region debug options
bool debugClearDBWhenStart = false;
bool debugClearSharedPrefWhenStart = false;
//#endregion

//#region log level
final kLogLevel = _LoggingLevel();
LogLevel kAppLogLevel = getDefaultLogLevel();
//#endregion

final GlobalKey<ScaffoldMessengerState> snackbarKey =
GlobalKey<ScaffoldMessengerState>();

class _LoggingLevel {
late LogLevel level;

_LoggingLevel() {
if (kDebugMode) {
level = LogLevel.debug;
} else if (kProfileMode) {
level = LogLevel.info;
} else if (kReleaseMode) {
level = LogLevel.warn;
} else {
level = LogLevel.debug;
}
}
}
13 changes: 13 additions & 0 deletions lib/common/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import 'package:url_launcher/url_launcher.dart' as url_launcher;
import 'package:url_launcher/url_launcher_string.dart';
import 'package:uuid/uuid.dart';

import '../logging/level.dart';
import '../theme/color.dart';
import 'consts.dart';

Expand Down Expand Up @@ -208,3 +209,15 @@ T clampInt<T extends int>(T value, {T? min, T? max}) {
return value;
}
}

LogLevel getDefaultLogLevel() {
if (kDebugMode) {
return LogLevel.debug;
} else if (kProfileMode) {
return LogLevel.info;
} else if (kReleaseMode) {
return LogLevel.warn;
} else {
return LogLevel.debug;
}
}
2 changes: 1 addition & 1 deletion lib/logging/handler/filter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class AppLogFilter implements LogFilter {
Future<void> init() async {}

@override
Level get level => kLogLevel.level.toLoggerLevel();
Level get level => kAppLogLevel.toLoggerLevel();

@override
set level(Level? value) {}
Expand Down
4 changes: 2 additions & 2 deletions lib/logging/logger_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import 'package:flutter/foundation.dart';
import 'package:logger/logger.dart' as l;

import '../common/abc.dart';
import '../common/async.dart';
import 'handler/console_output.dart';
import 'handler/console_printer.dart';
import 'handler/filter.dart';
Expand All @@ -24,7 +24,7 @@ import 'logger/value_change_logger.dart';
import 'logger/widget_logger.dart';
import 'logger_type.dart';

abstract interface class AppLoggerMananger with FutureInitializationABC {
abstract interface class AppLoggerMananger implements AsyncInitialization {
static AppLoggerMananger? _instance;

l.Logger get logger;
Expand Down
29 changes: 0 additions & 29 deletions lib/model/habit_detail_chart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,12 @@ import 'package:flutter/foundation.dart';
import 'package:quiver/core.dart';

import '../common/types.dart';
import 'habit_date.dart';
import 'habit_form.dart';

enum HabitDetailFreqChartCombine { weekly, monthly, yearly }

enum HabitDetailScoreChartCombine { daily, weekly, monthly, yearly }

HabitDate getProtoDateByFreqChartCombine(
HabitDate date, HabitDetailFreqChartCombine combine, int firstDay) {
switch (combine) {
case HabitDetailFreqChartCombine.monthly:
return date.firstDayOfMonth;
case HabitDetailFreqChartCombine.yearly:
return date.copyWith(month: 1, day: 1);
case HabitDetailFreqChartCombine.weekly:
return date
.subtract(Duration(days: date.weekDayWithStartDay(firstDay) - 1));
}
}

HabitDate getProtoDateByScoreChartCombine(
HabitDate date, HabitDetailScoreChartCombine combine, int firstDay) {
switch (combine) {
case HabitDetailScoreChartCombine.monthly:
return date.firstDayOfMonth;
case HabitDetailScoreChartCombine.yearly:
return date.copyWith(month: 1, day: 1);
case HabitDetailScoreChartCombine.weekly:
return date
.subtract(Duration(days: date.weekDayWithStartDay(firstDay) - 1));
case HabitDetailScoreChartCombine.daily:
return date;
}
}

class HabitHeatMapColorMapDefine {
static num uncomplate = 0;
static num partiallyCompleted = 1;
Expand Down
4 changes: 2 additions & 2 deletions lib/persistent/db_helper_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import 'dart:async';
import 'package:async/async.dart';
import 'package:flutter/foundation.dart';

import '../common/abc.dart';
import '../common/async.dart';
import '../common/consts.dart';
import '../common/types.dart';
import '../logging/helper.dart';
Expand All @@ -28,7 +28,7 @@ import 'local/handler/habit.dart';
import 'local/handler/record.dart';

class DBHelperViewModel extends ChangeNotifier
with FutureInitializationABC, ProviderMounted {
implements ProviderMounted, AsyncInitialization {
final DBHelper local;

Completer? _completer;
Expand Down
6 changes: 3 additions & 3 deletions lib/persistent/local/db_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';

import '../../assets/assets.dart';
import '../../common/abc.dart';
import '../../common/async.dart';
import '../../common/consts.dart';
import '../../common/global.dart';
import '../../logging/helper.dart';
import '../../logging/logger_stack.dart';
import '../utils.dart';
import 'table.dart';
import 'handler/habit.dart';
import 'handler/record.dart';
import 'sql.dart';
import 'table.dart';

abstract interface class DBHelper with FutureInitializationABC {
abstract interface class DBHelper implements AsyncInitialization {
Database get db;

factory DBHelper() => _DBHelper();
Expand Down
4 changes: 2 additions & 2 deletions lib/persistent/profile_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import 'package:async/async.dart';
import 'package:flutter/foundation.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../common/abc.dart';
import '../common/async.dart';
import '../common/global.dart';
import '../logging/helper.dart';
import '../provider/commons.dart';
Expand All @@ -28,7 +28,7 @@ typedef ProfileHandlerBuilder<T extends ProfileHelperHandler> = T Function(
SharedPreferences pref);

class ProfileViewModel extends ChangeNotifier
with FutureInitializationABC, ProviderMounted {
implements ProviderMounted, AsyncInitialization {
late final SharedPreferences _pref;

final Iterable<ProfileHandlerBuilder> _handlerBuilders;
Expand Down
Loading