diff --git a/lib/feature/initialization/data/initialize_dependencies.dart b/lib/feature/initialization/data/initialize_dependencies.dart index 069fe32..643666b 100644 --- a/lib/feature/initialization/data/initialize_dependencies.dart +++ b/lib/feature/initialization/data/initialize_dependencies.dart @@ -47,14 +47,15 @@ final Map _initializationSteps = 'Log app open': (_) {}, 'Initialize shared preferences': (dependencies) async => dependencies.sharedPreferences = await SharedPreferences.getInstance(), + 'Initialize database': (dependencies) async => + dependencies.database = MyDatabase(), 'Initialize settings repository': (dependencies) async => dependencies.settingsRepository = SettingsRepository( localDataProvider: SettingsLocalDataProvider( prefs: dependencies.sharedPreferences, + database: dependencies.database, ), ), - 'Initialize database': (dependencies) async => - dependencies.database = MyDatabase(), 'Initialize dio': (dependencies) async => dependencies.dio = Dio( BaseOptions( baseUrl: serverAddress, diff --git a/lib/feature/settings/data/settings_local_data_provider.dart b/lib/feature/settings/data/settings_local_data_provider.dart index 5bc6507..4467054 100644 --- a/lib/feature/settings/data/settings_local_data_provider.dart +++ b/lib/feature/settings/data/settings_local_data_provider.dart @@ -1,6 +1,8 @@ import 'dart:convert'; +import 'package:drift/drift.dart'; import 'package:shared_preferences/shared_preferences.dart'; +import 'package:uneconly/common/database/database.dart'; import 'package:uneconly/feature/select/model/group.dart'; abstract class ISettingsLocalDataProvider { @@ -13,10 +15,13 @@ abstract class ISettingsLocalDataProvider { Future getLanguage(); Future saveTheme(String theme); Future getTheme(); + Future clearAppCache(); + Future isAppCacheEmpty(); } class SettingsLocalDataProvider implements ISettingsLocalDataProvider { final SharedPreferences _prefs; + final MyDatabase _database; static const String _groupIdKey = 'groupId'; static const String _groupNameKey = 'groupName'; @@ -26,8 +31,11 @@ class SettingsLocalDataProvider implements ISettingsLocalDataProvider { static const String _themeKey = 'theme'; static const String _favoriteGroupsKey = 'favoriteGroups'; - SettingsLocalDataProvider({required SharedPreferences prefs}) - : _prefs = prefs; + SettingsLocalDataProvider({ + required SharedPreferences prefs, + required MyDatabase database, + }) : _prefs = prefs, + _database = database; @override Future getGroup() async { @@ -145,4 +153,17 @@ class SettingsLocalDataProvider implements ISettingsLocalDataProvider { return; } + + @override + Future clearAppCache() async { + await _database.lessons.deleteAll(); + } + + @override + Future isAppCacheEmpty() async { + final lessonsCount = await _database.lessons.count().getSingle(); + final isLessonsEmpty = lessonsCount == 0; + + return isLessonsEmpty; + } } diff --git a/lib/feature/settings/data/settings_repository.dart b/lib/feature/settings/data/settings_repository.dart index ae8b12f..306e351 100644 --- a/lib/feature/settings/data/settings_repository.dart +++ b/lib/feature/settings/data/settings_repository.dart @@ -16,6 +16,8 @@ abstract class ISettingsRepository { Future saveTheme(String theme); Future getTheme(); Stream getThemeChangedStream(); + Future clearAppCache(); + Future isAppCacheEmpty(); } class SettingsRepository implements ISettingsRepository { @@ -93,4 +95,14 @@ class SettingsRepository implements ISettingsRepository { Future removeGroupFromFavorites(Group group) { return _localDataProvider.removeGroupFromFavorites(group); } + + @override + Future clearAppCache() { + return _localDataProvider.clearAppCache(); + } + + @override + Future isAppCacheEmpty() { + return _localDataProvider.isAppCacheEmpty(); + } } diff --git a/lib/feature/settings/widget/settings_page.dart b/lib/feature/settings/widget/settings_page.dart index dc1d11b..9c70dbd 100644 --- a/lib/feature/settings/widget/settings_page.dart +++ b/lib/feature/settings/widget/settings_page.dart @@ -2,6 +2,7 @@ import 'dart:io'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; +import 'package:l/l.dart'; import 'package:uneconly/common/localization/localization.dart'; import 'package:uneconly/common/model/dependencies.dart'; import 'package:uneconly/common/utils/colors_utils.dart'; @@ -205,6 +206,60 @@ class _SettingsPageState extends State { ); }, ), + const SizedBox(height: 15), + SettingsTile( + title: AppLocalizations.of(context)!.cache, + description: AppLocalizations.of(context)!.clearCache, + onPressed: () async { + final settingsRepository = + Dependencies.of(context).settingsRepository; + + try { + final isCacheEmpty = + await settingsRepository.isAppCacheEmpty(); + + if (isCacheEmpty) { + l.vvvv('Nothing to clear'); + + if (context.mounted) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text( + AppLocalizations.of(context)!.cacheIsEmpty, + ), + ), + ); + } + + return; + } + + l.vvvv('Cache is not empty and may be cleared'); + await settingsRepository.clearAppCache(); + if (context.mounted) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text( + AppLocalizations.of(context)!.cacheIsCleared, + ), + ), + ); + } + } on Exception catch (e) { + l.vvvv(e); + if (context.mounted) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text( + AppLocalizations.of(context)! + .errorWhileCleaningCache, + ), + ), + ); + } + } + }, + ), ], ), const SizedBox(height: 15), diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index 8bf6dfd..14a266e 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -41,5 +41,10 @@ "noScheduleDescription": "Swipe left to see the schedule for the next week or right to see the schedule for the previous week", "licenses": "Licenses", "showLicenses": "View licenses list", - "appVersion": "App version" + "appVersion": "App version", + "cache": "Cache", + "clearCache": "Clear cache", + "cacheIsCleared": "Cache is successfully cleared!", + "errorWhileCleaningCache": "Unexpected error happened during cache clean. Try again later", + "cacheIsEmpty": "Cache is empty. Nothing to clean!" } \ No newline at end of file diff --git a/lib/l10n/app_ru.arb b/lib/l10n/app_ru.arb index 182bbbd..30e48ea 100644 --- a/lib/l10n/app_ru.arb +++ b/lib/l10n/app_ru.arb @@ -38,5 +38,10 @@ "noScheduleDescription": "Свайпните влево, чтобы посмотреть расписание на следующую неделю, или вправо, чтобы посмотреть на предыдущую", "licenses": "Лицензии", "showLicenses": "Посмотреть список лицензий", - "appVersion": "Версия приложения" + "appVersion": "Версия приложения", + "cache": "Кэш", + "clearCache": "Очистить кэш", + "cacheIsCleared": "Кэш успешно очищен!", + "errorWhileCleaningCache": "Возникла неожиданная ошибка при очистке кэша. Попробуйте снова", + "cacheIsEmpty": "Кэш уже пуст!" } \ No newline at end of file