Skip to content

Commit 12f4f88

Browse files
committed
fix: Perbaiki flow screenshot di start time
Yang diperbaiki adalah atur file screenshot di start time-nya agar terhapus secara otomatis jika file tersebut tidak terpakai. Dan ambil lagi screenshot-nya di start time berikutnya jika sudah terpenuhi interval-nya.
1 parent ab3328b commit 12f4f88

File tree

1 file changed

+46
-15
lines changed

1 file changed

+46
-15
lines changed

lib/feature/presentation/page/home/home_page.dart

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,6 @@ class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
10821082
final durationInSeconds = finishDateTime.difference(startDateTime).inSeconds.abs();
10831083

10841084
final activity = percentActivity.round();
1085-
10861085
final listPathScreenshots = <String?>[];
10871086
String files;
10881087
if (!isForceStop) {
@@ -1091,22 +1090,49 @@ class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
10911090
final isPermissionScreenRecordingGranted = await platformChannelHelper.checkPermissionScreenRecording();
10921091
if ((isPermissionScreenRecordingGranted != null && !isPermissionScreenRecordingGranted) ||
10931092
listPathScreenshots.isEmpty) {
1093+
// stop timer-nya jika permission screen recording-nya tidak diallow-kan atau
1094+
// gagal ambil screenshot-nya di end time
10941095
stopTimer();
10951096
isTimerStart = false;
10961097
selectedTask = null;
10971098
setState(() {});
10981099
final fileDefaultScreenshot = await widgetHelper.getImageFileFromAssets(BaseImage.imageFileNotFound);
10991100
listPathScreenshots.add(fileDefaultScreenshot.path);
11001101
}
1102+
if (listPathStartScreenshots.isNotEmpty) {
1103+
// hapus file list path start screenshot karena tidak pakai file tersebut
1104+
// jika file screenshot-nya dapat pas di end time
1105+
final filtered =
1106+
listPathStartScreenshots.where((element) => element != null && element.isNotEmpty).map((e) => e!).toList();
1107+
for (final element in filtered) {
1108+
final file = File(element);
1109+
if (file.existsSync()) {
1110+
file.deleteSync();
1111+
}
1112+
}
1113+
}
11011114
} else {
1115+
// stop timer-nya jika isForceStop bernilai true
11021116
listPathScreenshots.clear();
1117+
stopTimer();
1118+
isTimerStart = false;
1119+
selectedTask = null;
1120+
setState(() {});
1121+
11031122
if (listPathStartScreenshots.isNotEmpty) {
1104-
listPathScreenshots.addAll(listPathStartScreenshots);
1123+
final listFileStartScreenshotValid = listPathStartScreenshots
1124+
.where((element) => element != null && element.isNotEmpty && File(element).existsSync())
1125+
.toList();
1126+
if (listFileStartScreenshotValid.isNotEmpty) {
1127+
// masukkan file start screenshot yang valid
1128+
listPathScreenshots.addAll(listFileStartScreenshotValid);
1129+
} else {
1130+
// gunakan file default screenshot jika file start screenshot-nya tidak ada yang valid
1131+
final fileDefaultScreenshot = await widgetHelper.getImageFileFromAssets(BaseImage.imageFileNotFound);
1132+
listPathScreenshots.add(fileDefaultScreenshot.path);
1133+
}
11051134
} else {
1106-
stopTimer();
1107-
isTimerStart = false;
1108-
selectedTask = null;
1109-
setState(() {});
1135+
// gunakan file default screenshot jika file start screensho-nya empty
11101136
final fileDefaultScreenshot = await widgetHelper.getImageFileFromAssets(BaseImage.imageFileNotFound);
11111137
listPathScreenshots.add(fileDefaultScreenshot.path);
11121138
}
@@ -1176,15 +1202,7 @@ class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
11761202
stopTimer();
11771203

11781204
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-
});
1205+
doTakeScreenshotStart();
11881206
timeTrack = Timer.periodic(const Duration(milliseconds: 1), (timer) {
11891207
// intervalnya dibuat milliseconds agar bisa mengikuti dengan date time device-nya.
11901208
final newNow = DateTime.now();
@@ -1213,6 +1231,18 @@ class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
12131231
});
12141232
}
12151233

1234+
void doTakeScreenshotStart() {
1235+
platformChannelHelper.checkPermissionScreenRecording().then((isGranted) async {
1236+
if (isGranted != null && isGranted) {
1237+
final listPathScreenshots = await platformChannelHelper.doTakeScreenshot();
1238+
if (listPathScreenshots.isNotEmpty) {
1239+
listPathStartScreenshots.clear();
1240+
listPathStartScreenshots.addAll(listPathScreenshots);
1241+
}
1242+
}
1243+
});
1244+
}
1245+
12161246
void stopTimer() {
12171247
countTimeReminderTrackInSeconds = 0;
12181248
if (timeTrack != null && timeTrack!.isActive) {
@@ -1234,6 +1264,7 @@ class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
12341264
finishTime = DateTime.now();
12351265
doTakeScreenshot(startTime, finishTime);
12361266
resetCountTimer();
1267+
doTakeScreenshotStart();
12371268
startTime = DateTime.now();
12381269
finishTime = null;
12391270
}

0 commit comments

Comments
 (0)