Editing playlists

This commit is contained in:
Arkadiusz Fal 2021-07-08 19:18:36 +02:00
parent 88a733da34
commit 31bd2f7fe6
9 changed files with 255 additions and 132 deletions

View File

@ -3,22 +3,35 @@ import SwiftUI
struct CoverSectionView<Content: View>: View {
let title: String?
let rowsView: Content
let actionsView: Content
let divider: Bool
let inline: Bool
init(_ title: String? = nil, divider: Bool = true, @ViewBuilder rowsView: @escaping () -> Content) {
init(_ title: String? = nil, divider: Bool = true, inline: Bool = false, @ViewBuilder actionsView: @escaping () -> Content) {
self.title = title
self.divider = divider
self.rowsView = rowsView()
self.inline = inline
self.actionsView = actionsView()
}
var body: some View {
VStack(alignment: .leading) {
if title != nil {
if inline {
HStack {
if title != nil {
sectionTitle
}
Spacer()
actionsView
}
} else if title != nil {
sectionTitle
}
rowsView
if !inline {
actionsView
}
}
if divider {

View File

@ -1,74 +0,0 @@
import SwiftUI
import SwiftyJSON
struct NewPlaylistView: View {
@State private var name = ""
@State private var visibility = PlaylistVisibility.public
@State private var valid = false
@Binding var createdPlaylist: Playlist?
@Environment(\.dismiss) private var dismiss
var body: some View {
HStack {
Spacer()
VStack {
Spacer()
CoverSectionView("New Playlist") {
CoverSectionRowView("Name") {
TextField("Playlist Name", text: $name, onCommit: validate)
.frame(maxWidth: 450)
}
CoverSectionRowView("Visibility") { visibilityButton }
}
CoverSectionRowView {
Button("Create", action: createPlaylistAndDismiss).disabled(!valid)
}
Spacer()
}
.frame(maxWidth: 800)
Spacer()
}
.background(.thinMaterial)
.onAppear {
createdPlaylist = nil
}
}
func validate() {
valid = !name.isEmpty
}
func createPlaylistAndDismiss() {
let resource = InvidiousAPI.shared.playlists
let body = ["title": name, "privacy": visibility.rawValue]
resource.request(.post, json: body).onSuccess { response in
if let playlist: Playlist = response.typedContent() {
createdPlaylist = playlist
dismiss()
}
}
}
var visibilityButton: some View {
Button(self.visibility.name) {
self.visibility = self.visibility.next()
}
.contextMenu {
ForEach(PlaylistVisibility.allCases) { visibility in
Button(visibility.name) {
self.visibility = visibility
}
}
}
}
}

View File

@ -0,0 +1,120 @@
import Siesta
import SwiftUI
struct PlaylistFormView: View {
@State private var name = ""
@State private var visibility = PlaylistVisibility.public
@State private var valid = false
@State private var showingDeleteConfirmation = false
@Binding var playlist: Playlist!
@Environment(\.dismiss) private var dismiss
var editing: Bool {
playlist != nil
}
var body: some View {
HStack {
Spacer()
VStack {
Spacer()
CoverSectionView(editing ? "Edit Playlist" : "Create Playlist") {
CoverSectionRowView("Name") {
TextField("Playlist Name", text: $name, onCommit: validate)
.frame(maxWidth: 450)
}
CoverSectionRowView("Visibility") { visibilityButton }
}
CoverSectionRowView {
Button("Save", action: submitForm).disabled(!valid)
}
if editing {
CoverSectionView("Delete Playlist", divider: false, inline: true) { deletePlaylistButton }
.padding(.top, 50)
}
Spacer()
}
.frame(maxWidth: 800)
Spacer()
}
.background(.thinMaterial)
.onAppear {
guard editing else {
return
}
self.name = self.playlist.title
self.visibility = self.playlist.visibility
validate()
}
}
func validate() {
valid = !name.isEmpty
}
func submitForm() {
guard valid else {
return
}
let body = ["title": name, "privacy": visibility.rawValue]
resource.request(editing ? .patch : .post, json: body).onSuccess { response in
if let createdPlaylist: Playlist = response.typedContent() {
playlist = createdPlaylist
}
dismiss()
}
}
var resource: Resource {
editing ? InvidiousAPI.shared.playlist(playlist.id) : InvidiousAPI.shared.playlists
}
var visibilityButton: some View {
Button(self.visibility.name) {
self.visibility = self.visibility.next()
}
.contextMenu {
ForEach(PlaylistVisibility.allCases) { visibility in
Button(visibility.name) {
self.visibility = visibility
}
}
}
}
var deletePlaylistButton: some View {
Button("Delete", role: .destructive) {
showingDeleteConfirmation = true
}.alert(isPresented: $showingDeleteConfirmation) {
Alert(
title: Text("Are you sure you want to delete playlist?"),
message: Text("Playlist \"\(playlist.title)\" will be deleted.\nIt cannot be undone."),
primaryButton: .destructive(Text("Delete"), action: deletePlaylistAndDismiss),
secondaryButton: .cancel()
)
}
}
func deletePlaylistAndDismiss() {
let resource = InvidiousAPI.shared.playlist(playlist.id)
resource.request(.delete).onSuccess { _ in
playlist = nil
dismiss()
}
}
}

View File

@ -1,14 +1,19 @@
import Defaults
import Siesta
import SwiftUI
struct PlaylistsView: View {
@ObservedObject private var store = Store<[Playlist]>()
@Default(.selectedPlaylistID) private var selectedPlaylistID
@State private var selectedPlaylist: Playlist?
@State private var showingNewPlaylist = false
@State private var createdPlaylist: Playlist?
@State private var showingEditPlaylist = false
@State private var editedPlaylist: Playlist?
var resource: Resource {
InvidiousAPI.shared.playlists
}
@ -23,6 +28,10 @@ struct PlaylistsView: View {
HStack {
selectPlaylistButton
if currentPlaylist != nil {
editPlaylistButton
}
newPlaylistButton
}
.scaleEffect(0.85)
@ -35,20 +44,44 @@ struct PlaylistsView: View {
}
}
.fullScreenCover(isPresented: $showingNewPlaylist, onDismiss: selectCreatedPlaylist) {
NewPlaylistView(createdPlaylist: $createdPlaylist)
PlaylistFormView(playlist: $createdPlaylist)
}
.fullScreenCover(isPresented: $showingEditPlaylist, onDismiss: selectEditedPlaylist) {
PlaylistFormView(playlist: $editedPlaylist)
}
.onAppear {
resource.loadIfNeeded()
resource.loadIfNeeded()?.onSuccess { _ in
selectPlaylist(selectedPlaylistID)
}
}
}
func selectPlaylist(_ id: String?) {
selectedPlaylist = store.collection.first { $0.id == id }
selectedPlaylistID = id
}
func selectCreatedPlaylist() {
guard createdPlaylist != nil else {
return
}
resource.load().onSuccess { _ in
self.selectedPlaylist = store.collection.first { $0 == createdPlaylist }
self.selectPlaylist(createdPlaylist?.id)
self.createdPlaylist = nil
}
}
func selectEditedPlaylist() {
if editedPlaylist == nil {
selectPlaylist(nil)
}
resource.load().onSuccess { _ in
selectPlaylist(editedPlaylist?.id)
self.editedPlaylist = nil
}
}
@ -62,17 +95,26 @@ struct PlaylistsView: View {
return
}
selectedPlaylist = store.collection.next(after: currentPlaylist!)
selectPlaylist(store.collection.next(after: currentPlaylist!)?.id)
}
.contextMenu {
ForEach(store.collection) { playlist in
Button(playlist.title) {
selectedPlaylist = playlist
selectPlaylist(playlist.id)
}
}
}
}
var editPlaylistButton: some View {
Button(action: {
self.editedPlaylist = self.currentPlaylist
self.showingEditPlaylist = true
}) {
Image(systemName: "pencil")
}
}
var newPlaylistButton: some View {
Button(action: { self.showingNewPlaylist = true }) {
Image(systemName: "plus")

View File

@ -54,7 +54,7 @@ final class InvidiousAPI: Service {
content.json.arrayValue.map(Playlist.init)
}
configureTransformer("/auth/playlists", requestMethods: [.post]) { (content: Entity<Data>) -> Playlist in
configureTransformer("/auth/playlists", requestMethods: [.post, .patch]) { (content: Entity<Data>) -> Playlist in
// hacky, to verify if possible to get it in easier way
Playlist(JSON(parseJSON: String(data: content.content, encoding: .utf8)!))
}
@ -108,6 +108,10 @@ final class InvidiousAPI: Service {
resource("/auth/playlists")
}
func playlist(_ id: String) -> Resource {
resource("/auth/playlists/\(id)")
}
func search(_ query: SearchQuery) -> Resource {
var resource = resource("/search")
.withParam("q", searchQuery(query.query))

View File

@ -4,19 +4,19 @@ import SwiftyJSON
struct Playlist: Identifiable, Equatable, Hashable {
let id: String
var title: String
var description: String
var visibility: PlaylistVisibility
var videos = [Video]()
init(_ json: JSON) {
id = json["playlistId"].stringValue
title = json["title"].stringValue
description = json["description"].stringValue
visibility = json["isListed"].boolValue ? .public : .private
videos = json["videos"].arrayValue.map { Video($0) }
}
static func == (lhs: Playlist, rhs: Playlist) -> Bool {
lhs.id == rhs.id
lhs.id == rhs.id && lhs.title == rhs.title && lhs.visibility == rhs.visibility
}
func hash(into hasher: inout Hasher) {

View File

@ -54,12 +54,12 @@
373CFADB269663F1003CB2C6 /* Thumbnail.swift in Sources */ = {isa = PBXBuildFile; fileRef = 373CFADA269663F1003CB2C6 /* Thumbnail.swift */; };
373CFADC269663F1003CB2C6 /* Thumbnail.swift in Sources */ = {isa = PBXBuildFile; fileRef = 373CFADA269663F1003CB2C6 /* Thumbnail.swift */; };
373CFADD269663F1003CB2C6 /* Thumbnail.swift in Sources */ = {isa = PBXBuildFile; fileRef = 373CFADA269663F1003CB2C6 /* Thumbnail.swift */; };
373CFADF2696F88B003CB2C6 /* NewPlaylistView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 373CFADE2696F861003CB2C6 /* NewPlaylistView.swift */; };
373CFAE02696F88B003CB2C6 /* NewPlaylistView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 373CFADE2696F861003CB2C6 /* NewPlaylistView.swift */; };
373CFAE12696F88B003CB2C6 /* NewPlaylistView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 373CFADE2696F861003CB2C6 /* NewPlaylistView.swift */; };
373CFAE326974812003CB2C6 /* PlaylistVisibility.swift in Sources */ = {isa = PBXBuildFile; fileRef = 373CFAE226974812003CB2C6 /* PlaylistVisibility.swift */; };
373CFAE426974812003CB2C6 /* PlaylistVisibility.swift in Sources */ = {isa = PBXBuildFile; fileRef = 373CFAE226974812003CB2C6 /* PlaylistVisibility.swift */; };
373CFAE526974812003CB2C6 /* PlaylistVisibility.swift in Sources */ = {isa = PBXBuildFile; fileRef = 373CFAE226974812003CB2C6 /* PlaylistVisibility.swift */; };
373CFAEB26975CBF003CB2C6 /* PlaylistFormView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 373CFAEA26975CBF003CB2C6 /* PlaylistFormView.swift */; };
373CFAEC26975CBF003CB2C6 /* PlaylistFormView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 373CFAEA26975CBF003CB2C6 /* PlaylistFormView.swift */; };
373CFAED26975CBF003CB2C6 /* PlaylistFormView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 373CFAEA26975CBF003CB2C6 /* PlaylistFormView.swift */; };
3741B5302676213400125C5E /* PlayerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3741B52F2676213400125C5E /* PlayerViewController.swift */; };
376578852685429C00D4EA09 /* CaseIterable+Next.swift in Sources */ = {isa = PBXBuildFile; fileRef = 376578842685429C00D4EA09 /* CaseIterable+Next.swift */; };
376578862685429C00D4EA09 /* CaseIterable+Next.swift in Sources */ = {isa = PBXBuildFile; fileRef = 376578842685429C00D4EA09 /* CaseIterable+Next.swift */; };
@ -229,8 +229,8 @@
373CFAD2269662AB003CB2C6 /* SearchDate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchDate.swift; sourceTree = "<group>"; };
373CFAD6269662CD003CB2C6 /* SearchDuration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchDuration.swift; sourceTree = "<group>"; };
373CFADA269663F1003CB2C6 /* Thumbnail.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Thumbnail.swift; sourceTree = "<group>"; };
373CFADE2696F861003CB2C6 /* NewPlaylistView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewPlaylistView.swift; sourceTree = "<group>"; };
373CFAE226974812003CB2C6 /* PlaylistVisibility.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlaylistVisibility.swift; sourceTree = "<group>"; };
373CFAEA26975CBF003CB2C6 /* PlaylistFormView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlaylistFormView.swift; sourceTree = "<group>"; };
3741B52F2676213400125C5E /* PlayerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerViewController.swift; sourceTree = "<group>"; };
376578842685429C00D4EA09 /* CaseIterable+Next.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CaseIterable+Next.swift"; sourceTree = "<group>"; };
376578882685471400D4EA09 /* Playlist.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Playlist.swift; sourceTree = "<group>"; };
@ -423,10 +423,10 @@
37AAF2892673AB89007FC770 /* ChannelView.swift */,
373CFAC126966159003CB2C6 /* CoverSectionRowView.swift */,
373CFABD26966115003CB2C6 /* CoverSectionView.swift */,
373CFADE2696F861003CB2C6 /* NewPlaylistView.swift */,
37B76E95268747C900CE5671 /* OptionsView.swift */,
37D4B1822671681B00C925CA /* PlayerView.swift */,
3741B52F2676213400125C5E /* PlayerViewController.swift */,
373CFAEA26975CBF003CB2C6 /* PlaylistFormView.swift */,
376578902685490700D4EA09 /* PlaylistsView.swift */,
37AAF27D26737323007FC770 /* PopularVideosView.swift */,
373CFAC52696617C003CB2C6 /* SearchOptionsView.swift */,
@ -753,7 +753,6 @@
37EAD86F267B9ED100D9E01B /* Segment.swift in Sources */,
376578892685471400D4EA09 /* Playlist.swift in Sources */,
373CFADB269663F1003CB2C6 /* Thumbnail.swift in Sources */,
373CFAE12696F88B003CB2C6 /* NewPlaylistView.swift in Sources */,
37CEE4B52677B628005A1EFE /* StreamType.swift in Sources */,
37C7A1DC267CE9D90010EAD6 /* Profile.swift in Sources */,
373CFAC026966149003CB2C6 /* CoverSectionView.swift in Sources */,
@ -778,6 +777,7 @@
37AAF2A026741C97007FC770 /* SubscriptionsView.swift in Sources */,
373CFAD7269662CD003CB2C6 /* SearchDuration.swift in Sources */,
373CFACF26966290003CB2C6 /* SearchSortOrder.swift in Sources */,
373CFAEB26975CBF003CB2C6 /* PlaylistFormView.swift in Sources */,
377FC7DF267A082200A6BBAF /* VideosListView.swift in Sources */,
372915E62687E3B900F5A35B /* Defaults.swift in Sources */,
37D4B19726717E1500C925CA /* Video.swift in Sources */,
@ -838,7 +838,7 @@
37D4B0E52671614900C925CA /* PearvidiousApp.swift in Sources */,
373CFAE426974812003CB2C6 /* PlaylistVisibility.swift in Sources */,
37CEE4BA2677B63F005A1EFE /* StreamResolution.swift in Sources */,
373CFAE02696F88B003CB2C6 /* NewPlaylistView.swift in Sources */,
373CFAEC26975CBF003CB2C6 /* PlaylistFormView.swift in Sources */,
37977584268922F600DD52A8 /* InvidiousAPI.swift in Sources */,
37B17DA1268A1F89006AEE9B /* VideoContextMenuView.swift in Sources */,
373CFAD4269662AB003CB2C6 /* SearchDate.swift in Sources */,
@ -886,7 +886,6 @@
37EAD86D267B9C5600D9E01B /* SponsorBlockAPI.swift in Sources */,
3765788B2685471400D4EA09 /* Playlist.swift in Sources */,
373CFADD269663F1003CB2C6 /* Thumbnail.swift in Sources */,
373CFADF2696F88B003CB2C6 /* NewPlaylistView.swift in Sources */,
37C7A1DE267CE9D90010EAD6 /* Profile.swift in Sources */,
3741B5302676213400125C5E /* PlayerViewController.swift in Sources */,
373CFABE26966148003CB2C6 /* CoverSectionView.swift in Sources */,
@ -911,6 +910,7 @@
37D4B19926717E1500C925CA /* Video.swift in Sources */,
373CFAD9269662CD003CB2C6 /* SearchDuration.swift in Sources */,
373CFAD126966290003CB2C6 /* SearchSortOrder.swift in Sources */,
373CFAED26975CBF003CB2C6 /* PlaylistFormView.swift in Sources */,
3705B184267B4E4900704544 /* TrendingCategory.swift in Sources */,
37AAF2A226741C97007FC770 /* SubscriptionsView.swift in Sources */,
372915E82687E3B900F5A35B /* Defaults.swift in Sources */,

View File

@ -23,49 +23,65 @@
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "8A4F4C9F-7372-4E74-9535-16BB3F2A3493"
uuid = "FCFDD746-6F29-43D8-B813-CCDFF1B5AE90"
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Apple TV/VideosView.swift"
filePath = "Model/InvidiousAPI.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "13"
endingLineNumber = "13"
startingLineNumber = "59"
endingLineNumber = "59"
landmarkName = "init()"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "E59B76C5-4001-4E08-92DC-A345E7954D4D"
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Apple TV/NewPlaylistView.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "32"
endingLineNumber = "32"
landmarkName = "body"
landmarkType = "24">
<Locations>
<Location
uuid = "8A4F4C9F-7372-4E74-9535-16BB3F2A3493 - 5e5aadfff65b9d"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "Pearvidious__Apple_TV_.VideosView.body.getter : some"
moduleName = "Pearvidious (Apple TV)"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/arek/Developer/Pearvidious/Apple%20TV/VideosView.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "15"
endingLineNumber = "15"
offsetFromSymbolStart = "660">
</Location>
<Location
uuid = "8A4F4C9F-7372-4E74-9535-16BB3F2A3493 - d2d1bd8291983832"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "closure #1 () -&gt; Swift.Optional&lt;SwiftUI._ConditionalContent&lt;Pearvidious__Apple_TV_.VideosCellsView, Pearvidious__Apple_TV_.VideosListView&gt;&gt; in Pearvidious__Apple_TV_.VideosView.body.getter : some"
moduleName = "Pearvidious (Apple TV)"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/arek/Developer/Pearvidious/Apple%20TV/VideosView.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "15"
endingLineNumber = "15"
offsetFromSymbolStart = "1920">
</Location>
</Locations>
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "2B9757FC-6E4B-4236-9E5D-48DB8534C673"
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Apple TV/PlaylistFormView.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "75"
endingLineNumber = "75"
landmarkName = "submitForm()"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "69B09B52-FD39-4B67-A3C1-1344483D1C9F"
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Apple TV/PlaylistFormView.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "76"
endingLineNumber = "76"
landmarkName = "submitForm()"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>

View File

@ -11,4 +11,6 @@ extension Defaults.Keys {
static let searchDuration = Key<SearchDuration?>("searchDuration", default: nil)
static let openVideoID = Key<String>("videoID", default: "")
static let showingVideoDetails = Key<Bool>("showingVideoDetails", default: false)
static let selectedPlaylistID = Key<String?>("selectedPlaylistID")
}