Page Menu
Home
Musing Studio
Search
Configure Global Search
Log In
Files
F14178463
MacUpdatesViewModel.swift
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Subscribers
None
MacUpdatesViewModel.swift
View Options
/// See https://sparkle-project.org/documentation/programmatic-setup#create-an-updater-in-swiftui
import
SwiftUI
import
Sparkle
/// This view model class manages Sparkle's updater and publishes when new updates are allowed to be checked.
final
class
MacUpdatesViewModel
:
ObservableObject
{
@
Published
var
canCheckForUpdates
=
false
private
let
updaterController
:
SPUStandardUpdaterController
private
let
updaterDelegate
=
MacUpdatesViewModelDelegate
()
var
automaticallyCheckForUpdates
:
Bool
{
get
{
return
updaterController
.
updater
.
automaticallyChecksForUpdates
}
set
(
newValue
)
{
updaterController
.
updater
.
automaticallyChecksForUpdates
=
newValue
}
}
init
()
{
updaterController
=
SPUStandardUpdaterController
(
startingUpdater
:
true
,
updaterDelegate
:
updaterDelegate
,
userDriverDelegate
:
nil
)
updaterController
.
updater
.
publisher
(
for
:
\
.
canCheckForUpdates
)
.
assign
(
to
:
&
$
canCheckForUpdates
)
if
automaticallyCheckForUpdates
{
updaterController
.
updater
.
checkForUpdatesInBackground
()
}
}
func
checkForUpdates
()
{
updaterController
.
checkForUpdates
(
nil
)
}
func
getLastUpdateCheckDate
()
->
Date
?
{
return
updaterController
.
updater
.
lastUpdateCheckDate
}
@
discardableResult
func
toggleAllowedChannels
()
->
Set
<
String
>
{
return
updaterDelegate
.
allowedChannels
(
for
:
updaterController
.
updater
)
}
}
final
class
MacUpdatesViewModelDelegate
:
NSObject
,
SPUUpdaterDelegate
{
@
AppStorage
(
WFDefaults
.
subscribeToBetaUpdates
,
store
:
UserDefaults
.
shared
)
var
subscribeToBetaUpdates
:
Bool
=
false
func
allowedChannels
(
for
updater
:
SPUUpdater
)
->
Set
<
String
>
{
let
allowedChannels
=
Set
(
subscribeToBetaUpdates
?
[
"beta"
]
:
[])
return
allowedChannels
}
}
// This additional view is needed for the disabled state on the menu item to work properly before Monterey.
// See https://stackoverflow.com/questions/68553092/menu-not-updating-swiftui-bug for more information
struct
CheckForUpdatesView
:
View
{
@
ObservedObject
var
updaterViewModel
:
MacUpdatesViewModel
var
body
:
some
View
{
Button
(
"Check for Updates…"
,
action
:
updaterViewModel
.
checkForUpdates
)
.
disabled
(
!
updaterViewModel
.
canCheckForUpdates
)
}
}
File Metadata
Details
Attached
Mime Type
text/x-c
Expires
Thu, Apr 2, 6:27 AM (8 h, 42 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3673664
Attached To
rWFSUI WriteFreely SwiftUI
Event Timeline
Log In to Comment