replace bloc with watch_it

This commit is contained in:
shenlong-tanwen
2024-05-10 02:00:00 +05:30
parent aa5673bae3
commit fb6253d2d1
29 changed files with 663 additions and 239 deletions
@@ -1,15 +0,0 @@
import 'package:bloc/bloc.dart';
part 'home_state.dart';
class HomeCubit extends Cubit<HomeState> {
HomeCubit() : super(HomeState(albumCount: 0));
void increaseAlbumCount() {
emit(state.copyWith(albumCount: state.albumCount + 1));
}
void decreaseAlbumCount() {
emit(state.copyWith(albumCount: state.albumCount - 1));
}
}
@@ -1,41 +0,0 @@
part of 'home_cubit.dart';
class HomeState {
final int albumCount;
HomeState({
required this.albumCount,
});
HomeState copyWith({
int? albumCount,
}) {
return HomeState(
albumCount: albumCount ?? this.albumCount,
);
}
Map<String, dynamic> toMap() {
return {
'albumCount': albumCount,
};
}
factory HomeState.fromMap(Map<String, dynamic> map) {
return HomeState(
albumCount: map['albumCount']?.toInt() ?? 0,
);
}
@override
String toString() => 'HomeState(albumCount: $albumCount)';
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is HomeState && other.albumCount == albumCount;
}
@override
int get hashCode => albumCount.hashCode;
}
@@ -0,0 +1,12 @@
import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
@RoutePage()
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return Container();
}
}
@@ -0,0 +1,12 @@
import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
@RoutePage()
class LibraryPage extends StatelessWidget {
const LibraryPage({super.key});
@override
Widget build(BuildContext context) {
return Container();
}
}
@@ -0,0 +1,12 @@
import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
@RoutePage()
class SearchPage extends StatelessWidget {
const SearchPage({super.key});
@override
Widget build(BuildContext context) {
return Container();
}
}
@@ -0,0 +1,12 @@
import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
@RoutePage()
class SettingsPage extends StatelessWidget {
const SettingsPage({super.key});
@override
Widget build(BuildContext context) {
return Container();
}
}
@@ -0,0 +1,12 @@
import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
@RoutePage()
class SharingPage extends StatelessWidget {
const SharingPage({super.key});
@override
Widget build(BuildContext context) {
return Container();
}
}
-9
View File
@@ -1,9 +0,0 @@
import 'package:auto_route/auto_route.dart';
part 'router.gr.dart';
@AutoRouterConfig(replaceInRouteName: 'Page,Route')
class AppRouter extends _$AppRouter {
@override
List<AutoRoute> get routes => [];
}
@@ -0,0 +1,132 @@
import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter_adaptive_scaffold/flutter_adaptive_scaffold.dart';
import 'package:immich_mobile/i18n/strings.g.dart';
import 'package:immich_mobile/presentation/router/router.dart';
import 'package:material_symbols_icons/symbols.dart';
@RoutePage()
class TabControllerPage extends StatelessWidget {
const TabControllerPage({super.key});
@override
Widget build(BuildContext context) {
return AutoTabsRouter(
routes: const [
HomeRoute(),
SearchRoute(),
SharingRoute(),
LibraryRoute(),
],
builder: (ctx, child) {
final tabsRouter = AutoTabsRouter.of(ctx);
// Pop-back to photos tab or if already in photos tab, close the app
return PopScope(
canPop: tabsRouter.activeIndex == 0,
onPopInvoked: (didPop) =>
!didPop ? tabsRouter.setActiveIndex(0) : null,
child: _TabControllerAdaptiveScaffold(
body: (ctxx) => child,
selectedIndex: tabsRouter.activeIndex,
onSelectedIndexChange: (index) => tabsRouter.setActiveIndex(index),
destinations: [
NavigationDestination(
icon: const Icon(Symbols.photo_rounded),
selectedIcon: const Icon(Symbols.photo_rounded, fill: 1.0),
label: context.t.tab_controller.photos,
),
NavigationDestination(
icon: const Icon(Symbols.search_rounded),
selectedIcon: const Icon(Symbols.search_rounded, fill: 1.0),
label: context.t.tab_controller.search,
),
NavigationDestination(
icon: const Icon(Symbols.group_rounded),
selectedIcon: const Icon(Symbols.group_rounded, fill: 1.0),
label: context.t.tab_controller.sharing,
),
NavigationDestination(
icon: const Icon(Symbols.newsstand_rounded),
selectedIcon: const Icon(Symbols.newsstand_rounded, fill: 1.0),
label: context.t.tab_controller.library,
),
],
),
);
},
);
}
}
/// Adaptive scaffold to layout bottom navigation bar and navigation rail for the main
/// tab controller layout. This is not used elsewhere so is private to this widget
class _TabControllerAdaptiveScaffold extends StatelessWidget {
const _TabControllerAdaptiveScaffold({
required this.body,
required this.selectedIndex,
required this.onSelectedIndexChange,
required this.destinations,
});
final WidgetBuilder body;
final List<NavigationDestination> destinations;
final int selectedIndex;
final void Function(int) onSelectedIndexChange;
@override
Widget build(BuildContext context) {
final NavigationRailThemeData navRailTheme =
Theme.of(context).navigationRailTheme;
return Scaffold(
body: AdaptiveLayout(
// No animation on layout change
transitionDuration: Duration.zero,
primaryNavigation: SlotLayout(
config: <Breakpoint, SlotLayoutConfig>{
Breakpoints.mediumAndUp: SlotLayout.from(
key: const Key(
'_TabControllerAdaptiveScaffold Primary Navigation Medium',
),
builder: (_) => AdaptiveScaffold.standardNavigationRail(
selectedIndex: selectedIndex,
destinations: destinations
.map((NavigationDestination destination) =>
AdaptiveScaffold.toRailDestination(destination))
.toList(),
onDestinationSelected: onSelectedIndexChange,
backgroundColor: navRailTheme.backgroundColor,
selectedIconTheme: navRailTheme.selectedIconTheme,
unselectedIconTheme: navRailTheme.unselectedIconTheme,
selectedLabelTextStyle: navRailTheme.selectedLabelTextStyle,
unSelectedLabelTextStyle: navRailTheme.unselectedLabelTextStyle,
),
),
},
),
body: SlotLayout(
config: {
Breakpoints.standard: SlotLayout.from(
key: const Key('_TabControllerAdaptiveScaffold Body'),
builder: body,
),
},
),
),
bottomNavigationBar: SlotLayout(
config: <Breakpoint, SlotLayoutConfig>{
Breakpoints.small: SlotLayout.from(
key: const Key(
'_TabControllerAdaptiveScaffold Bottom Navigation Small',
),
builder: (_) => AdaptiveScaffold.standardBottomNavigationBar(
currentIndex: selectedIndex,
destinations: destinations,
onDestinationSelected: onSelectedIndexChange,
),
),
},
),
);
}
}
@@ -0,0 +1,25 @@
import 'package:auto_route/auto_route.dart';
import 'package:immich_mobile/presentation/modules/home/pages/home.page.dart';
import 'package:immich_mobile/presentation/modules/library/pages/library.page.dart';
import 'package:immich_mobile/presentation/modules/search/pages/search.page.dart';
import 'package:immich_mobile/presentation/modules/settings/pages/settings.page.dart';
import 'package:immich_mobile/presentation/modules/sharing/pages/sharing.page.dart';
import 'package:immich_mobile/presentation/router/pages/tab_controller.page.dart';
part 'router.gr.dart';
@AutoRouterConfig(replaceInRouteName: 'Page,Route')
class AppRouter extends _$AppRouter {
AppRouter();
@override
List<AutoRoute> get routes => [
AutoRoute(page: TabControllerRoute.page, initial: true, children: [
AutoRoute(page: HomeRoute.page),
AutoRoute(page: SearchRoute.page),
AutoRoute(page: SharingRoute.page),
AutoRoute(page: LibraryRoute.page),
]),
AutoRoute(page: SettingsRoute.page),
];
}
@@ -0,0 +1,30 @@
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:immich_mobile/domain/models/app_setting.model.dart';
import 'package:immich_mobile/domain/services/app_setting.service.dart';
import 'package:immich_mobile/presentation/theme/utils/colors.dart';
class AppThemeState extends ValueNotifier<AppTheme> {
final AppSettingsService _appSettings;
StreamSubscription? _appSettingSubscription;
AppThemeState({required AppSettingsService appSettings})
: _appSettings = appSettings,
super(AppTheme.blue);
void init() {
_appSettingSubscription =
_appSettings.watchSetting(AppSettings.appTheme).listen((themeIndex) {
final theme =
AppTheme.values.elementAtOrNull(themeIndex) ?? AppTheme.blue;
value = theme;
});
}
@override
void dispose() {
_appSettingSubscription?.cancel();
return super.dispose();
}
}
@@ -0,0 +1,92 @@
import 'package:flutter/material.dart';
enum AppTheme {
blue(AppColors._blueLight, AppColors._blueDark),
// Fallback color for dynamic theme for non-supported platforms
dynamic(AppColors._blueLight, AppColors._blueDark);
final ColorScheme lightSchema;
final ColorScheme darkSchema;
const AppTheme(this.lightSchema, this.darkSchema);
}
class AppColors {
const AppColors();
/// Blue color
static const ColorScheme _blueLight = ColorScheme(
brightness: Brightness.light,
primary: Color(0xff1565c0),
onPrimary: Color(0xffffffff),
primaryContainer: Color(0xffd6e3ff),
onPrimaryContainer: Color(0xff001b3d),
secondary: Color(0xff3277d2),
onSecondary: Color(0xfffdfbff),
secondaryContainer: Color(0xffecf0ff),
onSecondaryContainer: Color(0xff001b3d),
tertiary: Color(0xff7b4d88),
onTertiary: Color(0xfffffbff),
tertiaryContainer: Color(0xfffad7ff),
onTertiaryContainer: Color(0xff310540),
error: Color(0xffba1a1a),
onError: Color(0xfffffbff),
errorContainer: Color(0xffffdad6),
onErrorContainer: Color(0xff410002),
background: Color(0xfffcfafe),
onBackground: Color(0xff191c20),
surface: Color(0xfffdfbff),
onSurface: Color(0xff191c20),
surfaceVariant: Color(0xffdfe2ef),
onSurfaceVariant: Color(0xff424751),
outline: Color(0xff737782),
outlineVariant: Color(0xffc2c6d2),
shadow: Color(0xff000000),
scrim: Color(0xff000000),
inverseSurface: Color(0xff2e3036),
onInverseSurface: Color(0xfff0f0f7),
inversePrimary: Color(0xffa9c7ff),
surfaceTint: Color(0xff00468c),
);
static const ColorScheme _blueDark = ColorScheme(
brightness: Brightness.dark,
primary: Color(0xffa9c7ff),
onPrimary: Color(0xff001b3d),
primaryContainer: Color(0xff00468c),
onPrimaryContainer: Color(0xffd6e3ff),
secondary: Color(0xffd6e3ff),
onSecondary: Color(0xff001b3d),
secondaryContainer: Color(0xff003063),
onSecondaryContainer: Color(0xffd6e3ff),
tertiary: Color(0xffeab4f6),
onTertiary: Color(0xff310540),
tertiaryContainer: Color(0xff61356e),
onTertiaryContainer: Color(0xfffad7ff),
error: Color(0xffffb4ab),
onError: Color(0xff410002),
errorContainer: Color(0xff93000a),
onErrorContainer: Color(0xffffb4ab),
background: Color(0xff1a1d21),
onBackground: Color(0xffe2e2e9),
surface: Color(0xff1a1e22),
onSurface: Color(0xffe2e2e9),
surfaceVariant: Color(0xff424852),
onSurfaceVariant: Color(0xffc2c6d2),
outline: Color(0xff8c919c),
outlineVariant: Color(0xff424751),
shadow: Color(0xff000000),
scrim: Color(0xff000000),
inverseSurface: Color(0xffe1e1e9),
onInverseSurface: Color(0xff2e3036),
inversePrimary: Color(0xff005db7),
surfaceTint: Color(0xffa9c7ff),
);
static ThemeData getThemeForColorScheme(ColorScheme color) {
return ThemeData(
primaryColor: color.primary,
iconTheme: const IconThemeData(weight: 400),
);
}
}
@@ -0,0 +1,39 @@
import 'package:dynamic_color/dynamic_color.dart';
import 'package:flutter/material.dart';
import 'package:immich_mobile/presentation/theme/utils/colors.dart';
class AppThemeBuilder extends StatelessWidget {
const AppThemeBuilder({
super.key,
required this.theme,
required this.builder,
});
/// Current app theme to switch the theme data used
final AppTheme theme;
/// Builds the child widget of this widget, providing a light and dark [ThemeData] based on the
/// [theme] passed.
final Widget Function(ThemeData lightTheme, ThemeData darkTheme) builder;
@override
Widget build(BuildContext context) {
// Static colors
if (theme != AppTheme.dynamic) {
final lightTheme = AppColors.getThemeForColorScheme(theme.lightSchema);
final darkTheme = AppColors.getThemeForColorScheme(theme.darkSchema);
return builder(lightTheme, darkTheme);
}
// Dynamic color builder
return DynamicColorBuilder(builder: (lightDynamic, darkDynamic) {
final lightTheme =
AppColors.getThemeForColorScheme(lightDynamic ?? theme.lightSchema);
final darkTheme =
AppColors.getThemeForColorScheme(darkDynamic ?? theme.darkSchema);
return builder(lightTheme, darkTheme);
});
}
}