From 12f4f88d7c404ba26b2446e6cced4604967670b5 Mon Sep 17 00:00:00 2001 From: CoderJava Date: Mon, 7 Aug 2023 09:11:29 +0700 Subject: [PATCH] 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. --- .../presentation/page/home/home_page.dart | 61 ++++++++++++++----- 1 file changed, 46 insertions(+), 15 deletions(-) diff --git a/lib/feature/presentation/page/home/home_page.dart b/lib/feature/presentation/page/home/home_page.dart index b42971a..f3cc06c 100644 --- a/lib/feature/presentation/page/home/home_page.dart +++ b/lib/feature/presentation/page/home/home_page.dart @@ -1082,7 +1082,6 @@ class _HomePageState extends State with TrayListener, WindowListener { final durationInSeconds = finishDateTime.difference(startDateTime).inSeconds.abs(); final activity = percentActivity.round(); - final listPathScreenshots = []; String files; if (!isForceStop) { @@ -1091,6 +1090,8 @@ class _HomePageState extends State 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; @@ -1098,15 +1099,40 @@ class _HomePageState extends State with TrayListener, WindowListener { 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); } @@ -1176,15 +1202,7 @@ class _HomePageState extends State 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(); @@ -1213,6 +1231,18 @@ class _HomePageState extends State 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) { @@ -1234,6 +1264,7 @@ class _HomePageState extends State with TrayListener, WindowListener { finishTime = DateTime.now(); doTakeScreenshot(startTime, finishTime); resetCountTimer(); + doTakeScreenshotStart(); startTime = DateTime.now(); finishTime = null; }