@@ -69,6 +69,7 @@ class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
6969 final notificationHelper = sl <NotificationHelper >();
7070 final intervalScreenshot = 60 * 5 ; // 300 detik (5 menit)
7171 final listTrackLocal = < Track > [];
72+ final listPathStartScreenshots = < String ? > [];
7273
7374 var isWindowVisible = true ;
7475 var userId = '' ;
@@ -84,6 +85,7 @@ class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
8485 DateTime ? startTime;
8586 DateTime ? finishTime;
8687 DateTime ? infoDateTime;
88+ DateTime ? now;
8789
8890 @override
8991 void setState (VoidCallback fn) {
@@ -596,7 +598,8 @@ class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
596598 }
597599
598600 if (isPermissionScreenRecordingGranted! ) {
599- final isPermissionAccessibilityGranted = await platformChannelHelper.checkPermissionAccessibility ();
601+ final isPermissionAccessibilityGranted =
602+ await platformChannelHelper.checkPermissionAccessibility ();
600603 if (mounted && isPermissionAccessibilityGranted != null && ! isPermissionAccessibilityGranted) {
601604 widgetHelper.showDialogPermissionAccessibility (context);
602605 return ;
@@ -618,8 +621,8 @@ class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
618621 } else {
619622 isTimerStart = false ;
620623 itemTask.trackedInSeconds = valueNotifierTaskTracked.value;
621- finishTime = DateTime .now ();
622624 stopTimer ();
625+ finishTime = DateTime .now ();
623626 doTakeScreenshot (startTime, finishTime);
624627 selectedTask = null ;
625628 }
@@ -1004,13 +1007,28 @@ class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
10041007 platformChannelHelper.startEventChannel ().listen ((Object ? event) {
10051008 if (event != null ) {
10061009 if (event is String ) {
1007- isHaveActivity = true ;
1010+ final strEvent = event.toLowerCase ();
1011+ if (strEvent == 'triggered' ) {
1012+ // update flag activity menjadi true
1013+ isHaveActivity = true ;
1014+ } else if (strEvent == 'screen_is_locked' ) {
1015+ // auto stop timer dan ambil screenshot-nya
1016+ isTimerStart = false ;
1017+ stopTimer ();
1018+ finishTime = DateTime .now ();
1019+ doTakeScreenshot (startTime, finishTime, isForceStop: true );
1020+ selectedTask = null ;
1021+ setState (() {});
1022+ } else if (strEvent == 'screen_is_unlocked' ) {
1023+ // muat ulang datanya setelah user unlock screen
1024+ doLoadData ();
1025+ }
10081026 }
10091027 }
10101028 });
10111029 }
10121030
1013- void doTakeScreenshot (DateTime ? startTime, DateTime ? finishTime) async {
1031+ void doTakeScreenshot (DateTime ? startTime, DateTime ? finishTime, { bool isForceStop = false } ) async {
10141032 var percentActivity = 0.0 ;
10151033 if (counterActivity > 0 && countTimerInSeconds > 0 ) {
10161034 percentActivity = (counterActivity / countTimerInSeconds) * 100 ;
@@ -1065,28 +1083,35 @@ class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
10651083
10661084 final activity = percentActivity.round ();
10671085
1068- final listPathScreenshots = await platformChannelHelper.doTakeScreenshot ();
1069- final isPermissionScreenRecordingGranted = await platformChannelHelper.checkPermissionScreenRecording ();
1070- if (isPermissionScreenRecordingGranted != null && ! isPermissionScreenRecordingGranted) {
1071- debugPrint ('screen recording not granted' );
1072- notificationHelper.showPermissionScreenRecordingIssuedNotification ();
1073- valueNotifierTotalTracked.value -= durationInSeconds;
1074- valueNotifierTaskTracked.value -= durationInSeconds;
1075- isTimerStart = false ;
1076- stopTimer ();
1077- selectedTask = null ;
1078- setState (() {});
1079- return ;
1080- } else if (listPathScreenshots.isEmpty) {
1081- debugPrint ('list path screenshots is empty' );
1082- valueNotifierTotalTracked.value -= durationInSeconds;
1083- valueNotifierTaskTracked.value -= durationInSeconds;
1084- isTimerStart = false ;
1085- stopTimer ();
1086- selectedTask = null ;
1087- setState (() {});
1088- return ;
1086+ final listPathScreenshots = < String ? > [];
1087+ String files;
1088+ if (! isForceStop) {
1089+ listPathScreenshots.clear ();
1090+ listPathScreenshots.addAll (await platformChannelHelper.doTakeScreenshot ());
1091+ final isPermissionScreenRecordingGranted = await platformChannelHelper.checkPermissionScreenRecording ();
1092+ if ((isPermissionScreenRecordingGranted != null && ! isPermissionScreenRecordingGranted) ||
1093+ listPathScreenshots.isEmpty) {
1094+ stopTimer ();
1095+ isTimerStart = false ;
1096+ selectedTask = null ;
1097+ setState (() {});
1098+ final fileDefaultScreenshot = await widgetHelper.getImageFileFromAssets (BaseImage .imageFileNotFound);
1099+ listPathScreenshots.add (fileDefaultScreenshot.path);
1100+ }
1101+ } else {
1102+ listPathScreenshots.clear ();
1103+ if (listPathStartScreenshots.isNotEmpty) {
1104+ listPathScreenshots.addAll (listPathStartScreenshots);
1105+ } else {
1106+ stopTimer ();
1107+ isTimerStart = false ;
1108+ selectedTask = null ;
1109+ setState (() {});
1110+ final fileDefaultScreenshot = await widgetHelper.getImageFileFromAssets (BaseImage .imageFileNotFound);
1111+ listPathScreenshots.add (fileDefaultScreenshot.path);
1112+ }
10891113 }
1114+
10901115 listPathScreenshots.removeWhere ((element) => element == null || element.isEmpty);
10911116 if (listPathScreenshots.isNotEmpty) {
10921117 final firstElement = listPathScreenshots.first ?? '' ;
@@ -1105,7 +1130,7 @@ class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
11051130 );
11061131 }
11071132 }
1108- final files = listPathScreenshots.join (',' );
1133+ files = listPathScreenshots.join (',' );
11091134
11101135 final isShowScreenshotNotification =
11111136 sharedPreferencesManager.getBool (SharedPreferencesManager .keyIsEnableScreenshotNotification) ?? false ;
@@ -1149,8 +1174,42 @@ class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
11491174 void startTimer () {
11501175 countTimeReminderTrackInSeconds = 0 ;
11511176 stopTimer ();
1152- timeTrack = Timer .periodic (const Duration (seconds: 1 ), (_) {
1153- increaseTimerTray ();
1177+
1178+ now = DateTime .now ();
1179+ platformChannelHelper.checkPermissionScreenRecording ().then ((isGranted) async {
1180+ if (isGranted != null && isGranted) {
1181+ final listPathScreenshots = await platformChannelHelper.doTakeScreenshot ();
1182+ if (listPathScreenshots.isNotEmpty) {
1183+ listPathStartScreenshots.clear ();
1184+ listPathStartScreenshots.addAll (listPathScreenshots);
1185+ }
1186+ }
1187+ });
1188+ timeTrack = Timer .periodic (const Duration (milliseconds: 1 ), (timer) {
1189+ // intervalnya dibuat milliseconds agar bisa mengikuti dengan date time device-nya.
1190+ final newNow = DateTime .now ();
1191+ final newNowDate = DateTime (
1192+ newNow.year,
1193+ newNow.month,
1194+ newNow.day,
1195+ newNow.hour,
1196+ newNow.minute,
1197+ newNow.second,
1198+ );
1199+ if (now != null ) {
1200+ final nowDate = DateTime (
1201+ now! .year,
1202+ now! .month,
1203+ now! .day,
1204+ now! .hour,
1205+ now! .minute,
1206+ now! .second,
1207+ );
1208+ if (! nowDate.isAtSameMomentAs (newNowDate)) {
1209+ now = newNowDate;
1210+ increaseTimerTray ();
1211+ }
1212+ }
11541213 });
11551214 }
11561215
0 commit comments