Skip to content

Commit

Permalink
fix: Perbaiki flow screenshot di start time
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
CoderJava committed Aug 7, 2023
1 parent ab3328b commit 12f4f88
Showing 1 changed file with 46 additions and 15 deletions.
61 changes: 46 additions & 15 deletions lib/feature/presentation/page/home/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,6 @@ class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
final durationInSeconds = finishDateTime.difference(startDateTime).inSeconds.abs();

final activity = percentActivity.round();

final listPathScreenshots = <String?>[];
String files;
if (!isForceStop) {
Expand All @@ -1091,22 +1090,49 @@ class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
final isPermissionScreenRecordingGranted = await platformChannelHelper.checkPermissionScreenRecording();
if ((isPermissionScreenRecordingGranted != null && !isPermissionScreenRecordingGranted) ||
listPathScreenshots.isEmpty) {
// stop timer-nya jika permission screen recording-nya tidak diallow-kan atau
// gagal ambil screenshot-nya di end time
stopTimer();
isTimerStart = false;
selectedTask = null;
setState(() {});
final fileDefaultScreenshot = await widgetHelper.getImageFileFromAssets(BaseImage.imageFileNotFound);
listPathScreenshots.add(fileDefaultScreenshot.path);
}
if (listPathStartScreenshots.isNotEmpty) {
// hapus file list path start screenshot karena tidak pakai file tersebut
// jika file screenshot-nya dapat pas di end time
final filtered =
listPathStartScreenshots.where((element) => element != null && element.isNotEmpty).map((e) => e!).toList();
for (final element in filtered) {
final file = File(element);
if (file.existsSync()) {
file.deleteSync();
}
}
}
} else {
// stop timer-nya jika isForceStop bernilai true
listPathScreenshots.clear();
stopTimer();
isTimerStart = false;
selectedTask = null;
setState(() {});

if (listPathStartScreenshots.isNotEmpty) {
listPathScreenshots.addAll(listPathStartScreenshots);
final listFileStartScreenshotValid = listPathStartScreenshots
.where((element) => element != null && element.isNotEmpty && File(element).existsSync())
.toList();
if (listFileStartScreenshotValid.isNotEmpty) {
// masukkan file start screenshot yang valid
listPathScreenshots.addAll(listFileStartScreenshotValid);
} else {
// gunakan file default screenshot jika file start screenshot-nya tidak ada yang valid
final fileDefaultScreenshot = await widgetHelper.getImageFileFromAssets(BaseImage.imageFileNotFound);
listPathScreenshots.add(fileDefaultScreenshot.path);
}
} else {
stopTimer();
isTimerStart = false;
selectedTask = null;
setState(() {});
// gunakan file default screenshot jika file start screensho-nya empty
final fileDefaultScreenshot = await widgetHelper.getImageFileFromAssets(BaseImage.imageFileNotFound);
listPathScreenshots.add(fileDefaultScreenshot.path);
}
Expand Down Expand Up @@ -1176,15 +1202,7 @@ class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
stopTimer();

now = DateTime.now();
platformChannelHelper.checkPermissionScreenRecording().then((isGranted) async {
if (isGranted != null && isGranted) {
final listPathScreenshots = await platformChannelHelper.doTakeScreenshot();
if (listPathScreenshots.isNotEmpty) {
listPathStartScreenshots.clear();
listPathStartScreenshots.addAll(listPathScreenshots);
}
}
});
doTakeScreenshotStart();
timeTrack = Timer.periodic(const Duration(milliseconds: 1), (timer) {
// intervalnya dibuat milliseconds agar bisa mengikuti dengan date time device-nya.
final newNow = DateTime.now();
Expand Down Expand Up @@ -1213,6 +1231,18 @@ class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
});
}

void doTakeScreenshotStart() {
platformChannelHelper.checkPermissionScreenRecording().then((isGranted) async {
if (isGranted != null && isGranted) {
final listPathScreenshots = await platformChannelHelper.doTakeScreenshot();
if (listPathScreenshots.isNotEmpty) {
listPathStartScreenshots.clear();
listPathStartScreenshots.addAll(listPathScreenshots);
}
}
});
}

void stopTimer() {
countTimeReminderTrackInSeconds = 0;
if (timeTrack != null && timeTrack!.isActive) {
Expand All @@ -1234,6 +1264,7 @@ class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
finishTime = DateTime.now();
doTakeScreenshot(startTime, finishTime);
resetCountTimer();
doTakeScreenshotStart();
startTime = DateTime.now();
finishTime = null;
}
Expand Down

0 comments on commit 12f4f88

Please sign in to comment.