Skip to content

Commit

Permalink
fix: Refresh iOS folders when foregrounding the app (#482)
Browse files Browse the repository at this point in the history
This change also includes a drive-by fix to a bug which led to duplicate
`ApplicationModel` instances, causing unnecessary directory indexing.
  • Loading branch information
jbmorley authored Nov 26, 2022
1 parent 68fd192 commit da5715e
Show file tree
Hide file tree
Showing 15 changed files with 33 additions and 26 deletions.
13 changes: 0 additions & 13 deletions core/Sources/FileawayCore/Model/ApplicationModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,6 @@ import SwiftUI
import AppKit
#endif

public struct ApplicationModelKey: EnvironmentKey {
@MainActor public static var defaultValue: ApplicationModel = ApplicationModel()
}

extension EnvironmentValues {

public var applicationModel: ApplicationModel {
get { self[ApplicationModelKey.self] }
set { self[ApplicationModelKey.self] = newValue }
}

}

public class ApplicationModel: ObservableObject {

public let settings = Settings()
Expand Down
20 changes: 20 additions & 0 deletions core/Sources/FileawayCore/Utilities/DirectoryMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,26 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import Combine
import Foundation

#if os(macOS)
import EonilFSEvents
#else
import UIKit
#endif

public class DirectoryMonitor: ObservableObject {

let locations: [URL]
let syncQueue = DispatchQueue(label: "DirectoryMonitor.syncQueue")

var cancellables: Set<AnyCancellable> = []

@MainActor @Published var files: Set<URL>? = nil

#if os(macOS)

lazy var stream: EonilFSEventStream = {
let stream = try! EonilFSEventStream(
pathsToWatch: self.locations.map { $0.path },
Expand Down Expand Up @@ -63,14 +69,27 @@ public class DirectoryMonitor: ObservableObject {
stream.setDispatchQueue(syncQueue)
return stream
}()

#endif


public init(locations: [URL]) {
self.locations = locations
}

public func start() {
dispatchPrecondition(condition: .notOnQueue(syncQueue))

#if os(iOS)
NotificationCenter.default
.publisher(for: UIApplication.willEnterForegroundNotification)
.sink { foreground in
print("Refreshing monitor for \(self.locations).")
self.refresh()
}
.store(in: &cancellables)
#endif

syncQueue.async {
#if os(macOS)
try! self.stream.start()
Expand All @@ -89,6 +108,7 @@ public class DirectoryMonitor: ObservableObject {
self.stream.stop()
}
#endif
cancellables.removeAll()
}

public func refresh() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import SwiftUI

public struct LocationMenuItems: View {

@Environment(\.applicationModel) private var applicationModel
@EnvironmentObject private var applicationModel: ApplicationModel
@EnvironmentObject private var sceneModel: SceneModel

private var url: URL
Expand Down
2 changes: 1 addition & 1 deletion core/Sources/FileawayCore/Views/Viewer/LocationRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import FilePicker

struct LocationRow: View {

@Environment(\.applicationModel) private var applicationModel
@EnvironmentObject private var applicationModel: ApplicationModel
@ObservedObject var directoryViewModel: DirectoryViewModel

var body: some View {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import FilePicker

public struct LocationSection: View {

@Environment(\.applicationModel) private var applicationModel
@EnvironmentObject private var applicationModel: ApplicationModel

#if os(iOS)
@Environment(\.editMode) private var editMode
Expand Down
2 changes: 1 addition & 1 deletion core/Sources/FileawayCore/Views/Viewer/Sidebar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import SwiftUI

public struct Sidebar: View {

@Environment(\.applicationModel) var applicationModel
@EnvironmentObject private var applicationModel: ApplicationModel
@ObservedObject var sceneModel: SceneModel

public init(sceneModel: SceneModel) {
Expand Down
2 changes: 1 addition & 1 deletion ios/Fileaway/FileawayApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct AnytimeApp: App {
var body: some Scene {
WindowGroup {
ContentView(applicationModel: applicationModel)
.environment(\.applicationModel, applicationModel)
.environmentObject(applicationModel)
}
}

Expand Down
2 changes: 1 addition & 1 deletion ios/Fileaway/Views/Wizard/RulePicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import FileawayCore
struct RulePicker: View {

@Environment(\.dismiss) var dismiss
@Environment(\.applicationModel) var applicationModel

@EnvironmentObject private var applicationModel: ApplicationModel
@StateObject var rulePickerModel: RulePickerModel

private let url: URL
Expand Down
2 changes: 1 addition & 1 deletion ios/Fileaway/Views/Wizard/RulePickerRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import FileawayCore

struct RulePickerRow: View {

@Environment(\.applicationModel) var applicationModel
@EnvironmentObject private var applicationModel: ApplicationModel

private let url: URL
private let ruleModel: RuleModel
Expand Down
2 changes: 1 addition & 1 deletion ios/Fileaway/Views/Wizard/WizardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import FileawayCore

struct WizardView: View {

@Environment(\.applicationModel) var applicationModel
@Environment(\.dismiss) var dismiss

@EnvironmentObject private var applicationModel: ApplicationModel
@StateObject var wizardModel = WizardModel()

let file: FileInfo
Expand Down
2 changes: 1 addition & 1 deletion macos/Fileaway/FileawayApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct FileawayApp: App {

WindowGroup(id: "main") {
ContentView(applicationModel: applicationModel)
.environment(\.applicationModel, applicationModel)
.environmentObject(applicationModel)
}
.commands {
ToolbarCommands()
Expand Down
2 changes: 1 addition & 1 deletion macos/Fileaway/Scenes/Wizard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct Wizard: Scene {
WindowGroup(id: Self.windowID, for: URL.self) { $url in
if let url = url {
WizardView(url: url)
.environment(\.applicationModel, applicationModel)
.environmentObject(applicationModel)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion macos/Fileaway/Toolbars/SelectionToolbar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import FileawayCore

struct SelectionToolbar: CustomizableToolbarContent {

@Environment(\.applicationModel) var manager
@Environment(\.openWindow) var openWindow

@EnvironmentObject private var applicationModel: ApplicationModel
@ObservedObject var directoryViewModel: DirectoryViewModel

var body: some CustomizableToolbarContent {
Expand Down
2 changes: 1 addition & 1 deletion macos/Fileaway/Views/Settings/GeneralSettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import FileawayCore

struct GeneralSettingsView: View {

@Environment(\.applicationModel) var applicationModel: ApplicationModel
@EnvironmentObject private var applicationModel: ApplicationModel

var body: some View {
FileTypesView(settings: applicationModel.settings)
Expand Down
2 changes: 1 addition & 1 deletion macos/Fileaway/Views/Wizard/WizardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class RulesWizardModel: ObservableObject {

struct WizardView: View {

@Environment(\.applicationModel) var applicationModel
@EnvironmentObject private var applicationModel: ApplicationModel

@StateObject var rulesWizardModel = RulesWizardModel()

Expand Down

0 comments on commit da5715e

Please sign in to comment.