mirror of
https://github.com/venera-app/venera.git
synced 2025-09-27 07:47:24 +00:00
[Android] Turn page by volume keys
This commit is contained in:
@@ -117,6 +117,7 @@ class _Settings with ChangeNotifier {
|
||||
'webdav': [], // empty means not configured
|
||||
'dataVersion': 0,
|
||||
'quickFavorite': null,
|
||||
'enableTurnPageByVolumeKey': true,
|
||||
};
|
||||
|
||||
operator [](String key) {
|
||||
|
@@ -23,6 +23,7 @@ import 'package:venera/pages/settings/settings_page.dart';
|
||||
import 'package:venera/utils/file_type.dart';
|
||||
import 'package:venera/utils/io.dart';
|
||||
import 'package:venera/utils/translations.dart';
|
||||
import 'package:venera/utils/volume.dart';
|
||||
import 'package:window_manager/window_manager.dart';
|
||||
|
||||
part 'scaffold.dart';
|
||||
@@ -97,6 +98,8 @@ class _ReaderState extends State<Reader> with _ReaderLocation, _ReaderWindow {
|
||||
|
||||
var focusNode = FocusNode();
|
||||
|
||||
VolumeListener? volumeListener;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
page = widget.initialPage ?? 1;
|
||||
@@ -107,6 +110,9 @@ class _ReaderState extends State<Reader> with _ReaderLocation, _ReaderWindow {
|
||||
updateHistory();
|
||||
});
|
||||
SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersive);
|
||||
if(appdata.settings['enableTurnPageByVolumeKey']) {
|
||||
handleVolumeEvent();
|
||||
}
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@@ -115,6 +121,7 @@ class _ReaderState extends State<Reader> with _ReaderLocation, _ReaderWindow {
|
||||
autoPageTurningTimer?.cancel();
|
||||
focusNode.dispose();
|
||||
SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
|
||||
stopVolumeEvent();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@@ -152,6 +159,31 @@ class _ReaderState extends State<Reader> with _ReaderLocation, _ReaderWindow {
|
||||
HistoryManager().addHistory(history!);
|
||||
}
|
||||
}
|
||||
|
||||
void handleVolumeEvent() {
|
||||
if(!App.isAndroid) {
|
||||
// Currently only support Android
|
||||
return;
|
||||
}
|
||||
if(volumeListener != null) {
|
||||
volumeListener?.cancel();
|
||||
}
|
||||
volumeListener = VolumeListener(
|
||||
onDown: () {
|
||||
toNextPage();
|
||||
},
|
||||
onUp: () {
|
||||
toPrevPage();
|
||||
},
|
||||
)..listen();
|
||||
}
|
||||
|
||||
void stopVolumeEvent() {
|
||||
if(volumeListener != null) {
|
||||
volumeListener?.cancel();
|
||||
volumeListener = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
abstract mixin class _ReaderLocation {
|
||||
|
@@ -470,6 +470,13 @@ class _ReaderScaffoldState extends State<_ReaderScaffold> {
|
||||
context.reader.mode = ReaderMode.fromKey(appdata.settings[key]);
|
||||
App.rootContext.pop();
|
||||
}
|
||||
if (key == "enableTurnPageByVolumeKey") {
|
||||
if(appdata.settings[key]) {
|
||||
context.reader.handleVolumeEvent();
|
||||
} else {
|
||||
context.reader.stopVolumeEvent();
|
||||
}
|
||||
}
|
||||
context.reader.update();
|
||||
},
|
||||
),
|
||||
|
@@ -69,6 +69,14 @@ class _ReaderSettingsState extends State<ReaderSettings> {
|
||||
widget.onChanged?.call('limitImageWidth');
|
||||
},
|
||||
).toSliver(),
|
||||
if(App.isAndroid)
|
||||
_SwitchSetting(
|
||||
title: 'Turn page by volume key'.tl,
|
||||
settingKey: 'enableTurnPageByVolumeKey',
|
||||
onChanged: () {
|
||||
widget.onChanged?.call('enableTurnPageByVolumeKey');
|
||||
},
|
||||
).toSliver(),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
31
lib/utils/volume.dart
Normal file
31
lib/utils/volume.dart
Normal file
@@ -0,0 +1,31 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
class VolumeListener {
|
||||
static const channel = EventChannel('venera/volume');
|
||||
|
||||
void Function()? onUp;
|
||||
|
||||
void Function()? onDown;
|
||||
|
||||
VolumeListener({this.onUp, this.onDown});
|
||||
|
||||
StreamSubscription? stream;
|
||||
|
||||
void listen() {
|
||||
stream = channel.receiveBroadcastStream().listen(onEvent);
|
||||
}
|
||||
|
||||
void onEvent(event) {
|
||||
if (event == 1) {
|
||||
onUp!();
|
||||
} else if (event == 2) {
|
||||
onDown!();
|
||||
}
|
||||
}
|
||||
|
||||
void cancel() {
|
||||
stream?.cancel();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user