Ferrite-backup/Ferrite/Views/SheetViews/ActionChoiceView.swift
kingbri f622b7af05 Actions: Fix default action settings
Since actions use a new API, update default actions to use the
same API rather than the legacy models. If an action is removed,
a prompt will tell the user to change their default debrid/magnet
action and default to the choice sheet.

Also add extra UI fixes and cleanup.

Signed-off-by: kingbri <bdashore3@proton.me>
2023-03-01 18:31:29 -05:00

149 lines
6.1 KiB
Swift

//
// MagnetChoiceView.swift
// Ferrite
//
// Created by Brian Dashore on 7/20/22.
//
import SwiftUI
import SwiftUIX
struct ActionChoiceView: View {
@Environment(\.presentationMode) var presentationMode
@EnvironmentObject var scrapingModel: ScrapingViewModel
@EnvironmentObject var debridManager: DebridManager
@EnvironmentObject var navModel: NavigationViewModel
@EnvironmentObject var pluginManager: PluginManager
@FetchRequest(
entity: Action.entity(),
sortDescriptors: []
) var actions: FetchedResults<Action>
@State private var showLinkCopyAlert = false
@State private var showMagnetCopyAlert = false
var body: some View {
NavView {
Form {
Section(header: "Now Playing") {
VStack(alignment: .leading, spacing: 5) {
Text(navModel.selectedTitle)
.font(.callout)
.lineLimit(navModel.selectedBatchTitle.isEmpty ? .max : 1)
if !navModel.selectedBatchTitle.isEmpty {
Text(navModel.selectedBatchTitle)
.foregroundColor(.gray)
.font(.subheadline)
}
}
}
if !debridManager.downloadUrl.isEmpty {
Section(header: "Debrid options") {
ForEach(actions, id: \.id) { action in
if action.requires.contains(ActionRequirement.debrid.rawValue) {
ListRowButtonView(action.name, systemImage: "arrow.up.forward.app.fill") {
pluginManager.runDeeplinkAction(action, urlString: debridManager.downloadUrl)
}
}
}
ListRowButtonView("Copy download URL", systemImage: "doc.on.doc.fill") {
UIPasteboard.general.string = debridManager.downloadUrl
showLinkCopyAlert.toggle()
}
.backport.alert(
isPresented: $showLinkCopyAlert,
title: "Copied",
message: "Download link copied successfully",
buttons: [AlertButton("OK")]
)
ListRowButtonView("Share download URL", systemImage: "square.and.arrow.up.fill") {
if let url = URL(string: debridManager.downloadUrl) {
navModel.activityItems = [url]
navModel.showLocalActivitySheet.toggle()
}
}
}
}
if !navModel.resultFromCloud {
Section(header: "Magnet options") {
ForEach(actions, id: \.id) { action in
if action.requires.contains(ActionRequirement.magnet.rawValue) {
ListRowButtonView(action.name, systemImage: "arrow.up.forward.app.fill") {
pluginManager.runDeeplinkAction(action, urlString: navModel.selectedMagnet?.link)
}
}
}
ListRowButtonView("Copy magnet", systemImage: "doc.on.doc.fill") {
UIPasteboard.general.string = navModel.selectedMagnet?.link
showMagnetCopyAlert.toggle()
}
.backport.alert(
isPresented: $showMagnetCopyAlert,
title: "Copied",
message: "Magnet link copied successfully",
buttons: [AlertButton("OK")]
)
ListRowButtonView("Share magnet", systemImage: "square.and.arrow.up.fill") {
if let magnetLink = navModel.selectedMagnet?.link,
let url = URL(string: magnetLink)
{
navModel.activityItems = [url]
navModel.showLocalActivitySheet.toggle()
}
}
}
}
}
.backport.tint(.primary)
.sheet(isPresented: $navModel.showLocalActivitySheet) {
if #available(iOS 16, *) {
AppActivityView(activityItems: navModel.activityItems)
.presentationDetents([.medium, .large])
} else {
AppActivityView(activityItems: navModel.activityItems)
}
}
.backport.alert(
isPresented: $pluginManager.showBrokenDefaultActionAlert,
title: "Action not found",
message:
"The default action could not be run. The action choice sheet has been opened. \n\n" +
"Please check your default actions in Settings"
)
.onDisappear {
debridManager.downloadUrl = ""
navModel.selectedTitle = ""
navModel.selectedBatchTitle = ""
navModel.resultFromCloud = false
}
.navigationTitle("Link actions")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button("Done") {
debridManager.downloadUrl = ""
navModel.selectedTitle = ""
navModel.selectedBatchTitle = ""
presentationMode.wrappedValue.dismiss()
}
}
}
}
}
}
struct ActionChoiceView_Previews: PreviewProvider {
static var previews: some View {
ActionChoiceView()
}
}