Skip to content

Commit

Permalink
add clear cache option in settings
Browse files Browse the repository at this point in the history
  • Loading branch information
smart7even committed Aug 12, 2024
1 parent da563cd commit 52273a5
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 6 deletions.
5 changes: 3 additions & 2 deletions lib/feature/initialization/data/initialize_dependencies.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ final Map<String, _InitializationStep> _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,
Expand Down
25 changes: 23 additions & 2 deletions lib/feature/settings/data/settings_local_data_provider.dart
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -13,10 +15,13 @@ abstract class ISettingsLocalDataProvider {
Future<String?> getLanguage();
Future<void> saveTheme(String theme);
Future<String?> getTheme();
Future<void> clearAppCache();
Future<bool> isAppCacheEmpty();
}

class SettingsLocalDataProvider implements ISettingsLocalDataProvider {
final SharedPreferences _prefs;
final MyDatabase _database;

static const String _groupIdKey = 'groupId';
static const String _groupNameKey = 'groupName';
Expand All @@ -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<Group?> getGroup() async {
Expand Down Expand Up @@ -145,4 +153,17 @@ class SettingsLocalDataProvider implements ISettingsLocalDataProvider {

return;
}

@override
Future<void> clearAppCache() async {
await _database.lessons.deleteAll();
}

@override
Future<bool> isAppCacheEmpty() async {
final lessonsCount = await _database.lessons.count().getSingle();
final isLessonsEmpty = lessonsCount == 0;

return isLessonsEmpty;
}
}
12 changes: 12 additions & 0 deletions lib/feature/settings/data/settings_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ abstract class ISettingsRepository {
Future<void> saveTheme(String theme);
Future<String?> getTheme();
Stream<String> getThemeChangedStream();
Future<void> clearAppCache();
Future<bool> isAppCacheEmpty();
}

class SettingsRepository implements ISettingsRepository {
Expand Down Expand Up @@ -93,4 +95,14 @@ class SettingsRepository implements ISettingsRepository {
Future<void> removeGroupFromFavorites(Group group) {
return _localDataProvider.removeGroupFromFavorites(group);
}

@override
Future<void> clearAppCache() {
return _localDataProvider.clearAppCache();
}

@override
Future<bool> isAppCacheEmpty() {
return _localDataProvider.isAppCacheEmpty();
}
}
55 changes: 55 additions & 0 deletions lib/feature/settings/widget/settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -205,6 +206,60 @@ class _SettingsPageState extends State<SettingsPage> {
);
},
),
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),
Expand Down
7 changes: 6 additions & 1 deletion lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
}
7 changes: 6 additions & 1 deletion lib/l10n/app_ru.arb
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,10 @@
"noScheduleDescription": "Свайпните влево, чтобы посмотреть расписание на следующую неделю, или вправо, чтобы посмотреть на предыдущую",
"licenses": "Лицензии",
"showLicenses": "Посмотреть список лицензий",
"appVersion": "Версия приложения"
"appVersion": "Версия приложения",
"cache": "Кэш",
"clearCache": "Очистить кэш",
"cacheIsCleared": "Кэш успешно очищен!",
"errorWhileCleaningCache": "Возникла неожиданная ошибка при очистке кэша. Попробуйте снова",
"cacheIsEmpty": "Кэш уже пуст!"
}

0 comments on commit 52273a5

Please sign in to comment.