Skip to content

Commit cffc232

Browse files
committed
Update idle detection and folder management logic
- Increased idle timer duration in `IdleDetector` from 2 to 5 seconds. - Adjusted `DecayPercentage` calculation in `WatchedFolder` to return 1 when sizes are equal and updated `SavedSpace` calculation. - Enhanced `Watcher` to start `IdleDetector`; modified `UpdateWatched` to include `isFreshlyCompressed` parameter. - Converted `OnSystemIdle` to an asynchronous method with re-entrance checks and logging. - Made `Analyser` a class-level variable in `CompressableFolder`. - Updated `FolderViewModel` to pass an additional parameter to `UpdateWatched` during uncompression. - Changed `HomeViewModel` to call `UpdateWatched` for compressed folder notifications and refactored folder addition to `AddOrUpdateFolderWatcher`.
1 parent 58d860b commit cffc232

File tree

6 files changed

+42
-19
lines changed

6 files changed

+42
-19
lines changed

CompactGUI.Watcher/IdleDetector.vb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Public Class IdleDetector
1515
Public Shared Property IsEnabled As Boolean = True
1616

1717
Shared Sub New()
18-
_idletimer = New PeriodicTimer(TimeSpan.FromSeconds(2))
18+
_idletimer = New PeriodicTimer(TimeSpan.FromSeconds(5))
1919
End Sub
2020

2121
Public Shared Sub Start()

CompactGUI.Watcher/WatchedFolder.vb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ Public Class WatchedFolder : Inherits ObservableObject
2020
Public ReadOnly Property DecayPercentage As Decimal
2121
Get
2222
If LastCompressedSize = 0 Then Return 1
23-
Return If(LastUncompressedSize = LastCompressedSize OrElse LastCompressedSize > LastUncompressedSize, 0D, Math.Clamp((LastCheckedSize - LastCompressedSize) / (LastUncompressedSize - LastCompressedSize), 0, 1))
23+
Return If(LastUncompressedSize = LastCompressedSize OrElse LastCompressedSize > LastUncompressedSize, 1D, Math.Clamp((LastCheckedSize - LastCompressedSize) / (LastUncompressedSize - LastCompressedSize), 0, 1))
2424
End Get
2525
End Property
2626
<JsonIgnore>
2727
Public ReadOnly Property SavedSpace As Long
2828
Get
29-
Return LastUncompressedSize - LastCompressedSize
29+
Return LastUncompressedSize - LastCheckedSize
3030
End Get
3131
End Property
3232

CompactGUI.Watcher/Watcher.vb

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Public Class Watcher : Inherits ObservableObject
6464
Sub New(excludedFiletypes As String())
6565

6666
IdleDetector.Start()
67+
Debug.WriteLine("Idle Detector Started")
6768
AddHandler IdleDetector.IsIdle, AddressOf OnSystemIdle
6869

6970
BGCompactor = New BackgroundCompactor(excludedFiletypes)
@@ -114,20 +115,25 @@ Public Class Watcher : Inherits ObservableObject
114115

115116
End Sub
116117

117-
Public Sub UpdateWatched(folder As String, ByRef analyser As Analyser, Optional immediateFlushToDisk As Boolean = True)
118+
Public Sub UpdateWatched(folder As String, ByRef analyser As Analyser, isFreshlyCompressed As Boolean, Optional immediateFlushToDisk As Boolean = True)
118119

119120
Dim existingItem = WatchedFolders.FirstOrDefault(Function(f) f.Folder = folder)
120121
If existingItem IsNot Nothing Then
121122

122123
existingItem.LastCheckedDate = DateTime.Now
123124
existingItem.LastCheckedSize = analyser.CompressedBytes
125+
existingItem.LastUncompressedSize = analyser.UncompressedBytes
124126
existingItem.LastSystemModifiedDate = FolderMonitors.First(Function(f) f.Folder = folder).LastChangedDate
125127

126128
existingItem.CompressionLevel = analyser.FileCompressionDetailsList.Select(Function(f) f.CompressionMode).Max
127129

130+
If isFreshlyCompressed OrElse existingItem.CompressionLevel = WOFCompressionAlgorithm.NO_COMPRESSION Then
131+
existingItem.LastCompressedSize = analyser.CompressedBytes
132+
End If
133+
128134
FolderMonitors.First(Function(f) f.Folder = folder).HasTargetChanged = False
129135
OnPropertyChanged(NameOf(TotalSaved))
130-
If Not immediateFlushToDisk Then WriteToFile()
136+
If immediateFlushToDisk Then WriteToFile()
131137
End If
132138
End Sub
133139

@@ -204,27 +210,38 @@ Public Class Watcher : Inherits ObservableObject
204210

205211

206212

