Skip to content

Commit

Permalink
✨ Always open last viewed chest first
Browse files Browse the repository at this point in the history
  • Loading branch information
JakesMD committed Feb 14, 2025
1 parent 67cfd6c commit a0252c1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
11 changes: 10 additions & 1 deletion app/lib/pages/chest/page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class CChestPage extends StatelessWidget implements AutoRouteWrapper {
lazy: false,
create: (context) => CCurrentChestCubit(
chestID: chestID,
lastViewedChest:
context.read<CAppSettingsCubit>().state.lastViewedChest,
authRepository: context.read(),
),
child: BlocProvider(
Expand All @@ -39,7 +41,14 @@ class CChestPage extends StatelessWidget implements AutoRouteWrapper {
)..fetchChestPeople(
chestID: context.read<CCurrentChestCubit>().state.id,
),
child: this,
child: BlocListener<CChestPeopleFetchCubit, CChestPeopleFetchState>(
listener: (context, _) =>
context.read<CAppSettingsCubit>().updateLastViewedChest(
context.read<CCurrentChestCubit>().state.id,
),
listenWhen: (_, state) => state.succeeded,
child: this,
),
),
);
}
Expand Down
32 changes: 29 additions & 3 deletions app/lib/shared/logic/app_settings_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,33 @@ import 'package:hydrated_bloc/hydrated_bloc.dart';
/// {@endtemplate}
class CAppSettingsState {
/// {@macro CAppSettingsState}
const CAppSettingsState({required this.themeMode, this.locale});
const CAppSettingsState({
required this.themeMode,
this.locale,
this.lastViewedChest,
});

/// The current locale of the app.
final Locale? locale;

/// The current theme mode of the app.
final ThemeMode themeMode;

/// The last viewed chest.
final String? lastViewedChest;

/// Creates a copy of this state with the given fields replaced with the
/// given values.
CAppSettingsState copyWith({
Locale? locale,
ThemeMode? themeMode,
String? lastViewedChest,
}) =>
CAppSettingsState(
locale: locale ?? this.locale,
themeMode: themeMode ?? this.themeMode,
lastViewedChest: lastViewedChest ?? this.lastViewedChest,
);
}

/// {@template CAppSettingsCubit}
Expand All @@ -29,11 +49,15 @@ class CAppSettingsCubit extends HydratedCubit<CAppSettingsState> {

/// Updates the current locale.
void changeLocale({required Locale newLocale}) =>
emit(CAppSettingsState(locale: newLocale, themeMode: state.themeMode));
emit(state.copyWith(locale: newLocale));

/// Updates the current theme mode.
void changeThemeMode({required ThemeMode newThemeMode}) =>
emit(CAppSettingsState(locale: state.locale, themeMode: newThemeMode));
emit(state.copyWith(themeMode: newThemeMode));

/// Updates the last viewed chest.
void updateLastViewedChest(String chestID) =>
emit(state.copyWith(lastViewedChest: chestID));

@override
CAppSettingsState fromJson(Map<String, dynamic> json) => CAppSettingsState(
Expand All @@ -46,12 +70,14 @@ class CAppSettingsCubit extends HydratedCubit<CAppSettingsState> {
themeMode: json.containsKey('themeMode')
? ThemeMode.values[json['themeMode'] as int]
: ThemeMode.system,
lastViewedChest: json['lastViewedChest'] as String?,
);

@override
Map<String, dynamic> toJson(CAppSettingsState state) => {
if (state.locale != null) 'languageCode': state.locale!.languageCode,
if (state.locale != null) 'countryCode': state.locale!.countryCode,
'themeMode': state.themeMode.index,
'lastViewedChest': state.lastViewedChest,
};
}
5 changes: 4 additions & 1 deletion app/lib/shared/logic/current_chest_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ class CCurrentChestCubit extends Cubit<CAuthUserChest> {
/// {@macro CCurrentChestCubit}
CCurrentChestCubit({
required String? chestID,
required String? lastViewedChest,
required this.authRepository,
}) : super(
authRepository.currentUser!.chests.firstWhere(
(chest) => chest.id == chestID,
(chest) => chestID != null
? chest.id == chestID
: chest.id == lastViewedChest,
orElse: () => authRepository.currentUser!.chests.first,
),
);
Expand Down
2 changes: 1 addition & 1 deletion app/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: chuckle_chest
description: A free and open-source app for preserving memories that were not caught on camera.
publish_to: "none"
version: 0.18.0+1
version: 0.19.0+1

environment:
sdk: ">=3.4.0 <4.0.0"
Expand Down

0 comments on commit a0252c1

Please sign in to comment.