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

General Refactoring #196

Merged
merged 2 commits into from
Mar 12, 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
6 changes: 3 additions & 3 deletions app/lib/app/services/app_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ final class AppService {
final PreferencesStorage storage;

Locale get init {
final code = storage.readString(key: AppConst.localeKey);
final code = storage.readString(key: StorageKeys.localeKey);
if (code != null) return Locale(code);
// ignore: deprecated_member_use
final deviceLocal = window.locale.languageCode;
return AppLocalizations.delegate.isSupported(Locale(deviceLocal)) ? Locale(deviceLocal) : const Locale('en');
return AppLocalizationHelper.getSupportedLocale(deviceLocal);
}

Future<Locale> setLocale(String langKey) async {
await storage.writeString(key: AppConst.localeKey, value: langKey);
await storage.writeString(key: StorageKeys.localeKey, value: langKey);
return Locale(langKey);
}
}
19 changes: 11 additions & 8 deletions app/lib/app/services/auth_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ final class AuthService {
final RemoteClient client;

User? get init {
final userToken = storage.readString(key: AppConst.tokenKey);
final userGender = storage.readString(key: AppConst.genderKey);
final username = storage.readString(key: AppConst.usernameKey);
final userToken = storage.readString(key: StorageKeys.tokenKey);
final userGender = storage.readString(key: StorageKeys.genderKey);
final username = storage.readString(key: StorageKeys.usernameKey);
if (userToken == null && userGender == null && username == null) return null;
return User(
accessToken: userToken!,
Expand All @@ -25,7 +25,7 @@ final class AuthService {
);
}

String? getToken() => storage.readString(key: AppConst.tokenKey);
String? getToken() => storage.readString(key: StorageKeys.tokenKey);

Future<Either<User, Exception>> login(String languageCode, Gender gender) async {
final user = await client.post<User>(
Expand All @@ -41,15 +41,18 @@ final class AuthService {
Left.new,
(r) async {
final user = r.copyWith(gender: gender);
await storage.writeString(key: AppConst.tokenKey, value: user.accessToken);
await storage.writeString(key: AppConst.genderKey, value: user.gender!.name);
await storage.writeString(key: AppConst.usernameKey, value: user.username);
await Future.wait([
storage.writeString(key: StorageKeys.tokenKey, value: user.accessToken),
storage.writeString(key: StorageKeys.genderKey, value: user.gender!.name),
storage.writeString(key: StorageKeys.usernameKey, value: user.username),
]);

return Right(user);
},
);
}

Future<void> changeGender(Gender gender) async {
await storage.writeString(key: AppConst.genderKey, value: gender.name);
await storage.writeString(key: StorageKeys.genderKey, value: gender.name);
}
}
12 changes: 6 additions & 6 deletions app/lib/app/services/theme_service.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import 'package:flutter/material.dart';
import 'package:mq_storage/mq_storage.dart';
import 'package:my_quran/constants/contants.dart';

import 'package:my_quran/theme/theme.dart';

class ThemeService {
@immutable
final class ThemeService {
const ThemeService(this.storage);

final PreferencesStorage storage;

CustomTheme get init {
final isDark = storage.readBool(key: AppConst.modeKey);
final cachedColorIndex = storage.readInt(key: AppConst.colorKey);
final isDark = storage.readBool(key: StorageKeys.modeKey);
final cachedColorIndex = storage.readInt(key: StorageKeys.colorKey);
return switch (isDark) {
true => CustomTheme(Brightness.dark, _getColor(cachedColorIndex)),
_ => CustomTheme(Brightness.light, _getColor(cachedColorIndex)),
Expand All @@ -23,10 +23,10 @@ class ThemeService {
}

Future<void> setColor(int index) async {
await storage.writeInt(key: AppConst.colorKey, value: index);
await storage.writeInt(key: StorageKeys.colorKey, value: index);
}

Future<void> setMode({required bool isDark}) async {
await storage.writeBool(key: AppConst.modeKey, value: isDark);
await storage.writeBool(key: StorageKeys.modeKey, value: isDark);
}
}
14 changes: 8 additions & 6 deletions app/lib/app_observer.dart
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
import 'dart:developer';

import 'package:flutter_bloc/flutter_bloc.dart';

class AppBlocObserver extends BlocObserver {
const AppBlocObserver({this.onLog});

final void Function(String log)? onLog;

@override
void onCreate(BlocBase<dynamic> bloc) {
super.onCreate(bloc);
log('onCreate(${bloc.state})');
onLog?.call('onCreate(${bloc.state})');
}

@override
void onChange(BlocBase<dynamic> bloc, Change<dynamic> change) {
super.onChange(bloc, change);
log('onChange(${bloc.runtimeType}, $change)');
onLog?.call('onChange(${bloc.runtimeType}, $change)');
}

@override
void onError(BlocBase<dynamic> bloc, Object error, StackTrace stackTrace) {
log('onError(${bloc.runtimeType}, $error, $stackTrace)');
onLog?.call('onError(${bloc.runtimeType}, $error, $stackTrace)');
super.onError(bloc, error, stackTrace);
}

@override
void onClose(BlocBase<dynamic> bloc) {
super.onClose(bloc);
log('onClose(${bloc.runtimeType})');
onLog?.call('onClose(${bloc.runtimeType})');
}
}
9 changes: 4 additions & 5 deletions app/lib/components/card/select_lang_list.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:mq_ci_keys/mq_ci_keys.dart';

import 'package:my_quran/app/app.dart';
import 'package:my_quran/constants/contants.dart';
import 'package:my_quran/l10n/l10.dart';

class SelectLangFromListViewBuilder extends StatelessWidget {
const SelectLangFromListViewBuilder({super.key});
Expand All @@ -13,10 +12,10 @@ class SelectLangFromListViewBuilder extends StatelessWidget {
final appCubit = context.watch<AppCubit>();
final colorScheme = Theme.of(context).colorScheme;
return ListView.builder(
itemCount: AppConst.locales.length,
itemCount: AppLocalizationHelper.locales.length,
itemBuilder: (BuildContext context, int index) {
final locale = AppConst.locales[index];
final langName = AppConst.getName(locale.toLanguageTag());
final locale = AppLocalizationHelper.locales[index];
final langName = AppLocalizationHelper.getName(locale.toLanguageTag());
return Card(
child: ListTile(
key: Key(MqKeys.languageCode(locale.languageCode)),
Expand Down
84 changes: 0 additions & 84 deletions app/lib/constants/app/app_const.dart

This file was deleted.

3 changes: 2 additions & 1 deletion app/lib/constants/contants.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export 'api/api_const.dart';
export 'app/app_const.dart';
export 'assets/assets.gen.dart';
export 'assets/fonts.gen.dart';
export 'keys/storage_keys.dart';
export 'static/app_static.dart';
31 changes: 31 additions & 0 deletions app/lib/constants/keys/storage_keys.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:meta/meta.dart';
import 'package:my_quran/config/config.dart';

@immutable
final class StorageKeys {
const StorageKeys._();

static String get localeKey => apiConst.isDevmode ? _localeKeyDev : _localeKey;
static String get tokenKey => apiConst.isDevmode ? _tokenKeyDev : _tokenKey;
static String get genderKey => apiConst.isDevmode ? _genderKeyDev : _genderKey;
static String get usernameKey => apiConst.isDevmode ? _usernameKeyDev : _usernameKey;
static String get modeKey => apiConst.isDevmode ? _modeKeyDev : _modeKey;
static String get readThemeKey => apiConst.isDevmode ? _readThemeKeyDev : _readThemeKey;
static String get colorKey => apiConst.isDevmode ? _colorKeyDev : _colorKey;

static const _localeKey = 'locale';
static const _tokenKey = 'token';
static const _genderKey = 'gender';
static const _usernameKey = 'username';
static const _modeKey = 'mode';
static const _readThemeKey = 'readThemeKey';
static const _colorKey = 'color';

static const _localeKeyDev = 'locale-dev';
static const _tokenKeyDev = 'token-dev';
static const _genderKeyDev = 'gender-dev';
static const _usernameKeyDev = 'username-dev';
static const _modeKeyDev = 'mode-dev';
static const _readThemeKeyDev = 'readThemeKey-dev';
static const _colorKeyDev = 'color-dev';
}
46 changes: 46 additions & 0 deletions app/lib/constants/static/app_static.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import 'package:meta/meta.dart';

@immutable
final class AppStatic {
const AppStatic._();

static const bismallah = 'بِسْمِ ٱللَّهِ ٱلرَّحْمَـٰنِ ٱلرَّحِيمِ';
static const sajdaSymbol = '۩';
static const sajdaAyats = [
1160,
1722,
1951,
2138,
2308,
2613,
2672,
2915,
3185,
3518,
3994,
4256,
4846,
5905,
6125,
];

/*
yes - https://api.quran.com/api/v4/quran/verses/uthmanit?page_number=1 has sajda symbol ۩
correct - check from mushaf
1. 1160(ayat id) - 176 page - аль-Араф:206 (yes) (correct)
2. 1722(ayat id) - 251 page - ар-Рад:15 (yes) (correct)
3. 1951(ayat id) - 272 page - ан-Нахль:50 (yes) (correct)
4. 2138(ayat id) - 293 page - аль-Исра:109 (yes) (correct)
5. 2308(ayat id) - 309 page - Марьям:58 (yes) (correct)
6. 2613(ayat id) - 334 page - аль-Хадж:18 (yes) (correct)
7. 2672(ayat id) - 341 page - аль-Хадж:77 (yes) (correct)
8. 2915(ayat id) - 365 page - аль-Фуркан:60 (yes) (correct)
9. 3185(ayat id) - 379 page - ан-Намль:26 (yes) (correct)
10. 3518(ayat id) - 416 page - ас-Сажда:15 (yes) (correct)
11. 3994(ayat id) - 454 page - Сад:24 (yes) (correct)
12. 4256(ayat id) - 480 page - Фуссилят:38 (yes) (correct)
13. 4846(ayat id) - 528 page - ан-Наджм:62 (yes) (correct)
14. 5905(ayat id) - 589 page - аль-Иншикак:21 (yes) (correct)
16. 6125(ayat id) - 597 page - аль-Аляк:19 (yes) (correct)
*/
}
2 changes: 1 addition & 1 deletion app/lib/core/client/remote_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class RemoteClient {

/// if an Unauthorized return Authentication Exception
} else if (response.statusCode == 401) {
return const Left(AuthenticationException());
return const Left(AuthenticationExc());
} else {
/// if the response is not successful and unauthorized, it will return a server exception
return const Left(ServerExc('server exception'));
Expand Down
2 changes: 1 addition & 1 deletion app/lib/core/core.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export 'client/network_client.dart';
export 'client/remote_client.dart';
export 'either/either.dart';
export 'exceptions/network_exception.dart';
export 'exceptions/server_exception.dart';
export 'exceptions/auth_exception.dart';
export 'exceptions/convert_exception.dart';
export 'launch/app_launch.dart';
export 'interface/either.dart';
export 'enums/fetch_status.dart';
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ abstract class Either<R, L> {
/// }
/// ```
@immutable
class Right<R, L> extends Either<R, L> {
final class Right<R, L> extends Either<R, L> {
const Right(this.r);

/// A field is defined to hold the right value.
Expand All @@ -62,6 +62,7 @@ class Right<R, L> extends Either<R, L> {
/// Equality check is defined for operator == and hashCode is computed for hashing.
@override
bool operator ==(Object other) => other is Right && other.r == r;

@override
int get hashCode => r.hashCode;
}
Expand All @@ -81,7 +82,7 @@ class Right<R, L> extends Either<R, L> {
/// }
/// ```
@immutable
class Left<R, L> extends Either<R, L> {
final class Left<R, L> extends Either<R, L> {
const Left(this.l);

/// A field is defined to hold the left value.
Expand All @@ -94,6 +95,7 @@ class Left<R, L> extends Either<R, L> {
/// Equality check is defined for operator == and hashCode is computed for hashing.
@override
bool operator ==(Object other) => other is Left && other.l == l;

@override
int get hashCode => l.hashCode;
}
8 changes: 5 additions & 3 deletions app/lib/core/exceptions/auth_exception.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/// Auhtentication Exception
class AuthenticationException implements Exception {
const AuthenticationException({this.message});
import 'package:meta/meta.dart';

@immutable
final class AuthenticationExc implements Exception {
const AuthenticationExc({this.message});

final String? message;

Expand Down
Loading
Loading