Skip to content

Commit

Permalink
Add option to remove and add tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-128 committed Feb 5, 2025
1 parent edf8304 commit 1fcee35
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 23 deletions.
9 changes: 9 additions & 0 deletions Localization/Localizations/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,9 @@
},
"Add New Category" : {

},
"Add New Tag" : {

},
"Add Server" : {
"localizations" : {
Expand Down Expand Up @@ -686,6 +689,9 @@
}
}
}
},
"Add Tag" : {

},
"Add Task" : {
"localizations" : {
Expand Down Expand Up @@ -6603,6 +6609,9 @@
}
}
}
},
"Tag Name" : {

},
"Tags" : {
"localizations" : {
Expand Down
24 changes: 24 additions & 0 deletions qBitControl/Classes/qBittorrentClass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,30 @@ class qBittorrent {
qBitRequest.requestTorrentManagement(request: request, statusCode: {code in result((code == 200))})
}

static func removeTag(tag: String, then callback: ((Int) -> Void)?) {
let request = qBitRequest.prepareURLRequest(path: "/api/v2/torrents/deleteTags", queryItems: [
URLQueryItem(name: "tags", value: tag)
])

if let callback = callback {
qBitRequest.requestTorrentManagement(request: request, statusCode: {status in callback(status ?? 0)})
} else {
qBitRequest.requestTorrentManagement(request: request, statusCode: {_ in})
}
}

static func addTag(tag: String, then callback: ((Int) -> Void)?) {
let request = qBitRequest.prepareURLRequest(path: "/api/v2/torrents/createTags", queryItems: [
URLQueryItem(name: "tags", value: tag)
])

if let callback = callback {
qBitRequest.requestTorrentManagement(request: request, statusCode: {status in callback(status ?? 0)})
} else {
qBitRequest.requestTorrentManagement(request: request, statusCode: {_ in})
}
}

static func pauseTorrent(hash: String) {
// qBittorrent 5.0.0 changes pause route to stop and resume to start
let suffix = self.version.major == 5 ? "stop" : "pause"
Expand Down
107 changes: 85 additions & 22 deletions qBitControl/Components/ChangeTagsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,26 @@ struct ChangeTagsView: View {

public var onTagsChange: ((Set<String>) -> Void)?

@State private var showAddTagAlert = false
@State private var newTagName = ""

init(torrentHash: String, selectedTags: [String]) {
self.torrentHash = torrentHash
self.selectedTags = Set(selectedTags)
}

init(onTagsChange: @escaping (Set<String>) -> Void) {
self.selectedTags = Set()
init(selectedTags: Set<String>, onTagsChange: @escaping (Set<String>) -> Void) {
self.selectedTags = selectedTags
self.onTagsChange = onTagsChange
}

func removeTag(tag: String) {
func getTags() {
qBittorrent.getTags(completionHandler: { tags in
self.allTags = tags.sorted()
})
}

func unsetTag(tag: String) {
if let hash = self.torrentHash {
qBittorrent.unsetTag(hash: hash, tag: tag, result: { isSuccess in
if(isSuccess) { selectedTags.remove(tag) }
Expand All @@ -32,7 +41,7 @@ struct ChangeTagsView: View {
}
}

func addTag(tag: String) {
func setTag(tag: String) {
if let hash = self.torrentHash {
qBittorrent.setTag(hash: hash, tag: tag, result: { isSuccess in
if(isSuccess) { selectedTags.insert(tag) }
Expand All @@ -46,36 +55,90 @@ struct ChangeTagsView: View {
}
}

func addTag() {
qBittorrent.addTag(tag: newTagName, then: { status in
if(status == 200) {
self.getTags()
}
})
}

func removeTag(tag: String) {
qBittorrent.removeTag(tag: tag, then: { status in
if(status == 200) {
self.getTags()
self.clearSelectedTags()
}
})
}

func clearSelectedTags() {
if let onTagsChange = self.onTagsChange {
self.selectedTags = self.selectedTags.filter { tag in
return self.allTags.contains(tag)
}
onTagsChange(selectedTags)
}
}


var body: some View {
VStack {
Form {
Section(header: Text("Add Tag")) {
Button {
showAddTagAlert = true
} label: {
Label("Add Tag", systemImage: "plus.circle")
}.alert("Add New Tag", isPresented: $showAddTagAlert, actions: {
TextField("Tag Name", text: $newTagName)
Button("Add", action: {
self.addTag()
newTagName = ""
})
Button("Cancel", role: .cancel, action: {
newTagName = ""
})
})
}

if allTags.count > 1 {
List(allTags, id: \.self) { tag in
Button {
if selectedTags.contains(tag) {
removeTag(tag: tag)
} else {
addTag(tag: tag)
}
} label: {
HStack {
Text(tag)
.foregroundStyle(.foreground)
Spacer()
if selectedTags.contains(tag) {
Image(systemName: "checkmark")
.foregroundColor(.accentColor)
Section {
List {
ForEach(allTags, id: \.self) { tag in
Button {
if selectedTags.contains(tag) {
unsetTag(tag: tag)
} else {
setTag(tag: tag)
}
} label: {
HStack {
Text(tag)
.foregroundStyle(.foreground)
Spacer()
if selectedTags.contains(tag) {
Image(systemName: "checkmark")
.foregroundColor(.accentColor)
}
}
}
}

.onDelete(perform: { atOffsets in
atOffsets.forEach { index in
self.removeTag(tag: self.allTags[index])
}

self.allTags.remove(atOffsets: atOffsets)
})
}
}
}
}
.navigationTitle("Tags")
}.onAppear() {
qBittorrent.getTags(completionHandler: { _tags in
self.allTags = _tags.sorted()
})
self.getTags()
}
}
}
2 changes: 1 addition & 1 deletion qBitControl/Views/TorrentViews/TorrentAddView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ struct TorrentAddView: View {
}

NavigationLink {
ChangeTagsView(onTagsChange: { selectedTags in
ChangeTagsView(selectedTags: viewModel.selectedTags, onTagsChange: { selectedTags in
viewModel.selectedTags = selectedTags
})
} label: {
Expand Down

0 comments on commit 1fcee35

Please sign in to comment.