Skip to content

Commit

Permalink
Improve 'change category' view
Browse files Browse the repository at this point in the history
- Implement adding categories
- Add remove category
- Standardize category pickers to use the same view
- Improve refresh time after category change
  • Loading branch information
Michael-128 committed Feb 4, 2025
1 parent baf9235 commit 59f92ff
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 33 deletions.
28 changes: 28 additions & 0 deletions qBitControl/Classes/qBittorrentClass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,34 @@ class qBittorrent {
qBitRequest.requestTorrentManagement(request: request, statusCode: {_ in})
}

static func addCategory(category: String, savePath: String?, then callback: ((Int) -> Void)?) {
var params = [ URLQueryItem(name: "category", value: category) ]

if let savePath = savePath {
params.append(URLQueryItem(name: "savePath", value: savePath))
}

let request = qBitRequest.prepareURLRequest(path: "/api/v2/torrents/createCategory", queryItems: params)

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

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

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

static func getTags(completionHandler: @escaping ([String]) -> Void) {
let request = qBitRequest.prepareURLRequest(path: "/api/v2/torrents/tags")

Expand Down
60 changes: 43 additions & 17 deletions qBitControl/Components/ChangeCategoryView.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import SwiftUI

struct ChangeCategoryView: View {

@State var torrentHash: String

@State var torrentHash: String?
@State private var categories: [Category] = []

@State var category: String

@State private var showAddCategoryAlert = false
@State private var newCategoryName = ""

public var onCategoryChange: ((Category) -> Void)?

private func getCategories() {
qBittorrent.getCategories(completionHandler: { _categories in
var categories = _categories.map { $0.value }
categories.sort { $0.name < $1.name }

self.categories = categories
})
}

var body: some View {
VStack {
Form {
Expand All @@ -22,8 +30,11 @@ struct ChangeCategoryView: View {
}.alert("Add New Category", isPresented: $showAddCategoryAlert, actions: {
TextField("Category Name", text: $newCategoryName)
Button("Add", action: {
// Add category
print(newCategoryName)
qBittorrent.addCategory(category: newCategoryName, savePath: nil, then: { status in
if(status == 200) {
self.getCategories()
}
})
newCategoryName = ""
})
Button("Cancel", role: .cancel, action: {
Expand All @@ -33,24 +44,39 @@ struct ChangeCategoryView: View {
}

if categories.count > 1 {
Picker("Categories", selection: $category) {
Text("Uncategorized").tag("")
List {
ForEach(categories, id: \.self) { category in
Text(category.name).tag(category.name)
Button {
if(self.category != category.name) { self.category = category.name }
} label: {
HStack {
Text(category.name)
.foregroundStyle(.foreground)
Spacer()
if(self.category == category.name) {
Image(systemName: "checkmark")
.foregroundColor(.accentColor)
}
}
}
}
}.pickerStyle(.inline)
.onDelete(perform: { offsets in
for index in offsets {
let category = categories[index].name
qBittorrent.removeCategory(category: category, then: {status in print(status)})
}

categories.remove(atOffsets: offsets)
})
}
}
}
.navigationTitle("Categories")
}.onAppear() {
qBittorrent.getCategories(completionHandler: { _categories in
var categories = _categories.map { $0.value }
categories.sort { $0.name < $1.name }

self.categories = categories
})
self.getCategories()
}.onChange(of: category) { category in
qBittorrent.setCategory(hash: torrentHash, category: category)
if let onCategoryChange = self.onCategoryChange, let category = categories.first(where: { $0.name == category }) { onCategoryChange(category) }
if let hash = self.torrentHash { qBittorrent.setCategory(hash: hash, category: category) }
}
}
}
23 changes: 8 additions & 15 deletions qBitControl/Views/TorrentViews/TorrentAddView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,28 +138,21 @@ struct TorrentAddView: View {
Section(header: Text("Save Path")) { TextField("Path", text: $viewModel.savePath) }

Group {
Section(header: Text("Info")) {
Picker("Category", selection: $viewModel.category) {
if !viewModel.categories.isEmpty {
ForEach(viewModel.categories, id: \.self) { category in
Text(category.name).tag(category.name)
}
}
}.onChange(of: viewModel.category) { category in
if !viewModel.autoTmmEnabled { viewModel.savePath = category.savePath }
Section(header: Text("Info")) {
NavigationLink {
ChangeCategoryView(category: viewModel.category.name, onCategoryChange: { category in
viewModel.category = category
if !viewModel.autoTmmEnabled { viewModel.savePath = category.savePath }
})
} label: {
CustomLabelView(label: "Category", value: viewModel.category.name)
}

NavigationLink {
changeTagsView()
} label: {
CustomLabelView(label: "Tags", value: viewModel.getTag())
}

// Picker("Tags", selection: $viewModel.tags) {
// if(!viewModel.tagsArr.isEmpty) {
// ForEach(viewModel.tagsArr, id: \.self) { tag in Text(tag).tag(tag) }
// }
// }
}
}

Expand Down
4 changes: 3 additions & 1 deletion qBitControl/Views/TorrentViews/TorrentDetailsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ struct TorrentDetailsView: View {
CustomLabelView(label: "Added On", value: viewModel.getAddedOn())

NavigationLink {
ChangeCategoryView(torrentHash: viewModel.torrent.hash, category: viewModel.torrent.category)
ChangeCategoryView(torrentHash: viewModel.torrent.hash, category: viewModel.torrent.category, onCategoryChange: {_ in
viewModel.getTorrent()
})
} label: {
CustomLabelView(label: "Categories", value: viewModel.getCategory())
}
Expand Down

0 comments on commit 59f92ff

Please sign in to comment.