Skip to content

Commit

Permalink
Merge pull request #16 from Erica-Iris/main
Browse files Browse the repository at this point in the history
New: Add function "drop to import"
  • Loading branch information
haren724 authored Aug 25, 2023
2 parents 6ee5a7a + 87bec01 commit 8bf6d02
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
import SwiftUI

struct WallpaperExplorer: SubviewOfContentView {

@ObservedObject var viewModel: ContentViewModel
@ObservedObject var wallpaperViewModel: WallpaperViewModel

@State var imageScaleIndex: Int = -1
@State var selectedIndex: Int!

@State var fileURL: URL?

init(contentViewModel viewModel: ContentViewModel, wallpaperViewModel: WallpaperViewModel) {
self.viewModel = viewModel
Expand Down Expand Up @@ -96,7 +99,8 @@ struct WallpaperExplorer: SubviewOfContentView {
}
.padding(.bottom)
}
} }
}
}
}

// MARK: - View Modifiers Extension
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 8bf6d02

Please sign in to comment.