Skip to content

Commit

Permalink
feat: Refresh timer
Browse files Browse the repository at this point in the history
  • Loading branch information
macmade committed Aug 18, 2024
1 parent 1dc1d39 commit 938a247
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions SDO/Classes/MainWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ public class MainWindowController: NSWindowController, NSCollectionViewDataSourc
@IBOutlet private var collectionView: NSCollectionView?
@IBOutlet private var slider: Slider?

private var imageSizeObserver: NSKeyValueObservation?
private var imagesObserver: NSKeyValueObservation?
private var imageSizeObserver: NSKeyValueObservation?
private var imagesObserver: NSKeyValueObservation?
private var refreshIntervalObserver: NSKeyValueObservation?
private var automaticRefreshObserver: NSKeyValueObservation?
private var refreshTimer: Timer?

public init()
{
Expand All @@ -69,6 +72,7 @@ public class MainWindowController: NSWindowController, NSCollectionViewDataSourc
public override func windowDidLoad()
{
super.windowDidLoad()
self.updateRefreshTimer()

self.imageSizeObserver = Preferences.shared.observe( \.imageSize )
{
Expand Down Expand Up @@ -96,6 +100,16 @@ public class MainWindowController: NSWindowController, NSCollectionViewDataSourc
self.collectionView?.reloadData()
}

self.refreshIntervalObserver = Preferences.shared.observe( \.refreshInterval )
{
[ weak self ] _, _ in self?.updateRefreshTimer()
}

self.automaticRefreshObserver = Preferences.shared.observe( \.automaticRefresh )
{
[ weak self ] _, _ in self?.updateRefreshTimer()
}

self.collectionView?.register( NSNib( nibNamed: "ImageItem", bundle: nil ), forItemWithIdentifier: self.itemID )
self.refresh( nil )
self.updateLayout()
Expand All @@ -109,6 +123,19 @@ public class MainWindowController: NSWindowController, NSCollectionViewDataSourc
}
}

private func updateRefreshTimer()
{
self.refreshTimer?.invalidate()

if Preferences.shared.automaticRefresh
{
self.refreshTimer = Timer.scheduledTimer( withTimeInterval: TimeInterval( Preferences.shared.refreshInterval ), repeats: true )
{
[ weak self ] _ in self?.refresh( nil )
}
}
}

@IBAction
public func resetImageSize( _ sender: Any? )
{
Expand Down Expand Up @@ -158,11 +185,6 @@ public class MainWindowController: NSWindowController, NSCollectionViewDataSourc
self.lastRefresh = "Last Refreshed: \( fmt.string( from: Date() ) )"

self.collectionView?.reloadData()

DispatchQueue.main.asyncAfter( deadline: .now() + .seconds( 60 * 5 ) )
{
self.refresh( nil )
}
}
}
}
Expand Down

0 comments on commit 938a247

Please sign in to comment.