Skip to content

Commit

Permalink
[Optimize] Migrate from ObservableObject to @observable
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle-Ye committed Nov 6, 2023
1 parent ad3063c commit 379504e
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/sindresorhus/Settings.git",
"state" : {
"revision" : "c677bea28d92684911f93530d0b5f4c40eea743b",
"version" : "3.0.5"
"revision" : "2f4f65eed252198be383aa0d9058bbaf8f740aa5",
"version" : "3.1.0"
}
},
{
Expand Down
4 changes: 2 additions & 2 deletions MenuHelper/Store/CoffieStoreView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import StoreKit
import SwiftUI

struct CoffieStoreView: View {
@EnvironmentObject var store: Store
@Environment(Store.self) var store
let coffies: [Product]
let onPurchase: (Product) -> Void

Expand Down Expand Up @@ -51,6 +51,6 @@ struct CoffieStoreView_Previews: PreviewProvider {
CoffieStoreView(coffies: []) {
print($0.displayName + $0.displayPrice)
}
.environmentObject(Store())
.environment(Store())
}
}
2 changes: 1 addition & 1 deletion MenuHelper/Store/CoffieView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftUI

struct CoffieView: View {
@Environment(\.colorScheme) var colorScheme
@EnvironmentObject var store: Store
@Environment(Store.self) var store
@State private var errorTitle = ""
@State private var isShowingError = false

Expand Down
7 changes: 4 additions & 3 deletions MenuHelper/Store/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ enum StoreError: Error {
case failedVerification
}

class Store: ObservableObject {
@Published private(set) var coffies: [Product] = []
@Published private(set) var purchased = false
@Observable
final class Store {
private(set) var coffies: [Product] = []
private(set) var purchased = false
var updateListenerTask: Task<Void, any Error>?

private let storage = NSUbiquitousKeyValueStore.default
Expand Down
6 changes: 3 additions & 3 deletions MenuHelper/View/AppMenuItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

struct AppMenuItemView: View {
@EnvironmentObject var store: MenuItemStore
@Environment(MenuItemStore.self) var store
@State private var editingItem = false
var item: AppMenuItem

Expand All @@ -29,14 +29,14 @@ struct AppMenuItemView: View {
}
.sheet(isPresented: $editingItem, onDismiss: nil) {
AppMenuItemEditor(item: item, index: store.appItems.firstIndex(of: item))
.environmentObject(store)
.environment(store)
}
}
}

struct AppMenuItemView_Previews: PreviewProvider {
static var previews: some View {
AppMenuItemView(item: .xcode!)
.environmentObject(MenuItemStore())
.environment(MenuItemStore())
}
}
4 changes: 2 additions & 2 deletions MenuHelper/View/Editor/AppMenuItemEditor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import SwiftUI

struct AppMenuItemEditor: View {
@Environment(\.dismiss) private var dismiss
@EnvironmentObject var store: MenuItemStore
@Environment(MenuItemStore.self) private var store

@State var item: AppMenuItem
@State private var argumentString: String = ""
Expand Down Expand Up @@ -98,6 +98,6 @@ struct AppMenuItemEditor: View {
struct AppMenuItemEditor_Previews: PreviewProvider {
static var previews: some View {
AppMenuItemEditor(item: AppMenuItem(bundleIdentifier: "com.apple.dt.Xcode")!)
.environmentObject(MenuItemStore())
.environment(MenuItemStore())
}
}
4 changes: 3 additions & 1 deletion MenuHelper/View/SettingView/FolderSettingTab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import enum Settings.Settings
import SwiftUI

struct FolderSettingTab: View {
@ObservedObject var store: FolderItemStore
var store: FolderItemStore
@State private var isExpanded = false

var body: some View {
Expand All @@ -25,6 +25,7 @@ struct FolderSettingTab: View {
}
}

@MainActor
var openSection: Settings.Section {
Settings.Section(bottomDivider: true, verticalAlignment: .top) {
EmptyView()
Expand Down Expand Up @@ -77,6 +78,7 @@ struct FolderSettingTab: View {
}
}

@MainActor
var syncSection: Settings.Section {
Settings.Section {
EmptyView()
Expand Down
2 changes: 1 addition & 1 deletion MenuHelper/View/SettingView/GeneralSettingTab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ struct GeneralSettingTab: View {
@AppStorage(Key.newFileExtension)
private var newFileExtension = NewFileExtension.none

@ObservedObject var menuItemStore: MenuItemStore
var menuItemStore: MenuItemStore

@State private var model = GeneralSettingTabState()

Expand Down
6 changes: 4 additions & 2 deletions MenuHelper/View/SettingView/MenuSettingTab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class MenuSettingTabState {
}

struct MenuSettingTab: View {
@ObservedObject var store: MenuItemStore
var store: MenuItemStore
@State private var isDrogTargeted = false
@State private var appMenuItemEdited = false

Expand All @@ -50,6 +50,7 @@ struct MenuSettingTab: View {
}
}

@MainActor
var appItemSection: Settings.Section {
Settings.Section(bottomDivider: true, verticalAlignment: .top) {
EmptyView()
Expand Down Expand Up @@ -78,7 +79,7 @@ struct MenuSettingTab: View {
}
ForEach(store.appItems) { item in
AppMenuItemView(item: item)
.environmentObject(store)
.environment(store)
}
.onDelete { store.deleteAppItems(offsets: $0) }
.onMove { store.moveAppItems(from: $0, to: $1) }
Expand All @@ -103,6 +104,7 @@ struct MenuSettingTab: View {
}
}

@MainActor
var actionItemSection: Settings.Section {
Settings.Section {
EmptyView()
Expand Down
4 changes: 2 additions & 2 deletions MenuHelper/View/SettingView/SettingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import SwiftUI

struct SettingView: View {
@StateObject var menuItemStore = MenuItemStore()
@StateObject var folderItemStore = FolderItemStore()
@State var menuItemStore = MenuItemStore()
@State var folderItemStore = FolderItemStore()

var body: some View {
TabView {
Expand Down
4 changes: 2 additions & 2 deletions MenuHelper/Windows/SupportWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import Foundation
import SwiftUI

struct SupportWindow: View {
@StateObject var store = Store()
@State var store = Store()

var body: some View {
CoffieStoreView(coffies: store.coffies) { _ in }
.environmentObject(store)
.environment(store)
}
}

Expand Down
23 changes: 15 additions & 8 deletions Shared/ViewModel/FolderItemStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import SwiftUI

private let logger = Logger(subsystem: subsystem, category: "folder_item_store")

class FolderItemStore: ObservableObject {
@Published private(set) var bookmarkItems: [BookmarkFolderItem] = []
@Published private(set) var syncItems: [SyncFolderItem] = []
@Observable
final class FolderItemStore: Sendable {
private(set) var bookmarkItems: [BookmarkFolderItem] = []
private(set) var syncItems: [SyncFolderItem] = []

// MARK: - Init

Expand Down Expand Up @@ -82,23 +83,28 @@ class FolderItemStore: ObservableObject {
try? save()
}

@MainActor func deleteAllBookmarkItems() {
func deleteAllBookmarkItems() {
withAnimation {
bookmarkItems.removeAll()
}
try? save()
Task.detached {
try await self.save()
}
}

@MainActor func deleteAllSyncItems() {
func deleteAllSyncItems() {
withAnimation {
syncItems.removeAll()
}
try? save()
Task.detached {
try await self.save()
}
}

// MARK: - UserDefaults

@MainActor private func load() throws {
@MainActor
private func load() throws {
if let bookmarkItemData = UserDefaults.group.data(forKey: "BOOKMARK_ITEMS"),
let syncItemData = UserDefaults.group.data(forKey: "SYNC_ITEMS") {
let decoder = PropertyListDecoder()
Expand All @@ -115,6 +121,7 @@ class FolderItemStore: ObservableObject {
}
}

@MainActor
private func save() throws {
let encoder = PropertyListEncoder()
let bookmarkItemData = try encoder.encode(OrderedSet(bookmarkItems))
Expand Down
7 changes: 4 additions & 3 deletions Shared/ViewModel/MenuItemStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
import OrderedCollections
import SwiftUI

@MainActor class MenuItemStore: ObservableObject {
@Published private(set) var appItems: [AppMenuItem] = []
@Published private(set) var actionItems: [ActionMenuItem] = []
@Observable
class MenuItemStore {
private(set) var appItems: [AppMenuItem] = []
private(set) var actionItems: [ActionMenuItem] = []

// MARK: - Init

Expand Down

0 comments on commit 379504e

Please sign in to comment.