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

Count with device volume keys #5

Merged
merged 8 commits into from
Sep 24, 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,49 @@
package com.hassaneltantawy.ruqayyah

import io.flutter.embedding.android.FlutterActivity
import android.view.KeyEvent
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel

class MainActivity: FlutterActivity() {
private lateinit var channel: MethodChannel
private var activateVolumeDispatch:Boolean = false

override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
channel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "volume_button_channel")
channel.setMethodCallHandler { call, _ ->
if (call.method == "activate_volumeBtn") {
activateVolumeDispatch = call.arguments as Boolean
}
}
}

override fun dispatchKeyEvent(event: KeyEvent): Boolean {
val action: Int = event.getAction()
val keyCode: Int = event.getKeyCode()
if (!activateVolumeDispatch){
return super.dispatchKeyEvent(event)
}
return when (keyCode) {
KeyEvent.KEYCODE_VOLUME_UP -> {
if (action == KeyEvent.ACTION_DOWN) {
channel.invokeMethod("volumeBtnPressed", "VOLUME_UP_DOWN")
} else if (action == KeyEvent.ACTION_UP) {
channel.invokeMethod("volumeBtnPressed", "VOLUME_UP_UP")
}
true
}
KeyEvent.KEYCODE_VOLUME_DOWN -> {
if (action == KeyEvent.ACTION_DOWN) {
channel.invokeMethod("volumeBtnPressed", "VOLUME_DOWN_DOWN")
} else if (action == KeyEvent.ACTION_UP) {
channel.invokeMethod("volumeBtnPressed", "VOLUME_DOWN_UP")
}
true
}

else -> super.dispatchKeyEvent(event)
}
}
}
2 changes: 1 addition & 1 deletion ruqayyah/lib/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class MyApp extends StatelessWidget {
onGenerateTitle: (context) => S.of(context).appTitle,
scrollBehavior: AppScrollBehavior(),
debugShowCheckedModeBanner: false,
locale: const Locale('ar', 'EG'),
locale: const Locale('ar'),
supportedLocales: S.delegate.supportedLocales,
localizationsDelegates: const [
S.delegate,
Expand Down
2 changes: 2 additions & 0 deletions ruqayyah/lib/generated/intl/messages_ar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class MessageLookup extends MessageLookupByLibrary {
"اهتزاز الهاتف عند انتهاء جميع الأذكار"),
"prayForUsAndParents":
MessageLookupByLibrary.simpleMessage("نسألكم الدعاء لنا ولوالدينا"),
"prefPraiseWithVolumeKeys":
MessageLookupByLibrary.simpleMessage("التسبيح بمفاتيح الصوت"),
"rukiaBookAuthor": MessageLookupByLibrary.simpleMessage(
"د. خالد بن عبدالرحمن الجريسي"),
"rukiaBookTitle": MessageLookupByLibrary.simpleMessage(
Expand Down
2 changes: 2 additions & 0 deletions ruqayyah/lib/generated/intl/messages_en.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class MessageLookup extends MessageLookupByLibrary {
"Phone vibration when all zikr end"),
"prayForUsAndParents": MessageLookupByLibrary.simpleMessage(
"Pray for us and our parents."),
"prefPraiseWithVolumeKeys":
MessageLookupByLibrary.simpleMessage("Praise with volume keys"),
"rukiaBookAuthor": MessageLookupByLibrary.simpleMessage(
"Dr. Khalid bin Abdulrahman Al-Juraisi"),
"rukiaBookTitle": MessageLookupByLibrary.simpleMessage(
Expand Down
10 changes: 10 additions & 0 deletions ruqayyah/lib/generated/l10n.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ruqayyah/lib/l10n/intl_ar.arb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"phoneVibrationAtSingleZikrEnd": "اهتزاز الهاتف عند انتهاء كل ذكر",
"phoneVibrationWhenAllZikrEnd": "اهتزاز الهاتف عند انتهاء جميع الأذكار",
"prayForUsAndParents": "نسألكم الدعاء لنا ولوالدينا",
"prefPraiseWithVolumeKeys": "التسبيح بمفاتيح الصوت",
"rukiaBookAuthor": "د. خالد بن عبدالرحمن الجريسي",
"rukiaBookTitle": "الرقية الشرعية من القرآن الكريم والسنة النبوية",
"ruqyahEtiquette": "آداب الرقية",
Expand Down
1 change: 1 addition & 0 deletions ruqayyah/lib/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"phoneVibrationAtSingleZikrEnd": "Phone vibration at single zikr end",
"phoneVibrationWhenAllZikrEnd": "Phone vibration when all zikr end",
"prayForUsAndParents": "Pray for us and our parents.",
"prefPraiseWithVolumeKeys": "Praise with volume keys",
"rukiaBookAuthor": "Dr. Khalid bin Abdulrahman Al-Juraisi",
"rukiaBookTitle": "Al-Ruqyah Al-Shar'iyyah from the Quran Al-Kareem and the Sunnah Al-Nabawiyyah",
"ruqyahEtiquette": "Ruqyah Etiquette",
Expand Down
2 changes: 2 additions & 0 deletions ruqayyah/lib/src/core/di/dependency_injection.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:get_it/get_it.dart';
import 'package:get_storage/get_storage.dart';
import 'package:ruqayyah/src/core/constants/const.dart';
import 'package:ruqayyah/src/core/utils/volume_button_manager.dart';
import 'package:ruqayyah/src/features/effects_manager/data/repository/effects_repo.dart';
import 'package:ruqayyah/src/features/effects_manager/presentation/controller/effect_manager.dart';
import 'package:ruqayyah/src/features/home/data/repository/ruki_db_helper.dart';
Expand All @@ -26,6 +27,7 @@ Future<void> initSL() async {

///MARK: Init Manager
sl.registerFactory(() => EffectsManager(sl()));
sl.registerFactory(() => VolumeButtonManager());

///MARK: Init BLOC

Expand Down
48 changes: 48 additions & 0 deletions ruqayyah/lib/src/core/utils/volume_button_manager.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import 'dart:io';

import 'package:flutter/services.dart';

class VolumeButtonManager {
static MethodChannel channel = const MethodChannel("volume_button_channel");

void listen({
Function()? onVolumeUpPressed,
Function()? onVolumeDownPressed,
Function()? onVolumeUpReleased,
Function()? onVolumeDownReleased,
}) {
channel.setMethodCallHandler(
(call) async {
if (call.method == "volumeBtnPressed") {
switch (call.arguments) {
case "VOLUME_UP_DOWN":
onVolumeUpPressed?.call();

case "VOLUME_DOWN_DOWN":
onVolumeDownPressed?.call();

case "VOLUME_UP_UP":
onVolumeUpReleased?.call();

case "VOLUME_DOWN_UP":
onVolumeDownReleased?.call();
}
}
},
);
}

Future<void> toggleActivation({required bool activate}) async {
if (Platform.isAndroid) {
await channel.invokeMethod(
'activate_volumeBtn',
activate,
);
}
}

void dispose() {
toggleActivation(activate: false);
channel.setMethodCallHandler(null);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'package:flutter/material.dart';
import 'package:ruqayyah/src/core/di/dependency_injection.dart';
import 'package:ruqayyah/src/core/utils/volume_button_manager.dart';
import 'package:ruqayyah/src/features/effects_manager/presentation/controller/effect_manager.dart';
import 'package:ruqayyah/src/features/home/data/models/rukia.dart';
import 'package:ruqayyah/src/features/home/data/models/rukia_type_enum.dart';
Expand Down Expand Up @@ -33,31 +34,52 @@ class _RukiaViewerScreenState extends State<RukiaViewerScreen> {
void initState() {
super.initState();

_pageController = PageController();
_pageController.addListener(_pageChange);

if (sl<AppSettingsRepo>().enableWakeLock) {
WakelockPlus.enable();
}
_loadData();
_start();
}

@override
void dispose() {
_pageController.dispose();
WakelockPlus.disable();
sl<VolumeButtonManager>().dispose();
super.dispose();
}

Future _loadData() async {
rukiasToView =
await sl<RukiaDBHelper>().getRukiaListByType(widget.rukiaType);
Future _start() async {
_pageController = PageController();
_pageController.addListener(_pageChange);

if (sl<AppSettingsRepo>().enableWakeLock) {
await WakelockPlus.enable();
}

sl<VolumeButtonManager>().toggleActivation(
activate: sl<AppSettingsRepo>().praiseWithVolumeKeys,
);

sl<VolumeButtonManager>().listen(
onVolumeUpPressed: () => _onTap(currentPage),
onVolumeDownPressed: () => _onTap(currentPage),
);

await _loadData();

setState(() {
isLoading = false;
});
}

Future _loadData() async {
rukiasToView =
await sl<RukiaDBHelper>().getRukiaListByType(widget.rukiaType);
}

Future _reset() async {
await _loadData();
_pageController.jumpTo(0);
setState(() {});
}

void _pageChange() {
setState(() {
currentPage = _pageController.page?.toInt() ?? 0;
Expand Down Expand Up @@ -98,7 +120,7 @@ class _RukiaViewerScreenState extends State<RukiaViewerScreen> {

@override
Widget build(BuildContext context) {
if (isLoading) return const SizedBox();
if (isLoading) return const Center(child: CircularProgressIndicator());

return Scaffold(
appBar: AppBar(
Expand Down Expand Up @@ -151,8 +173,7 @@ class _RukiaViewerScreenState extends State<RukiaViewerScreen> {
),
IconButton(
onPressed: () {
_pageController.jumpTo(0);
_loadData();
_reset();
},
icon: const Icon(Icons.repeat),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class SettingsCubit extends Cubit<SettingsState> {
if (activate) {
effectsManager.onCountSound();
}
await effectsRepo.changeSingleDoneSoundStatus(value: activate);
await effectsRepo.changeOnCountStatus(value: activate);
emit(
state.copyWith(
zikrEffects: state.zikrEffects.copyWith(soundOnCount: activate),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
appBar: AppBar(
centerTitle: true,
elevation: 0,
title: const Text(
"الإعدادات",
),
title: Text(S.of(context).settings),
),
body: ListView(
children: [
Expand Down Expand Up @@ -78,6 +76,16 @@ class SettingsGeneralSection extends StatelessWidget {
builder: (context, state) {
return Column(
children: [
SwitchListTile(
secondary: const Icon(Icons.volume_down),
value: state.praiseWithVolumeKeys,
title: Text(S.of(context).prefPraiseWithVolumeKeys),
onChanged: (value) {
context.read<SettingsCubit>().togglePraiseWithVolumeKeys(
use: !state.praiseWithVolumeKeys,
);
},
),
SwitchListTile(
secondary: const Icon(Icons.screenshot),
value: state.enableWakeLock,
Expand Down