improve tendies list handling

This commit is contained in:
leminlimez committed 2025-06-03 10:54:10 -04:00
1 parent aaffd5ae0b
commit 7e60c71c0d
2 files changed
+13 -27

No files matched your search

+3 -11
View File
@@ -13,7 +13,7 @@ struct Pocket_PosterApp: App {
@AppStorage("finishedTutorial") var finishedTutorial: Bool = false @AppStorage("finishedTutorial") var finishedTutorial: Bool = false
@AppStorage("pbHash") var pbHash: String = "" @AppStorage("pbHash") var pbHash: String = ""
@State var selectedTendies: [URL]? = nil @State var selectedTendies: [URL] = []
@State var downloadURL: String? = nil @State var downloadURL: String? = nil
@State var showDownloadAlert = false @State var showDownloadAlert = false
@@ -51,11 +51,7 @@ struct Pocket_PosterApp: App {
// copy it over to the KFC bucket // copy it over to the KFC bucket
do { do {
let newURL = try DownloadManager.copyTendies(from: url) let newURL = try DownloadManager.copyTendies(from: url)
if selectedTendies == nil { selectedTendies.append(newURL)
selectedTendies = [newURL]
} else {
selectedTendies?.append(newURL)
}
Haptic.shared.notify(.success) Haptic.shared.notify(.success)
UIApplication.shared.alert(title: "Successfully imported \(url.lastPathComponent)", body: "") UIApplication.shared.alert(title: "Successfully imported \(url.lastPathComponent)", body: "")
} catch { } catch {
@@ -74,11 +70,7 @@ struct Pocket_PosterApp: App {
Task { Task {
do { do {
let newURL = try await DownloadManager.downloadFromURL(string: downloadURL!) let newURL = try await DownloadManager.downloadFromURL(string: downloadURL!)
if selectedTendies == nil { selectedTendies.append(newURL)
selectedTendies = [newURL]
} else {
selectedTendies?.append(newURL)
}
Haptic.shared.notify(.success) Haptic.shared.notify(.success)
UIApplication.shared.dismissAlert(animated: true) UIApplication.shared.dismissAlert(animated: true)
} catch { } catch {
+10 -16
View File
@@ -21,7 +21,7 @@ struct ContentView: View {
private let buildNumber = Bundle.main.infoDictionary?["CFBundleVersion"] as? String ?? "" private let buildNumber = Bundle.main.infoDictionary?["CFBundleVersion"] as? String ?? ""
@State var showTendiesImporter: Bool = false @State var showTendiesImporter: Bool = false
var selectedTendies: Binding<[URL]?> var selectedTendies: Binding<[URL]>
@State var showErrorAlert = false @State var showErrorAlert = false
@State var showSuccessAlert = false @State var showSuccessAlert = false
@@ -47,24 +47,24 @@ struct ContentView: View {
.buttonStyle(TintedButton(color: .green, fullwidth: true)) .buttonStyle(TintedButton(color: .green, fullwidth: true))
.padding(10) .padding(10)
if let selectedTendies = selectedTendies.wrappedValue, !selectedTendies.isEmpty { if !selectedTendies.wrappedValue.isEmpty {
HStack { HStack {
Text("Selected Tendies") Text("Selected Tendies")
.font(.headline) .font(.headline)
Spacer() Spacer()
} }
List { List {
ForEach(selectedTendies, id: \.self) { tendie in ForEach(selectedTendies.wrappedValue, id: \.self) { tendie in
Text(tendie.deletingPathExtension().lastPathComponent) Text(tendie.deletingPathExtension().lastPathComponent)
} }
.onDelete(perform: delete) .onDelete(perform: delete)
} }
.frame(height: CGFloat((selectedTendies.count * 65) + (selectedTendies.count < 3 ? 100 : 0)), alignment: .top) .frame(height: CGFloat((selectedTendies.wrappedValue.count * 65) + (selectedTendies.wrappedValue.count < 3 ? 100 : 0)), alignment: .top)
} }
} }
Section { Section {
if selectedTendies.wrappedValue != nil && !selectedTendies.wrappedValue!.isEmpty { if !selectedTendies.wrappedValue.isEmpty {
if pbHash == "" { if pbHash == "" {
Text("Enter your PosterBoard app hash in Settings.") Text("Enter your PosterBoard app hash in Settings.")
} else { } else {
@@ -74,8 +74,8 @@ struct ContentView: View {
// DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { // DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
do { do {
try PosterBoardManager.applyTendies(selectedTendies.wrappedValue!, appHash: pbHash) try PosterBoardManager.applyTendies(selectedTendies.wrappedValue, appHash: pbHash)
selectedTendies.wrappedValue = nil selectedTendies.wrappedValue.removeAll()
try? FileManager.default.removeItem(at: PosterBoardManager.getTendiesStoreURL()) try? FileManager.default.removeItem(at: PosterBoardManager.getTendiesStoreURL())
Haptic.shared.notify(.success) Haptic.shared.notify(.success)
// UIApplication.shared.dismissAlert(animated: true) // UIApplication.shared.dismissAlert(animated: true)
@@ -112,11 +112,7 @@ struct ContentView: View {
.fileImporter(isPresented: $showTendiesImporter, allowedContentTypes: [UTType(filenameExtension: "tendies", conformingTo: .data)!], allowsMultipleSelection: true, onCompletion: { result in .fileImporter(isPresented: $showTendiesImporter, allowedContentTypes: [UTType(filenameExtension: "tendies", conformingTo: .data)!], allowsMultipleSelection: true, onCompletion: { result in
switch result { switch result {
case .success(let url): case .success(let url):
if selectedTendies.wrappedValue == nil { selectedTendies.wrappedValue.append(contentsOf: url)
selectedTendies.wrappedValue = url
} else {
selectedTendies.wrappedValue?.append(contentsOf: url)
}
case .failure(let error): case .failure(let error):
lastError = error.localizedDescription lastError = error.localizedDescription
showErrorAlert.toggle() showErrorAlert.toggle()
@@ -137,12 +133,10 @@ struct ContentView: View {
} }
func delete(at offsets: IndexSet) { func delete(at offsets: IndexSet) {
if selectedTendies.wrappedValue != nil { selectedTendies.wrappedValue.remove(atOffsets: offsets)
selectedTendies.wrappedValue?.remove(atOffsets: offsets)
}
} }
init(selectedTendies: Binding<[URL]?>) { init(selectedTendies: Binding<[URL]>) {
self.selectedTendies = selectedTendies self.selectedTendies = selectedTendies
// Fix file picker // Fix file picker
let fixMethod = class_getInstanceMethod(UIDocumentPickerViewController.self, #selector(UIDocumentPickerViewController.fix_init(forOpeningContentTypes:asCopy:)))! let fixMethod = class_getInstanceMethod(UIDocumentPickerViewController.self, #selector(UIDocumentPickerViewController.fix_init(forOpeningContentTypes:asCopy:)))!