Skip to content

Commit

Permalink
Fix: Alert When Error Detected & Code Restructure
Browse files Browse the repository at this point in the history
btw, always remember to add `[weak self]` when you intend to reach self property in the closures, or it maybe lead to memory leaks
  • Loading branch information
haren724 committed Aug 25, 2023
1 parent 1a54abe commit 87bec01
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,8 @@
//

import SwiftUI
import Foundation
import UniformTypeIdentifiers

struct WallpaperExplorer: SubviewOfContentView,DropDelegate {

func dropUpdated(info: DropInfo) -> DropProposal? {
let proposal = DropProposal(operation: .copy)
return proposal
}

func performDrop(info: DropInfo) -> Bool {
guard let itemProvider = info.itemProviders(for: [UTType.fileURL]).first else { return false }
itemProvider.loadItem(forTypeIdentifier: UTType.fileURL.identifier, options: nil) { item, _ in
guard let data = item as? Data, let url = URL(dataRepresentation: data, relativeTo: nil) else { return }
// Do something with the file url
// remember to dispatch on main in case of a @State change
guard let wallpaperFolder = try? FileWrapper(url: url)
else{print("1");return}
guard wallpaperFolder.fileWrappers?["project.json"] != nil
else{print("2");return}
DispatchQueue.main.async {
try? FileManager.default.copyItem(
at: url,
to: FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
.appending(path: url.lastPathComponent)
)
}
}
return true
}
struct WallpaperExplorer: SubviewOfContentView {

@ObservedObject var viewModel: ContentViewModel
@ObservedObject var wallpaperViewModel: WallpaperViewModel
Expand Down Expand Up @@ -128,7 +100,6 @@ struct WallpaperExplorer: SubviewOfContentView,DropDelegate {
.padding(.bottom)
}
}
.onDrop(of: [UTType.fileURL], delegate: self)
}
}

Expand Down
1 change: 1 addition & 0 deletions Open Wallpaper Engine/ContentView/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ struct ContentView: View {
.animation(.spring(), value: viewModel.isFilterReveal)

WallpaperExplorer(contentViewModel: viewModel, wallpaperViewModel: wallpaperViewModel)
.onDrop(of: [.fileURL], delegate: viewModel)
.contextMenu {
ExplorerGlobalMenu(contentViewModel: viewModel, wallpaperViewModel: wallpaperViewModel)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
//

import SwiftUI
import UniformTypeIdentifiers

class ContentViewModel: ObservableObject {
class ContentViewModel: ObservableObject, DropDelegate {
@AppStorage("FilterReveal") var isFilterReveal = false
@AppStorage("WallpaperURLs") var wallpaperUrls = [URL]()
@AppStorage("SelectedIndex") var selectedIndex = 0
Expand Down Expand Up @@ -104,6 +105,46 @@ class ContentViewModel: ObservableObject {
self.importAlertError = error
self.importAlertPresented = true
}

func dropUpdated(info: DropInfo) -> DropProposal? {
let proposal = DropProposal(operation: .copy)
return proposal
}

func performDrop(info: DropInfo) -> Bool {
guard let itemProvider = info.itemProviders(for: [UTType.fileURL]).first
else {
alertImportModal(which: .unkown)
return false
}
itemProvider.loadItem(forTypeIdentifier: UTType.fileURL.identifier, options: nil) { [weak self] item, _ in
guard let data = item as? Data, let url = URL(dataRepresentation: data, relativeTo: nil)
else {
self?.alertImportModal(which: .unkown)
return
}
// Do something with the file url
// remember to dispatch on main in case of a @State change
guard let wallpaperFolder = try? FileWrapper(url: url)
else{
self?.alertImportModal(which: .unkown)
return
}
guard wallpaperFolder.fileWrappers?["project.json"] != nil
else{
self?.alertImportModal(which: .doesNotContainWallpaper)
return
}
DispatchQueue.main.async {
try? FileManager.default.copyItem(
at: url,
to: FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
.appending(path: url.lastPathComponent)
)
}
}
return true
}
}

extension Array: RawRepresentable where Element: Codable {
Expand Down

0 comments on commit 87bec01

Please sign in to comment.