Skip to content

Commit

Permalink
Merge pull request #170 from Eldar2021/el-dev-mode
Browse files Browse the repository at this point in the history
Set dev mode
  • Loading branch information
Eldar2021 authored Oct 20, 2023
2 parents ebaa63f + 20586e3 commit 6301db2
Show file tree
Hide file tree
Showing 21 changed files with 236 additions and 79 deletions.
2 changes: 2 additions & 0 deletions app/lib/app/router/app_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class AppRouter {
static const String aboutUs = '/about-us';
static const String contactUs = '/contect-us';
static const String developers = '/developers';
static const String devModeView = '/dev-mode-view';

static Route<void> onGenerateRoute(RouteSettings settings, User? user) {
return switch (settings.name) {
Expand All @@ -37,6 +38,7 @@ class AppRouter {
genderSettings => CupertinoPageRoute(builder: (_) => const GenderSettingView()),
langSettings => CupertinoPageRoute(builder: (_) => const LangSettingsView()),
themeSettings => CupertinoPageRoute(builder: (_) => const ThemeSettingsView()),
devModeView => CupertinoPageRoute(builder: (_) => const DevModeView()),
read => CupertinoPageRoute<bool>(
builder: (_) {
final args = settings.arguments!;
Expand Down
3 changes: 2 additions & 1 deletion app/lib/app/services/auth_service.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/cupertino.dart';

import 'package:mq_storage/mq_storage.dart';
import 'package:my_quran/config/config.dart';
import 'package:my_quran/constants/contants.dart';
import 'package:my_quran/core/core.dart';
import 'package:my_quran/models/models.dart';
Expand Down Expand Up @@ -28,7 +29,7 @@ final class AuthService {

Future<Either<User, Exception>> login(String languageCode, Gender gender) async {
final user = await client.post<User>(
ApiConst.signUp,
apiConst.signUp,
fromJson: User.fromJson,
body: <String, dynamic>{
'gender': gender.name.toUpperCase(),
Expand Down
32 changes: 31 additions & 1 deletion app/lib/config/app_config.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@
import 'package:flutter/foundation.dart';
import 'package:mq_storage/mq_storage.dart';
import 'package:my_quran/constants/contants.dart';

@immutable
final class AppConfig {
const AppConfig({this.isIntegrationTest = false});
const AppConfig({
this.storage,
this.isIntegrationTest = false,
});

final bool isIntegrationTest;

final PreferencesStorage? storage;

Future<void> setDevMode({required String devDomain, required bool isDevmode}) async {
await storage?.writeString(key: 'dev-domain', value: devDomain);
await storage?.writeBool(key: 'dev-mode', value: isDevmode);
}

Future<void> clearDevMode() async {
await storage?.delete(key: 'dev-domain');
await storage?.delete(key: 'dev-mode');
}

Future<void> changeJustDevMode({required bool isDevmode}) async {
await storage?.writeBool(key: 'dev-mode', value: isDevmode);
init();
}

void init() {
final devDomain = storage?.readString(key: 'dev-domain');
final isDevmode = storage?.readBool(key: 'dev-mode') ?? false;
apiConst = ApiConst(isDevmode: isDevmode, devDomain: devDomain);
}
}

ApiConst apiConst = const ApiConst(isDevmode: false);
66 changes: 37 additions & 29 deletions app/lib/constants/api/api_const.dart
Original file line number Diff line number Diff line change
@@ -1,33 +1,41 @@
class ApiConst {
static const domain = 'http://quran.isistant.io/api/v1';
static const signUp = '$domain/auth/sign_up';
static const home = '$domain/report/dashboard';
static const hatim = '$domain/hatim/join_to_hatim';

static const appLogoLink = 'https://github.com/Eldar2021/my_quran/blob/main/app/assets/images/app_icon.png?raw=true';

static const socketBase = 'ws://quran.isistant.io/ws';

static String juzSocket(String hatimId) => '/topic/$hatimId/list_of_juz';

static String userPages(String username) => '/topic/$username/user_pages';

static String getPagesByJuz(String juzId) => '/topic/$juzId/list_of_page';

static const setInProgress = '/app/in_progress';
static const setBook = '/app/book';
static const setTodo = '/app/to_do';
static const setDone = '/app/done';

static Map<String, String> authMap(String token) => {'Authorization': 'Bearer $token'};

static const urlGitHub = 'https://github.com/Eldar2021/my_quran';
const ApiConst({
required this.isDevmode,
this.devDomain,
});

final bool isDevmode;
final String? devDomain;

static const domain = 'quran.isistant.io';

String get baseUrl => 'http://$_getDomain/api/v1';
String get signUp => '$baseUrl/auth/sign_up';
String get home => '$baseUrl/report/dashboard';
String get hatim => '$baseUrl/hatim/join_to_hatim';

String get baseSocket => 'ws://$_getDomain/ws';
String juzSocket(String hatimId) => '/topic/$hatimId/list_of_juz';
String userPages(String username) => '/topic/$username/user_pages';
String getPagesByJuz(String juzId) => '/topic/$juzId/list_of_page';
String get setInProgress => '/app/in_progress';
String get setBook => '/app/book';
String get setTodo => '/app/to_do';
String get setDone => '/app/done';

Map<String, String> authMap(String token) => {'Authorization': 'Bearer $token'};

String get _getDomain {
if (isDevmode && devDomain != null && devDomain!.isNotEmpty) return devDomain!;
return domain;
}

String verse(int page, String quranFmt) => 'https://api.quran.com/api/v4/quran/verses/$quranFmt?page_number=$page';
String audio(String surahIndex) =>
'https://download.quranicaudio.com/quran/mishaari_raashid_al_3afaasee/$surahIndex.mp3';

String get urlGitHub => 'https://github.com/Eldar2021/my_quran';
// TODO(eldiiar): Replace the privacy policy URL with the actual [URL] of your privacy policy page.
static const provicyPolicy = 'https://github.com/Eldar2021/my_quran';

static String verse(int page, String quranFmt) =>
'https://api.quran.com/api/v4/quran/verses/$quranFmt?page_number=$page';
static String audio(String surahIndex) =>
'https://download.quranicaudio.com/quran/mishaari_raashid_al_3afaasee/$surahIndex.mp3';
String get provicyPolicy => 'https://github.com/Eldar2021/my_quran';
String get appLogoLink => 'https://github.com/Eldar2021/my_quran/blob/main/app/assets/images/app_icon.png?raw=true';
}
31 changes: 24 additions & 7 deletions app/lib/constants/app/app_const.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
import 'package:flutter/material.dart';
import 'package:my_quran/config/app_config.dart';

class AppConst {
const AppConst._();

static const bismallah = 'بِسْمِ ٱللَّهِ ٱلرَّحْمَـٰنِ ٱلرَّحِيمِ';

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 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';

static const locales = <Locale>[
Locale('en'),
Expand Down
6 changes: 5 additions & 1 deletion app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import 'package:my_quran/app_observer.dart';
import 'package:my_quran/config/config.dart';
import 'package:my_quran/core/core.dart';

Future<void> main({AppConfig appConfig = const AppConfig()}) async {
Future<void> main({AppConfig? appConfig}) async {
WidgetsFlutterBinding.ensureInitialized();
FlutterError.onError = (details) {
log(details.exceptionAsString(), stackTrace: details.stack);
Expand All @@ -28,6 +28,10 @@ Future<void> main({AppConfig appConfig = const AppConfig()}) async {
Bloc.observer = AppBlocObserver();
final storage = await PreferencesStorage.getInstance();

appConfig ??= AppConfig(storage: storage);

appConfig.init();

runApp(
MultiRepositoryProvider(
providers: [
Expand Down
1 change: 1 addition & 0 deletions app/lib/modules/dev_mode/dev_mode.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export 'view/dev_mode_view.dart';
65 changes: 65 additions & 0 deletions app/lib/modules/dev_mode/view/dev_mode_view.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:my_quran/config/app_config.dart';
import 'package:my_quran/utils/show/alerts.dart';

class DevModeView extends StatefulWidget {
const DevModeView({super.key});

@override
State<DevModeView> createState() => _DevModeViewState();
}

class _DevModeViewState extends State<DevModeView> {
final _controller = TextEditingController();
bool isDevMode = false;

@override
void initState() {
_controller.text = apiConst.devDomain ?? '';
isDevMode = apiConst.isDevmode;
super.initState();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Enable/Disable dev-mode'),
),
body: ListView(
padding: const EdgeInsets.symmetric(horizontal: 20),
children: [
const SizedBox(height: 30),
TextFormField(controller: _controller),
const SizedBox(height: 20),
Card(
child: SwitchListTile.adaptive(
title: const Text('Enable/Disable dev-mode'),
value: isDevMode,
onChanged: (v) {
setState(() {
isDevMode = v;
});
},
),
),
const SizedBox(height: 40),
ElevatedButton(
onPressed: () async {
final appConfig = context.read<AppConfig>();
await appConfig.setDevMode(
devDomain: _controller.text,
isDevmode: isDevMode,
);
if (context.mounted) {
AppAlert.showRestartDialog(context);
}
},
child: const Text('Save'),
),
],
),
);
}
}
16 changes: 8 additions & 8 deletions app/lib/modules/hatim/logic/hatim_juz/hatim_juz_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:stomp_dart_client/stomp.dart';
import 'package:stomp_dart_client/stomp_config.dart';
import 'package:stomp_dart_client/stomp_frame.dart';

import 'package:my_quran/constants/contants.dart';
import 'package:my_quran/config/config.dart';
import 'package:my_quran/core/core.dart';
import 'package:my_quran/models/models.dart';

Expand All @@ -23,7 +23,7 @@ class HatimJuzCubit extends Cubit<HatimJuzState> {
dynamic connect(String hatimId, String token) {
client = StompClient(
config: StompConfig(
url: ApiConst.socketBase,
url: apiConst.baseSocket,
onStompError: onStompError,
onWebSocketError: onWebSocketError,
onConnect: (event) => onConnect(hatimId, token),
Expand Down Expand Up @@ -54,24 +54,24 @@ class HatimJuzCubit extends Cubit<HatimJuzState> {

void onConnect(String juzId, String token) {
client.subscribe(
destination: ApiConst.getPagesByJuz(juzId),
headers: ApiConst.authMap(token),
destination: apiConst.getPagesByJuz(juzId),
headers: apiConst.authMap(token),
callback: callback,
);
}

void selectPage(String pageId, String token, String username) {
client.send(
destination: ApiConst.setBook,
headers: ApiConst.authMap(token),
destination: apiConst.setBook,
headers: apiConst.authMap(token),
body: jsonEncode({'pageId': pageId, 'username': username}),
);
}

void unSelectPage(String pageId, String token, String username) {
client.send(
destination: ApiConst.setTodo,
headers: ApiConst.authMap(token),
destination: apiConst.setTodo,
headers: apiConst.authMap(token),
body: jsonEncode({'pageId': pageId, 'username': username}),
);
}
Expand Down
8 changes: 4 additions & 4 deletions app/lib/modules/hatim/logic/hatim_juzs/hatim_juzs_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:stomp_dart_client/stomp.dart';
import 'package:stomp_dart_client/stomp_config.dart';
import 'package:stomp_dart_client/stomp_frame.dart';

import 'package:my_quran/constants/contants.dart';
import 'package:my_quran/config/config.dart';
import 'package:my_quran/models/hatim/hatim_juz.dart';

part 'hatim_juzs_state.dart';
Expand All @@ -19,7 +19,7 @@ class HatimJuzsCubit extends Cubit<HatimJuzsState> {
dynamic connect(String hatimId, String token) {
client = StompClient(
config: StompConfig(
url: ApiConst.socketBase,
url: apiConst.baseSocket,
onStompError: onStompError,
onWebSocketError: onWebSocketError,
onConnect: (event) => onConnect(hatimId, token),
Expand Down Expand Up @@ -49,8 +49,8 @@ class HatimJuzsCubit extends Cubit<HatimJuzsState> {

void onConnect(String hatimId, String token) {
client.subscribe(
destination: ApiConst.juzSocket(hatimId),
headers: ApiConst.authMap(token),
destination: apiConst.juzSocket(hatimId),
headers: apiConst.authMap(token),
callback: callback,
);
}
Expand Down
Loading

0 comments on commit 6301db2

Please sign in to comment.