213+
Private _isHandlingIdle As Boolean = False
207214

208-
Private Async Sub OnSystemIdle()
215+
Private Async Function OnSystemIdle() As Task
216+
If _isHandlingIdle Then Return
217+
_isHandlingIdle = True
218+
Try
209219

210-
If Not IsWatchingEnabled Then Return
220+
Debug.WriteLine("System Idle Detected")
221+
If Not IsWatchingEnabled Then Return
211222

212-
Dim recentThresholdDate As DateTime = DateTime.Now.AddSeconds(-LAST_SYSTEM_MODIFIED_TIME_THRESHOLD)
213-
If FolderMonitors.Exists(Function(x) x.LastChangedDate > recentThresholdDate) Then Return
223+
Dim recentThresholdDate As DateTime = DateTime.Now.AddSeconds(-LAST_SYSTEM_MODIFIED_TIME_THRESHOLD)
224+
If FolderMonitors.Exists(Function(x) x.LastChangedDate > recentThresholdDate) Then Return
225+
226+
If _parseWatchersSemaphore.CurrentCount <> 0 Then
227+
Await ParseWatchers()
228+
End If
229+
If _parseWatchersSemaphore.CurrentCount <> 0 AndAlso IsBackgroundCompactingEnabled Then
230+
Await BackgroundCompact()
231+
End If
232+
Finally
233+
234+
_isHandlingIdle = False
235+
End Try
236+
End Function
214237

215-
If _parseWatchersSemaphore.CurrentCount <> 0 Then
216-
Await ParseWatchers()
217-
End If
218-
If _parseWatchersSemaphore.CurrentCount <> 0 AndAlso IsBackgroundCompactingEnabled Then
219-
Await BackgroundCompact()
220-
End If
221-
End Sub
222238

223239
Public Async Function ParseWatchers(Optional ParseAll As Boolean = False) As Task
224240
Dim acquired = Await _parseWatchersSemaphore.WaitAsync(0)
225241
If Not acquired Then Return
226242

227243
Try
244+
Debug.WriteLine("Background Parsing Watchers")
228245

229246
Dim WatchersToCheck = If(ParseAll, FolderMonitors, FolderMonitors.Where(Function(w) w.HasTargetChanged)).ToList()
230247

CompactGUI/Models/NewModels/ICompressableFolder.vb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,16 @@ Public MustInherit Class CompressableFolder : Inherits ObservableObject
116116

117117
Private CancellationTokenSource As CancellationTokenSource
118118

119+
Public Analyser As Analyser
120+
119121
Public Async Function AnalyseFolderAsync() As Task(Of Integer)
120122

121123
FolderActionState = ActionState.Analysing
122124

123125
CancellationTokenSource = New CancellationTokenSource()
124126
Dim token = CancellationTokenSource.Token
125127

126-
Dim Analyser As New Analyser(FolderName)
128+
Analyser = New Analyser(FolderName)
127129

128130
If Not Analyser.HasDirectoryWritePermission Then
129131
FolderActionState = ActionState.Idle

CompactGUI/ViewModels/FolderViewModel.vb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ Public Class FolderViewModel : Inherits ObservableObject
9595

9696
Public Async Function UncompressFolderAsync() As Task
9797
Await Folder.UncompressFolder()
98+
Application.GetService(Of Watcher.Watcher).UpdateWatched(Folder.FolderName, Folder.Analyser, True)
99+
98100
End Function
99101

100102

CompactGUI/ViewModels/HomeViewModel.vb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ Partial Public Class HomeViewModel
172172

173173
End If
174174

175+
Application.GetService(Of Watcher.Watcher).UpdateWatched(folder.FolderName, folder.Analyser, True)
176+
175177
'For Each poorext In folder.PoorlyCompressedFiles
176178
' Debug.WriteLine($"{poorext.extension} : {poorext.totalFiles} with ratio of {poorext.cRatio}")
177179
'Next
@@ -183,15 +185,15 @@ Partial Public Class HomeViewModel
183185
Compressing = False
184186

185187
For Each folder In Folders.Where(Function(f) f.CompressionOptions.WatchFolderForChanges)
186-
AddFolderToWatcher(folder)
188+
AddOrUpdateFolderWatcher(folder)
187189
Next
188190

189191

190192
Await Application.GetService(Of Watcher.Watcher).EnableBackgrounding()
191193
End Function
192194

193195

194-
Public Sub AddFolderToWatcher(folder As CompressableFolder)
196+
Public Sub AddOrUpdateFolderWatcher(folder As CompressableFolder)
195197
Debug.WriteLine("Adding folder to watcher: " & folder.FolderName)
196198
Dim newWatched = New Watcher.WatchedFolder With {
197199
.Folder = folder.FolderName,

0 commit comments

Comments
 (0)