forked from firka/firka
firka_wear: add rotary support
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package app.firka.naplo
|
||||
|
||||
import android.view.MotionEvent
|
||||
import android.app.NotificationChannel
|
||||
import android.app.NotificationManager
|
||||
import android.app.PendingIntent
|
||||
@@ -10,6 +11,7 @@ import android.os.Bundle
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.wear.ongoing.OngoingActivity
|
||||
import androidx.wear.ongoing.Status
|
||||
import com.samsung.wearable_rotary.WearableRotaryPlugin
|
||||
import io.flutter.embedding.android.FlutterActivity
|
||||
import io.flutter.embedding.engine.FlutterEngine
|
||||
import io.flutter.plugin.common.MethodChannel
|
||||
@@ -89,4 +91,10 @@ class MainActivity : FlutterActivity() {
|
||||
window.clearFlags(android.view.WindowManager.LayoutParams.FLAG_SECURE)
|
||||
}
|
||||
|
||||
override fun onGenericMotionEvent(event: MotionEvent?): Boolean {
|
||||
return when {
|
||||
WearableRotaryPlugin.onGenericMotionEvent(event) -> true
|
||||
else -> super.onGenericMotionEvent(event)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
||||
import 'package:kreta_api/kreta_api.dart';
|
||||
import 'package:watch_connectivity/watch_connectivity.dart';
|
||||
import 'package:wearable_rotary/wearable_rotary.dart';
|
||||
import 'package:wear_plus/wear_plus.dart';
|
||||
|
||||
import 'package:firka_wear/app/app_state.dart';
|
||||
@@ -45,6 +46,7 @@ class _WearHomeScreenState extends State<WearHomeScreen> {
|
||||
final platform = MethodChannel('firka.app/main');
|
||||
final watch = WatchConnectivity();
|
||||
StreamSubscription? _messageSub;
|
||||
StreamSubscription<RotaryEvent>? _rotarySub;
|
||||
WearSyncCubit? _syncCubit;
|
||||
late final PageController _pageController;
|
||||
|
||||
@@ -79,9 +81,28 @@ class _WearHomeScreenState extends State<WearHomeScreen> {
|
||||
now = timeNow();
|
||||
});
|
||||
});
|
||||
_rotarySub = rotaryEvents.listen(_onRotaryEvent);
|
||||
initStateAsync();
|
||||
}
|
||||
|
||||
void _onRotaryEvent(RotaryEvent event) {
|
||||
if (!_pageController.hasClients) return;
|
||||
final pos = _pageController.position;
|
||||
final pageCount = (pos.maxScrollExtent / pos.viewportDimension).round() + 1;
|
||||
final currentPage = (_pageController.page ?? 0).round();
|
||||
final nextPage = event.direction == RotaryDirection.clockwise
|
||||
? (currentPage + 1).clamp(0, pageCount - 1)
|
||||
: (currentPage - 1).clamp(0, pageCount - 1);
|
||||
if (nextPage != currentPage) {
|
||||
HapticFeedback.lightImpact();
|
||||
_pageController.animateToPage(
|
||||
nextPage,
|
||||
duration: const Duration(milliseconds: 250),
|
||||
curve: Curves.easeInOutCirc,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void _onSyncData(Map<String, dynamic> msg) async {
|
||||
if (disposed) return;
|
||||
_syncCubit?.setSyncing(true);
|
||||
@@ -624,6 +645,7 @@ class _WearHomeScreenState extends State<WearHomeScreen> {
|
||||
@override
|
||||
void dispose() {
|
||||
_messageSub?.cancel();
|
||||
_rotarySub?.cancel();
|
||||
timer?.cancel();
|
||||
_pageController.dispose();
|
||||
disposed = true;
|
||||
|
||||
@@ -45,6 +45,7 @@ dependencies:
|
||||
carousel_slider: ^5.0.0
|
||||
dart_jsonwebtoken: ^3.2.0
|
||||
wear_plus: ^1.2.4
|
||||
wearable_rotary: ^2.0.4
|
||||
majesticons_flutter: ^0.0.1
|
||||
watch_connectivity: ^0.2.1+1
|
||||
permission_handler: ^12.0.1
|
||||
|
||||
Reference in New Issue
Block a user