Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New: Add function "drop to import" #16

Merged
merged 2 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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