@@ -69,6 +69,7 @@ class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
69
69
final notificationHelper = sl <NotificationHelper >();
70
70
final intervalScreenshot = 60 * 5 ; // 300 detik (5 menit)
71
71
final listTrackLocal = < Track > [];
72
+ final listPathStartScreenshots = < String ? > [];
72
73
73
74
var isWindowVisible = true ;
74
75
var userId = '' ;
@@ -84,6 +85,7 @@ class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
84
85
DateTime ? startTime;
85
86
DateTime ? finishTime;
86
87
DateTime ? infoDateTime;
88
+ DateTime ? now;
87
89
88
90
@override
89
91
void setState (VoidCallback fn) {
@@ -596,7 +598,8 @@ class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
596
598
}
597
599
598
600
if (isPermissionScreenRecordingGranted! ) {
599
- final isPermissionAccessibilityGranted = await platformChannelHelper.checkPermissionAccessibility ();
601
+ final isPermissionAccessibilityGranted =
602
+ await platformChannelHelper.checkPermissionAccessibility ();
600
603
if (mounted && isPermissionAccessibilityGranted != null && ! isPermissionAccessibilityGranted) {
601
604
widgetHelper.showDialogPermissionAccessibility (context);
602
605
return ;
@@ -618,8 +621,8 @@ class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
618
621
} else {
619
622
isTimerStart = false ;
620
623
itemTask.trackedInSeconds = valueNotifierTaskTracked.value;
621
- finishTime = DateTime .now ();
622
624
stopTimer ();
625
+ finishTime = DateTime .now ();
623
626
doTakeScreenshot (startTime, finishTime);
624
627
selectedTask = null ;
625
628
}
@@ -1004,13 +1007,28 @@ class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
1004
1007
platformChannelHelper.startEventChannel ().listen ((Object ? event) {
1005
1008
if (event != null ) {
1006
1009
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
+ }
1008
1026
}
1009
1027
}
1010
1028
});
1011
1029
}
1012
1030
1013
- void doTakeScreenshot (DateTime ? startTime, DateTime ? finishTime) async {
1031
+ void doTakeScreenshot (DateTime ? startTime, DateTime ? finishTime, { bool isForceStop = false } ) async {
1014
1032
var percentActivity = 0.0 ;
1015
1033
if (counterActivity > 0 && countTimerInSeconds > 0 ) {
1016
1034
percentActivity = (counterActivity / countTimerInSeconds) * 100 ;
@@ -1065,28 +1083,35 @@ class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
1065
1083
1066
1084
final activity = percentActivity.round ();
1067
1085
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
+ }
1089
1113
}
1114
+
1090
1115
listPathScreenshots.removeWhere ((element) => element == null || element.isEmpty);
1091
1116
if (listPathScreenshots.isNotEmpty) {
1092
1117
final firstElement = listPathScreenshots.first ?? '' ;
@@ -1105,7 +1130,7 @@ class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
1105
1130
);
1106
1131
}
1107
1132
}
1108
- final files = listPathScreenshots.join (',' );
1133
+ files = listPathScreenshots.join (',' );
1109
1134
1110
1135
final isShowScreenshotNotification =
1111
1136
sharedPreferencesManager.getBool (SharedPreferencesManager .keyIsEnableScreenshotNotification) ?? false ;
@@ -1149,8 +1174,42 @@ class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
1149
1174
void startTimer () {
1150
1175
countTimeReminderTrackInSeconds = 0 ;
1151
1176
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
+ }
1154
1213
});
1155
1214
}
1156
1215
0 commit comments