mirror of
https://github.com/Myzel394/quid_faciam_hodie.git
synced 2025-06-18 15:25:27 +02:00
added focus change listener for android
This commit is contained in:
parent
887c79467b
commit
7ef92fc932
@ -6,6 +6,7 @@
|
||||
<uses-permission android:name="android.permission.RECORD_AUDIO" />
|
||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
||||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
|
||||
<uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />
|
||||
|
||||
<application
|
||||
android:label="Quid faciam hodie?"
|
||||
|
@ -1,6 +1,37 @@
|
||||
package floss.myzel394.quid_faciam_hodie
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import io.flutter.embedding.android.FlutterActivity
|
||||
import io.flutter.embedding.engine.FlutterEngine
|
||||
import io.flutter.plugin.common.MethodChannel
|
||||
|
||||
|
||||
class MainActivity: FlutterActivity() {
|
||||
val CHANNEL_ID = "floss.myzel394.quid_faciam_hodie/window_focus"
|
||||
|
||||
private var channel: MethodChannel? = null
|
||||
|
||||
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
|
||||
super.configureFlutterEngine(flutterEngine)
|
||||
setupChannel(flutterEngine)
|
||||
}
|
||||
|
||||
override fun cleanUpFlutterEngine(flutterEngine: FlutterEngine) {
|
||||
super.cleanUpFlutterEngine(flutterEngine)
|
||||
teardownChannel()
|
||||
}
|
||||
|
||||
@SuppressLint("WrongConstant")
|
||||
override fun onWindowFocusChanged(hasFocus: Boolean) {
|
||||
channel?.invokeMethod("windowFocusChanged", hasFocus)
|
||||
}
|
||||
|
||||
private fun setupChannel(flutterEngine: FlutterEngine) {
|
||||
channel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL_ID)
|
||||
}
|
||||
|
||||
private fun teardownChannel() {
|
||||
channel?.setMethodCallHandler(null)
|
||||
channel = null
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:quid_faciam_hodie/constants/spacing.dart';
|
||||
import 'package:quid_faciam_hodie/controllers/status_controller.dart';
|
||||
import 'package:quid_faciam_hodie/native_events/window_focus.dart';
|
||||
|
||||
const BAR_HEIGHT = 4.0;
|
||||
|
||||
@ -11,6 +12,7 @@ class Status extends StatefulWidget {
|
||||
final bool hideProgressBar;
|
||||
final bool autoStart;
|
||||
final bool isIndeterminate;
|
||||
final bool pauseOnLostFocus;
|
||||
final VoidCallback? onEnd;
|
||||
final Duration duration;
|
||||
|
||||
@ -21,6 +23,7 @@ class Status extends StatefulWidget {
|
||||
this.hideProgressBar = false,
|
||||
this.autoStart = false,
|
||||
this.isIndeterminate = false,
|
||||
this.pauseOnLostFocus = true,
|
||||
this.duration = const Duration(seconds: 5),
|
||||
this.onEnd,
|
||||
this.controller,
|
||||
@ -31,6 +34,8 @@ class Status extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _StatusState extends State<Status> with TickerProviderStateMixin {
|
||||
bool _wasAnimatingBeforeLostFocus = false;
|
||||
|
||||
Animation<double>? animation;
|
||||
AnimationController? animationController;
|
||||
|
||||
@ -60,6 +65,20 @@ class _StatusState extends State<Status> with TickerProviderStateMixin {
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
EventChannelWindowFocus.setGlobalListener((hasFocus) {
|
||||
if (!widget.pauseOnLostFocus) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (hasFocus && _wasAnimatingBeforeLostFocus) {
|
||||
animationController?.forward();
|
||||
} else if (!hasFocus) {
|
||||
_wasAnimatingBeforeLostFocus =
|
||||
animationController?.isAnimating ?? false;
|
||||
animationController?.stop();
|
||||
}
|
||||
});
|
||||
|
||||
if (widget.autoStart) {
|
||||
initializeAnimation();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user