Skip to content

Commit

Permalink
fix(storage optimization): Removed and replaced used of PStore with G… (
Browse files Browse the repository at this point in the history
#1730)

* fix(storage optimization): Removed and replaced used of PStore with GetStorage

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: ggurdin <46800240+ggurdin@users.noreply.github.com>
Co-authored-by: ggurdin <ggurdin@gmail.com>
  • Loading branch information
4 people authored Feb 21, 2025
1 parent 67b1183 commit 4c1594d
Show file tree
Hide file tree
Showing 18 changed files with 111 additions and 168 deletions.
4 changes: 2 additions & 2 deletions lib/pages/chat_list/chat_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -507,10 +507,10 @@ class ChatListController extends State<ChatList>
spaceId,
);

// #Pangea
final String? justInputtedCode =
MatrixState.pangeaController.pStoreService.read(
MatrixState.pangeaController.classController.chatBox.read(
PLocalKey.justInputtedCode,
isAccountData: false,
);
final newSpaceCode = space?.classCode(context);
if (newSpaceCode == justInputtedCode) return;
Expand Down
12 changes: 7 additions & 5 deletions lib/pangea/activity_planner/bookmarked_activities_repo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import 'package:uuid/uuid.dart';
import 'package:fluffychat/pangea/activity_planner/activity_plan_model.dart';

class BookmarkedActivitiesRepo {
static final GetStorage _storage = GetStorage('bookmarked_activities');
static const Uuid _uuid = Uuid();

static final GetStorage _bookStorage = GetStorage('bookmarked_activities');

/// save an activity to the list of bookmarked activities
/// returns the activity with a bookmarkId
static Future<ActivityPlanModel> save(ActivityPlanModel activity) async {
activity.bookmarkId ??= _uuid.v4();

await _storage.write(
await _bookStorage.write(
activity.bookmarkId!,
activity.toJson(),
);
Expand All @@ -23,15 +24,16 @@ class BookmarkedActivitiesRepo {
return activity;
}

static Future<void> remove(String bookmarkId) => _storage.remove(bookmarkId);
static Future<void> remove(String bookmarkId) =>
_bookStorage.remove(bookmarkId);

static bool isBookmarked(ActivityPlanModel activity) {
return activity.bookmarkId != null &&
_storage.read(activity.bookmarkId!) != null;
_bookStorage.read(activity.bookmarkId!) != null;
}

static List<ActivityPlanModel> get() {
final list = _storage.getValues();
final list = _bookStorage.getValues();

if (list == null) return [];

Expand Down
4 changes: 3 additions & 1 deletion lib/pangea/analytics_misc/get_analytics_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:math';

import 'package:flutter/material.dart';

import 'package:get_storage/get_storage.dart';
import 'package:matrix/matrix.dart';
import 'package:sentry_flutter/sentry_flutter.dart';

Expand All @@ -22,6 +23,7 @@ import 'package:fluffychat/pangea/learning_settings/models/language_model.dart';

/// A minimized version of AnalyticsController that get the logged in user's analytics
class GetAnalyticsController extends BaseController {
final GetStorage analyticsBox = GetStorage("analytics_storage");
late PangeaController _pangeaController;
late MessageAnalyticsController perMessage;

Expand Down Expand Up @@ -173,7 +175,7 @@ class GetAnalyticsController extends BaseController {
/// eventID.
Map<String, List<OneConstructUse>> get messagesSinceUpdate {
try {
final dynamic locallySaved = _pangeaController.pStoreService.read(
final dynamic locallySaved = analyticsBox.read(
PLocalKey.messagesSinceUpdate,
);
if (locallySaved == null) return {};
Expand Down
9 changes: 6 additions & 3 deletions lib/pangea/analytics_misc/put_analytics_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'package:fluffychat/pangea/common/utils/error_handler.dart';
import 'package:fluffychat/pangea/events/models/pangea_token_model.dart';
import 'package:fluffychat/pangea/extensions/pangea_room_extension.dart';
import 'package:fluffychat/pangea/learning_settings/models/language_model.dart';
import 'package:fluffychat/widgets/matrix.dart';

enum AnalyticsUpdateType { server, local }

Expand Down Expand Up @@ -316,14 +317,16 @@ class PutAnalyticsController extends BaseController<AnalyticsStream> {
/// Clears the local cache of recently sent constructs. Called before updating analytics
void clearMessagesSinceUpdate({clearDrafts = false}) {
if (clearDrafts) {
_pangeaController.pStoreService.delete(PLocalKey.messagesSinceUpdate);
MatrixState.pangeaController.getAnalytics.analyticsBox
.remove(PLocalKey.messagesSinceUpdate);
return;
}

final localCache = _pangeaController.getAnalytics.messagesSinceUpdate;
final draftKeys = localCache.keys.where((key) => key.startsWith('draft'));
if (draftKeys.isEmpty) {
_pangeaController.pStoreService.delete(PLocalKey.messagesSinceUpdate);
MatrixState.pangeaController.getAnalytics.analyticsBox
.remove(PLocalKey.messagesSinceUpdate);
return;
}

Expand All @@ -343,7 +346,7 @@ class PutAnalyticsController extends BaseController<AnalyticsStream> {
final constructJsons = entry.value.map((e) => e.toJson()).toList();
formattedCache[entry.key] = constructJsons;
}
await _pangeaController.pStoreService.save(
await MatrixState.pangeaController.getAnalytics.analyticsBox.write(
PLocalKey.messagesSinceUpdate,
formattedCache,
);
Expand Down
8 changes: 5 additions & 3 deletions lib/pangea/chat_list/utils/app_version_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:get_storage/get_storage.dart';
import 'package:http/http.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:universal_html/html.dart' as html;
Expand All @@ -22,6 +23,8 @@ import 'package:fluffychat/widgets/adaptive_dialogs/show_ok_cancel_alert_dialog.
import 'package:fluffychat/widgets/matrix.dart';

class AppVersionUtil {
static final GetStorage _versionBox = GetStorage("version_storage");

static Future<AppVersionResponse> _getAppVersion(
String accessToken,
) async {
Expand Down Expand Up @@ -148,7 +151,7 @@ class AppVersionUtil {
);

if (!mandatoryUpdate && dialogResponse != OkCancelResult.ok) {
await MatrixState.pangeaController.pStoreService.save(
await _versionBox.write(
PLocalKey.showedUpdateDialog,
DateTime.now().toIso8601String(),
);
Expand Down Expand Up @@ -210,8 +213,7 @@ class AppVersionUtil {
}

static DateTime? get showedUpdateDialog {
final entry = MatrixState.pangeaController.pStoreService
.read(PLocalKey.showedUpdateDialog);
final entry = _versionBox.read(PLocalKey.showedUpdateDialog);
if (entry == null) return null;
try {
return DateTime.parse(entry);
Expand Down
7 changes: 3 additions & 4 deletions lib/pangea/chat_list/utils/chat_list_handle_space_tap.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'package:matrix/matrix.dart';
import 'package:fluffychat/config/themes.dart';
import 'package:fluffychat/pages/chat_list/chat_list.dart';
import 'package:fluffychat/pangea/common/constants/local.key.dart';
import 'package:fluffychat/pangea/common/controllers/pangea_controller.dart';
import 'package:fluffychat/pangea/extensions/pangea_room_extension.dart';
import 'package:fluffychat/widgets/adaptive_dialogs/show_ok_cancel_alert_dialog.dart';
import 'package:fluffychat/widgets/future_loading_dialog.dart';
Expand All @@ -20,7 +19,6 @@ void chatListHandleSpaceTap(
ChatListController controller,
Room space,
) {
final PangeaController pangeaController = MatrixState.pangeaController;
void setActiveSpaceAndCloseChat() {
controller.setActiveSpace(space.id);

Expand Down Expand Up @@ -102,8 +100,9 @@ void chatListHandleSpaceTap(
(element) =>
element.isSpace && element.membership == Membership.join,
);
final justInputtedCode = pangeaController.pStoreService
.read(PLocalKey.justInputtedCode, isAccountData: false);
final justInputtedCode = MatrixState
.pangeaController.classController.chatBox
.read(PLocalKey.justInputtedCode);
if (rooms.any((s) => s.spaceChildren.any((c) => c.roomId == space.id))) {
autoJoin(space);
} else if (justInputtedCode != null &&
Expand Down
3 changes: 0 additions & 3 deletions lib/pangea/common/controllers/pangea_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import 'package:fluffychat/widgets/matrix.dart';
import '../../../config/app_config.dart';
import '../../choreographer/controllers/it_feedback_controller.dart';
import '../utils/firebase_analytics.dart';
import '../utils/p_store.dart';

class PangeaController {
///pangeaControllers
Expand All @@ -56,7 +55,6 @@ class PangeaController {
late PracticeActivityRecordController activityRecordController;

///store Services
late PStore pStoreService;
final pLanguageStore = PangeaLanguage();

StreamSubscription? _languageStream;
Expand Down Expand Up @@ -94,7 +92,6 @@ class PangeaController {

/// Initialize controllers
_addRefInObjects() {
pStoreService = PStore(pangeaController: this);
userController = UserController(this);
languageController = LanguageController(this);
classController = ClassController(this);
Expand Down
67 changes: 0 additions & 67 deletions lib/pangea/common/utils/p_store.dart

This file was deleted.

3 changes: 1 addition & 2 deletions lib/pangea/common/widgets/customized_svg.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ class CustomizedSvg extends StatelessWidget {
/// Icon to show in case of error
final Widget errorIcon;

static final GetStorage _svgStorage = GetStorage('svg_cache');
const CustomizedSvg({
super.key,
required this.svgUrl,
required this.colorReplacements,
this.errorIcon = const Icon(Icons.error_outline),
});

static final GetStorage _svgStorage = GetStorage('svg_cache');

Future<String?> _fetchSvg() async {
final cachedSvgEntry = _svgStorage.read(svgUrl);
if (cachedSvgEntry != null && cachedSvgEntry is Map<String, dynamic>) {
Expand Down
4 changes: 2 additions & 2 deletions lib/pangea/login/pages/user_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ class UserSettingsState extends State<UserSettingsPage> {
}

bool get isSSOSignup {
final loginTypeEntry =
_pangeaController.pStoreService.read(PLocalKey.loginType);
final loginTypeEntry = MatrixState.pangeaController.userController.loginBox
.read(PLocalKey.loginType);
return loginTypeEntry is String && loginTypeEntry == 'sso';
}

Expand Down
4 changes: 3 additions & 1 deletion lib/pangea/login/utils/sso_login_action.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ Future<void> pangeaSSOLoginAction(
token: token,
initialDeviceDisplayName: PlatformInfos.clientName,
);
MatrixState.pangeaController.pStoreService.save(PLocalKey.loginType, 'sso');

MatrixState.pangeaController.userController.loginBox
.write(PLocalKey.loginType, 'sso');
GoogleAnalytics.login(provider.name!, loginRes.userId);
}
22 changes: 11 additions & 11 deletions lib/pangea/spaces/controllers/space_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,38 @@ import '../../common/controllers/base_controller.dart';
class ClassController extends BaseController {
late PangeaController _pangeaController;

//Storage Initialization
final GetStorage chatBox = GetStorage("chat_list_storage");
final GetStorage linkBox = GetStorage("link_storage");
static final GetStorage _classStorage = GetStorage('class_storage');

ClassController(PangeaController pangeaController) : super() {
_pangeaController = pangeaController;
}

static final GetStorage _aliasStorage = GetStorage('alias_storage');

void setActiveSpaceIdInChatListController(String? classId) {
setState({"activeSpaceId": classId});
}

Future<void> joinCachedSpaceCode(BuildContext context) async {
final String? classCode = _pangeaController.pStoreService.read(
final String? classCode = linkBox.read(
PLocalKey.cachedClassCodeToJoin,
isAccountData: false,
);

final String? alias = _aliasStorage.read(PLocalKey.cachedAliasToJoin);
final String? alias = _classStorage.read(PLocalKey.cachedAliasToJoin);

if (classCode != null) {
await joinClasswithCode(
context,
classCode,
);

await _pangeaController.pStoreService.delete(
await linkBox.remove(
PLocalKey.cachedClassCodeToJoin,
isAccountData: false,
);
} else if (alias != null) {
await joinCachedRoomAlias(alias, context);
await _aliasStorage.remove(PLocalKey.cachedAliasToJoin);
await _classStorage.remove(PLocalKey.cachedAliasToJoin);
}
}

Expand All @@ -65,7 +66,7 @@ class ClassController extends BaseController {

final client = Matrix.of(context).client;
if (!client.isLogged()) {
await _aliasStorage.write(PLocalKey.cachedAliasToJoin, alias);
await _classStorage.write(PLocalKey.cachedAliasToJoin, alias);
context.go("/home");
return;
}
Expand Down Expand Up @@ -140,10 +141,9 @@ class ClassController extends BaseController {
}

final chosenClassId = foundClasses.first;
await _pangeaController.pStoreService.save(
await chatBox.write(
PLocalKey.justInputtedCode,
classCode,
isAccountData: false,
);
return chosenClassId;
},
Expand Down
3 changes: 1 addition & 2 deletions lib/pangea/spaces/utils/join_with_link.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ class _JoinClassWithLinkState extends State<JoinClassWithLink> {
);
return;
}
await pangeaController.pStoreService.save(
await MatrixState.pangeaController.classController.linkBox.write(
PLocalKey.cachedClassCodeToJoin,
classCode,
isAccountData: false,
);
context.go("/home");
});
Expand Down
Loading

0 comments on commit 4c1594d

Please sign in to comment.