From 00d6277bc6cc1cbf4eb7d357f53d5a6ecdf3f3e1 Mon Sep 17 00:00:00 2001 From: William Jordan-Cooley Date: Thu, 24 Oct 2024 12:48:51 -0400 Subject: [PATCH] some code cleanup and comments --- lib/pangea/controllers/base_controller.dart | 8 ++++---- lib/pangea/controllers/user_controller.dart | 7 +++++++ lib/pangea/enum/construct_use_type_enum.dart | 9 +++++++++ lib/pangea/models/analytics/constructs_model.dart | 5 +---- .../message_activity_request.dart | 7 +------ 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/lib/pangea/controllers/base_controller.dart b/lib/pangea/controllers/base_controller.dart index 69939e50a..51a66dd00 100644 --- a/lib/pangea/controllers/base_controller.dart +++ b/lib/pangea/controllers/base_controller.dart @@ -1,18 +1,18 @@ import 'dart:async'; class BaseController { - final StreamController stateListener = StreamController(); + final StreamController _stateListener = StreamController(); late Stream stateStream; BaseController() { - stateStream = stateListener.stream.asBroadcastStream(); + stateStream = _stateListener.stream.asBroadcastStream(); } dispose() { - stateListener.close(); + _stateListener.close(); } setState(T data) { - stateListener.add(data); + _stateListener.add(data); } } diff --git a/lib/pangea/controllers/user_controller.dart b/lib/pangea/controllers/user_controller.dart index 5ee8672ff..ca7e2869a 100644 --- a/lib/pangea/controllers/user_controller.dart +++ b/lib/pangea/controllers/user_controller.dart @@ -121,19 +121,26 @@ class UserController extends BaseController { /// Initializes the user's profile by waiting for account data to load, reading in account /// data to profile, and migrating from the pangea profile if the account data is not present. Future _initialize() async { + // wait for account data to load + // as long as it's not null, then this we've already migrated the profile await _pangeaController.matrixState.client.waitForAccountData(); if (profile.userSettings.dateOfBirth != null) { return; } + // we used to store the user's profile in the pangea server + // we now store it in the matrix account data final PangeaProfileResponse? resp = await PUserRepo.fetchPangeaUserInfo( userID: userId!, matrixAccessToken: _matrixAccessToken!, ); + + // if it's null, we don't have a profile in the pangea server if (resp?.profile == null) { return; } + // if we have a profile in the pangea server, we need to migrate it to the matrix account data final userSetting = UserSettings.fromJson(resp!.profile.toJson()); final newProfile = Profile(userSettings: userSetting); await newProfile.saveProfileData(waitForDataInSync: true); diff --git a/lib/pangea/enum/construct_use_type_enum.dart b/lib/pangea/enum/construct_use_type_enum.dart index 1f1d37dfe..6ced270b7 100644 --- a/lib/pangea/enum/construct_use_type_enum.dart +++ b/lib/pangea/enum/construct_use_type_enum.dart @@ -125,3 +125,12 @@ extension ConstructUseTypeExtension on ConstructUseTypeEnum { } } } + +class ConstructUseTypeUtil { + static ConstructUseTypeEnum fromString(String value) { + return ConstructUseTypeEnum.values.firstWhere( + (e) => e.string == value, + orElse: () => ConstructUseTypeEnum.nan, + ); + } +} diff --git a/lib/pangea/models/analytics/constructs_model.dart b/lib/pangea/models/analytics/constructs_model.dart index 10a47516a..a9781f9ae 100644 --- a/lib/pangea/models/analytics/constructs_model.dart +++ b/lib/pangea/models/analytics/constructs_model.dart @@ -1,6 +1,5 @@ import 'dart:developer'; -import 'package:collection/collection.dart'; import 'package:fluffychat/pangea/enum/construct_use_type_enum.dart'; import 'package:fluffychat/pangea/utils/error_handler.dart'; import 'package:flutter/foundation.dart'; @@ -106,9 +105,7 @@ class OneConstructUse { debugger(when: kDebugMode && constructType == null); return OneConstructUse( - useType: ConstructUseTypeEnum.values - .firstWhereOrNull((e) => e.string == json['useType']) ?? - ConstructUseTypeEnum.unk, + useType: ConstructUseTypeUtil.fromString(json['useType']), lemma: json['lemma'], form: json['form'], categories: json['categories'] != null diff --git a/lib/pangea/models/practice_activities.dart/message_activity_request.dart b/lib/pangea/models/practice_activities.dart/message_activity_request.dart index 0740fb8c3..9101a78ce 100644 --- a/lib/pangea/models/practice_activities.dart/message_activity_request.dart +++ b/lib/pangea/models/practice_activities.dart/message_activity_request.dart @@ -27,12 +27,7 @@ class ConstructWithXP { ? DateTime.parse(json['last_used'] as String) : null, condensedConstructUses: (json['uses'] as List).map((e) { - return ConstructUseTypeEnum.values.firstWhereOrNull( - (element) => - element.string == e || - element.toString().split('.').last == e, - ) ?? - ConstructUseTypeEnum.nan; + return ConstructUseTypeUtil.fromString(e); }).toList(), ); }