Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move & rewrite Theme logic #1

Merged
merged 9 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assets/fonts/Inter-VariableFont_slnt,wght.ttf
Binary file not shown.
Binary file added assets/fonts/Lexend-VariableFont_wght.ttf
Binary file not shown.
37 changes: 37 additions & 0 deletions lib/src/_conf/adapters.hive.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import 'package:autojidelna/src/types/all.dart';
import 'package:autojidelna/src/types/theme.dart';
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';

class ThemeModeAdapter extends TypeAdapter<ThemeMode> {
@override
final typeId = 0; // Put an ID you didn't use yet.

@override
ThemeMode read(BinaryReader reader) => ThemeMode.values[reader.readInt()];

@override
void write(BinaryWriter writer, ThemeMode obj) => writer.writeInt(obj.index);
}

class ThemeStyleAdapter extends TypeAdapter<ThemeStyle> {
@override
final typeId = 1; // Put an ID you didn't use yet.

@override
ThemeStyle read(BinaryReader reader) => ThemeStyle.values[reader.readInt()];

@override
void write(BinaryWriter writer, ThemeStyle obj) => writer.writeInt(obj.index);
}

class DateFormatOptionsAdapter extends TypeAdapter<DateFormatOptions> {
@override
final typeId = 2; // Put an ID you didn't use yet.

@override
DateFormatOptions read(BinaryReader reader) => DateFormatOptions.values[reader.readInt()];

@override
void write(BinaryWriter writer, DateFormatOptions obj) => writer.writeInt(obj.index);
}
13 changes: 8 additions & 5 deletions lib/src/_conf/hive.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/// Hive boxes. Use these values to open a box.
class Boxes {
static const String settings = 'settings';
static const String appState = 'appState';
static const String theme = 'theme';
static const String settings = 'settings';
static const String cache = 'cache';
static const String statistics = 'statistics';
static const String notifications = 'notifications';
Expand All @@ -18,14 +19,16 @@ class HiveKeys {
static const String hideBurzaAlertDialog = 'hideBurzaAlertDialog';
static String location(String userName, String url) => 'location_${userName}_$url';

//settings box
static const String shouldAskForNotificationPermission = 'shouldAskForNotificationPermission';
static const String webNotificationsAccepted = 'webNotificationsAccepted';
// theme box
static const String themeMode = 'themeMode';
static const String themeStyle = 'themeStyle';
static const String amoledMode = 'amoledMode';

// settings box
static const String shouldAskForNotificationPermission = 'shouldAskForNotificationPermission';
static const String webNotificationsAccepted = 'webNotificationsAccepted';
static const String tabletUi = 'tabletUi';
static const String listUi = 'listUi';
static const String pureBlack = 'amoledMode';
static const String bigCalendarMarkers = 'bigCalendarMarkers';
static const String dateFormat = 'dateFormat';
static const String relTimeStamps = 'relativeTimeStamps';
Expand Down
6 changes: 0 additions & 6 deletions lib/src/_conf/notifications.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
// Purpose: stores constants used throughout the app.

import 'package:flutter/material.dart';

class Locales {
static Locale get csCZ => const Locale('cs', 'CZ');
}

class NotificationIds {
static String kreditChannel(String userName, String url) => 'kredit_channel_${userName}_$url';
static String objednanoChannel(String userName, String url) => 'objednano_channel_${userName}_$url';
Expand Down
8 changes: 7 additions & 1 deletion lib/src/_global/app.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:autojidelna/src/_conf/adapters.hive.dart';
import 'package:autojidelna/src/_conf/hive.dart';
import 'package:autojidelna/src/_conf/notifications.dart';
import 'package:autojidelna/src/_global/providers/remote_config.dart';
import 'package:autojidelna/src/lang/supported_locales.dart';
import 'package:autojidelna/src/logic/canteenwrapper.dart';
import 'package:autojidelna/src/logic/notifications.dart';
import 'package:autojidelna/src/types/all.dart';
Expand Down Expand Up @@ -147,6 +149,10 @@ class App {
if (_initHiveExecuted) return;

await Hive.initFlutter();
Hive.registerAdapter(ThemeModeAdapter());
Hive.registerAdapter(ThemeStyleAdapter());
Hive.registerAdapter(DateFormatOptionsAdapter());
await Hive.openBox(Boxes.theme);
await Hive.openBox(Boxes.settings);
await Hive.openBox(Boxes.cache);
await Hive.openBox(Boxes.appState);
Expand Down Expand Up @@ -174,7 +180,7 @@ class App {

static final remoteConfigProvider = Rmc();

static const defaultLocale = Locale('en');
static final defaultLocale = Locales.cs;

static const defaultRotations = [
DeviceOrientation.portraitUp,
Expand Down
66 changes: 66 additions & 0 deletions lib/src/_global/providers/theme.provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import 'dart:async';

import 'package:autojidelna/src/_conf/hive.dart';
import 'package:autojidelna/src/types/theme.dart';
import 'package:autojidelna/src/ui/theme/app_themes.dart';
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';

class ThemeProvider extends ChangeNotifier {
static Box box = Hive.box(Boxes.settings);

ThemeStyle _themeStyle = box.get(HiveKeys.themeStyle, defaultValue: ThemeStyle.defaultStyle);
ThemeMode _themeMode = box.get(HiveKeys.themeMode, defaultValue: ThemeMode.system);
bool _amoledMode = box.get(HiveKeys.amoledMode, defaultValue: false);

/// Theme style getter
ThemeStyle get themeStyle => _themeStyle;

/// Theme mode getter
ThemeMode get themeMode => _themeMode;

/// Pure black getter
bool get amoledMode => _amoledMode;

/// Setter for theme style
void setThemeStyle(ThemeStyle themeStyle) {
if (_themeStyle == themeStyle) return;
_themeStyle = themeStyle;
unawaited(box.put(HiveKeys.themeStyle, _themeStyle));
notifyListeners();
}

/// Setter for theme mode
void setThemeMode(ThemeMode themeMode) {
_themeMode = themeMode;
unawaited(box.put(HiveKeys.themeMode, _themeMode));
notifyListeners();
}

/// Setter for pure black
void setAmoledMode(bool isPureBlack) {
_amoledMode = isPureBlack;
unawaited(box.put(HiveKeys.amoledMode, _amoledMode));
notifyListeners();
}

ColorScheme colorSchemeLight(ThemeStyle themeStyle) {
ColorStyle colorStyle = AppThemes.colorStyles[themeStyle]!;

return AppThemes.colorSchemeLight.copyWith(
primary: colorStyle.primaryLight,
secondary: colorStyle.secondaryLight,
);
}

ColorScheme colorSchemeDark(ThemeStyle themeStyle) {
ColorStyle colorStyle = AppThemes.colorStyles[themeStyle]!;

return AppThemes.colorSchemeDark.copyWith(
primary: colorStyle.primaryDark,
secondary: colorStyle.secondaryDark,
surface: amoledMode ? Colors.black : const Color(0xff121212),
scrim: amoledMode ? Colors.black87 : Colors.black54,
);
}
}
7 changes: 7 additions & 0 deletions lib/src/lang/supported_locales.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'package:flutter/material.dart';

/// Class for supported Locales
class Locales {
static Locale get cs => const Locale('cs', 'CZ');
static Locale get en => const Locale('en', 'US');
}
3 changes: 2 additions & 1 deletion lib/src/logic/notifications.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:autojidelna/src/_conf/dates.dart';
import 'package:autojidelna/src/_conf/hive.dart';
import 'package:autojidelna/src/_conf/notifications.dart';
import 'package:autojidelna/src/lang/l10n_context_extension.dart';
import 'package:autojidelna/src/lang/supported_locales.dart';
import 'package:autojidelna/src/logic/canteenwrapper.dart';
import 'package:autojidelna/src/types/all.dart';
import 'package:awesome_notifications/awesome_notifications.dart';
Expand Down Expand Up @@ -152,7 +153,7 @@ Future<bool> initAwesome() async {

Future<void> doNotifications({bool force = false}) async {
//TODO: add more langueages
final lang = lookupTexts(const Locale('cs'));
final lang = lookupTexts(Locales.cs);
LoggedInCanteen loggedInCanteen = LoggedInCanteen();
LoginDataAutojidelna loginData = await loggedInCanteen.getLoginDataFromSecureStorage();
AwesomeNotifications().createNotification(
Expand Down
17 changes: 0 additions & 17 deletions lib/src/types/all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,6 @@ enum TypStatistiky {
burzaCatcher
}

/// Describes what colors will be used by the app
enum ThemeStyle {
defaultStyle,
plumBrown,
blueMauve,
rustOlive,
evergreenSlate,
crimsonEarth,
}

/// Describes what time format will be used by the app
enum DateFormatOptions {
dMy,
Expand All @@ -224,10 +214,3 @@ class Fonts {
static const String body = 'Inter';
static const String headings = 'Lexend';
}

enum TabletUi {
auto,
always,
landscape,
never,
}
25 changes: 25 additions & 0 deletions lib/src/types/theme.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:flutter/material.dart';

class ColorStyle {
Color primaryLight;
Color secondaryLight;
Color primaryDark;
Color secondaryDark;

ColorStyle({
required this.primaryLight,
required this.secondaryLight,
required this.primaryDark,
required this.secondaryDark,
});
}

/// Describes what ColorStyle will be used by the app
enum ThemeStyle {
defaultStyle,
plumBrown,
blueMauve,
rustOlive,
evergreenSlate,
crimsonEarth,
}
34 changes: 20 additions & 14 deletions lib/src/ui/material_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import 'dart:async';

import 'package:autojidelna/src/_conf/hive.dart';
import 'package:autojidelna/src/_global/app.dart';
import 'package:autojidelna/src/_global/providers/theme.provider.dart';
import 'package:autojidelna/src/_sentry/sentry.dart';
import 'package:autojidelna/src/lang/l10n_context_extension.dart';
import 'package:autojidelna/src/_routing/app_router.dart';
import 'package:autojidelna/src/logic/deep_link_transformer_logic.dart';
import 'package:autojidelna/src/ui/theme/app_themes.dart';
import 'package:flutter/material.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:provider/provider.dart';
Expand Down Expand Up @@ -51,20 +53,23 @@ class _MyAppState extends State<MyApp> {

@override
Widget build(BuildContext context) {
return MaterialApp.router(
supportedLocales: Texts.supportedLocales,
localizationsDelegates: Texts.localizationsDelegates,
locale: _locale,
debugShowCheckedModeBanner: false,
routerConfig: _appRouter.config(
navigatorObservers: () => [SentryNavigatorObserver(), SentryTabObserver()],
includePrefixMatches: true,
deepLinkTransformer: (uri) async => deepLinkTransformer(uri),
),
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
return Consumer<ThemeProvider>(
builder: (context, theme, ___) {
return MaterialApp.router(
debugShowCheckedModeBanner: false,
themeMode: theme.themeMode,
theme: AppThemes.theme(theme.colorSchemeLight(theme.themeStyle), theme.amoledMode),
darkTheme: AppThemes.theme(theme.colorSchemeDark(theme.themeStyle), theme.amoledMode),
locale: _locale,
supportedLocales: Texts.supportedLocales,
localizationsDelegates: Texts.localizationsDelegates,
routerConfig: _appRouter.config(
includePrefixMatches: true,
navigatorObservers: () => [SentryNavigatorObserver(), SentryTabObserver()],
deepLinkTransformer: (uri) async => deepLinkTransformer(uri),
),
);
},
);
}
}
Expand All @@ -77,6 +82,7 @@ class MyAppWrapper extends StatelessWidget {
return MultiProvider(
providers: [
ChangeNotifierProvider.value(value: App.remoteConfigProvider),
ChangeNotifierProvider(create: (_) => ThemeProvider()),
],
child: const MyApp(),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:auto_route/auto_route.dart';
import 'package:autojidelna/src/_global/app.dart';
import 'package:autojidelna/src/lang/l10n_context_extension.dart';
import 'package:autojidelna/src/lang/supported_locales.dart';
import 'package:flutter/material.dart';

@RoutePage()
Expand All @@ -18,11 +19,11 @@ class LocalizationsPage extends StatelessWidget {
child: Column(
children: [
ElevatedButton(
onPressed: () => App.translate(const Locale('en')),
onPressed: () => App.translate(Locales.en),
child: Text(lang.languageEnglish),
),
ElevatedButton(
onPressed: () => App.translate(const Locale('cs')),
onPressed: () => App.translate(Locales.cs),
child: Text(lang.languageCzech),
),
],
Expand Down
Loading