Replace environment objects with observed objects

This commit is contained in:
Arkadiusz Fal
2022-11-24 21:36:05 +01:00
parent 23fa0968c6
commit 0d333b5583
102 changed files with 427 additions and 723 deletions

View File

@@ -1,7 +1,7 @@
import SwiftUI
struct AccountsNavigationLink: View {
@EnvironmentObject<AccountsModel> private var accounts
@ObservedObject private var accounts = AccountsModel.shared
var instance: Instance
var body: some View {
@@ -30,6 +30,6 @@ struct AccountsNavigationLink: View {
if accounts.current?.instance == instance {
accounts.setCurrent(nil)
}
InstancesModel.remove(instance)
InstancesModel.shared.remove(instance)
}
}

View File

@@ -9,11 +9,6 @@ struct AdvancedSettings: View {
@Default(.countryOfPublicInstances) private var countryOfPublicInstances
@Default(.instances) private var instances
@EnvironmentObject<AccountsModel> private var accounts
@EnvironmentObject<NavigationModel> private var navigation
@EnvironmentObject<PlayerModel> private var player
@EnvironmentObject<SettingsModel> private var settings
@State private var countries = [String]()
@State private var filesToShare = [MPVClient.logFile]
@State private var presentingInstanceForm = false
@@ -39,7 +34,7 @@ struct AdvancedSettings: View {
#endif
}
.onChange(of: countryOfPublicInstances) { newCountry in
InstancesManifest.shared.setPublicAccount(newCountry, accounts: accounts, asCurrent: accounts.current?.isPublic ?? true)
InstancesManifest.shared.setPublicAccount(newCountry, asCurrent: AccountsModel.shared.current?.isPublic ?? true)
}
.sheet(isPresented: $presentingInstanceForm) {
InstanceForm(savedInstanceID: $savedFormInstanceID)

View File

@@ -22,7 +22,7 @@ struct BrowsingSettings: View {
@Default(.homeHistoryItems) private var homeHistoryItems
@Default(.visibleSections) private var visibleSections
@EnvironmentObject<AccountsModel> private var accounts
@ObservedObject private var accounts = AccountsModel.shared
@State private var homeHistoryItemsText = ""
#if os(iOS)

View File

@@ -2,8 +2,7 @@ import Defaults
import SwiftUI
struct EditFavorites: View {
@EnvironmentObject<PlaylistsModel> private var playlistsModel
private var playlistsModel = PlaylistsModel.shared
private var model = FavoritesModel.shared
@Default(.favorites) private var favorites

View File

@@ -4,8 +4,8 @@ import SwiftUI
struct HistorySettings: View {
static let watchedThresholds = [50, 60, 70, 80, 90, 95, 100]
@EnvironmentObject<PlayerModel> private var player
@EnvironmentObject<SettingsModel> private var settings
private var player = PlayerModel.shared
private var settings = SettingsModel.shared
@Default(.saveRecents) private var saveRecents
@Default(.saveLastPlayed) private var saveLastPlayed

View File

@@ -16,7 +16,7 @@ struct InstanceForm: View {
@Environment(\.colorScheme) private var colorScheme
@Environment(\.presentationMode) private var presentationMode
@EnvironmentObject<AccountsModel> private var accounts
@ObservedObject private var accounts = AccountsModel.shared
var body: some View {
VStack(alignment: .leading) {
@@ -140,7 +140,7 @@ struct InstanceForm: View {
return
}
let savedInstance = InstancesModel.add(app: app, name: name, url: url)
let savedInstance = InstancesModel.shared.add(app: app, name: name, url: url)
savedInstanceID = savedInstance.id
if accounts.isEmpty {

View File

@@ -13,7 +13,7 @@ struct InstanceSettings: View {
List {
Section(header: Text("Accounts".localized())) {
if instance.app.supportsAccounts {
ForEach(InstancesModel.accounts(instance.id), id: \.self) { account in
ForEach(InstancesModel.shared.accounts(instance.id), id: \.self) { account in
#if os(tvOS)
Button(account.description) {}
.contextMenu {
@@ -70,7 +70,7 @@ struct InstanceSettings: View {
frontendURL = instance.frontendURL ?? ""
}
.onChange(of: frontendURL) { newValue in
InstancesModel.setFrontendURL(instance, newValue)
InstancesModel.shared.setFrontendURL(instance, newValue)
}
.labelsHidden()
.autocapitalization(.none)
@@ -84,7 +84,7 @@ struct InstanceSettings: View {
proxiesVideos = instance.proxiesVideos
}
.onChange(of: proxiesVideos) { newValue in
InstancesModel.setProxiesVideos(instance, newValue)
InstancesModel.shared.setProxiesVideos(instance, newValue)
}
}
}

View File

@@ -6,9 +6,8 @@ struct LocationsSettings: View {
@State private var presentingInstanceForm = false
@State private var savedFormInstanceID: Instance.ID?
@EnvironmentObject<AccountsModel> private var accounts
@EnvironmentObject<NavigationModel> private var navigation
@EnvironmentObject<SettingsModel> private var model
@ObservedObject private var accounts = AccountsModel.shared
private var model = SettingsModel.shared
@Default(.countryOfPublicInstances) private var countryOfPublicInstances
@Default(.instances) private var instances
@@ -30,7 +29,7 @@ struct LocationsSettings: View {
}
.onAppear(perform: loadCountries)
.onChange(of: countryOfPublicInstances) { newCountry in
InstancesManifest.shared.setPublicAccount(newCountry, accounts: accounts, asCurrent: accounts.current?.isPublic ?? true)
InstancesManifest.shared.setPublicAccount(newCountry, asCurrent: accounts.current?.isPublic ?? true)
}
.onChange(of: instancesManifest) { _ in
countryOfPublicInstances = nil
@@ -74,7 +73,7 @@ struct LocationsSettings: View {
.disabled(countries.isEmpty)
Button {
InstancesManifest.shared.changePublicAccount(accounts, settings: model)
InstancesManifest.shared.changePublicAccount()
} label: {
if let account = accounts.current, account.isPublic {
Text("Switch to other public location")
@@ -89,7 +88,6 @@ struct LocationsSettings: View {
Section(header: SettingsHeader(text: "Custom Locations".localized())) {
#if os(macOS)
InstancesSettings()
.environmentObject(model)
#else
ForEach(instances) { instance in
AccountsNavigationLink(instance: instance)
@@ -137,8 +135,5 @@ struct LocationsSettings: View {
struct LocationsSettings_Previews: PreviewProvider {
static var previews: some View {
LocationsSettings()
.environmentObject(AccountsModel())
.environmentObject(NavigationModel())
.environmentObject(SettingsModel())
}
}

View File

@@ -35,8 +35,8 @@ struct PlayerSettings: View {
@Default(.enableReturnYouTubeDislike) private var enableReturnYouTubeDislike
@Default(.systemControlsCommands) private var systemControlsCommands
@EnvironmentObject<AccountsModel> private var accounts
@EnvironmentObject<PlayerModel> private var player
@ObservedObject private var accounts = AccountsModel.shared
private var player = PlayerModel.shared
#if os(iOS)
private var idiom: UIUserInterfaceIdiom {

View File

@@ -5,7 +5,7 @@ struct QualitySettings: View {
@State private var presentingProfileForm = false
@State private var editedProfileID: QualityProfile.ID?
@EnvironmentObject<SettingsModel> private var settings
@ObservedObject private var settings = SettingsModel.shared
@Default(.qualityProfiles) private var qualityProfiles
@Default(.batteryCellularProfile) private var batteryCellularProfile

View File

@@ -19,15 +19,13 @@ struct SettingsView: View {
@Environment(\.presentationMode) private var presentationMode
#endif
@EnvironmentObject<AccountsModel> private var accounts
@EnvironmentObject<NavigationModel> private var navigation
@EnvironmentObject<SettingsModel> private var model
@ObservedObject private var accounts = AccountsModel.shared
@ObservedObject private var model = SettingsModel.shared
@Default(.instances) private var instances
var body: some View {
settings
.environmentObject(model)
.alert(isPresented: $model.presentingAlert) { model.alert }
}