Skip to content

Commit

Permalink
Add tag multi-select for 'torrent add view'
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-128 committed Feb 3, 2025
1 parent 0b008f9 commit 377a36a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 14 deletions.
4 changes: 2 additions & 2 deletions qBitControl.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = qBitControl/qBitControl.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 12;
CURRENT_PROJECT_VERSION = 13;
DEVELOPMENT_ASSET_PATHS = "";
DEVELOPMENT_TEAM = 626XV358Y5;
ENABLE_PREVIEWS = YES;
Expand Down Expand Up @@ -832,7 +832,7 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = qBitControl/qBitControl.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 12;
CURRENT_PROJECT_VERSION = 13;
DEVELOPMENT_ASSET_PATHS = "";
DEVELOPMENT_TEAM = 626XV358Y5;
ENABLE_PREVIEWS = YES;
Expand Down
20 changes: 13 additions & 7 deletions qBitControl/ViewModels/TorrentView/TorrentAddViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ class TorrentAddViewModel: ObservableObject {

@Published var cookie = ""
@Published var category: Category = defaultCategory
@Published var tags = defaultTag

var tags: [String] { Array(selectedTags).sorted(by: <) }
var tagsString: String { tags.joined(separator: ",") }
@Published var selectedTags: Set<String> = Set()

@Published var skipChecking = false
@Published var paused = false
Expand All @@ -40,12 +43,16 @@ class TorrentAddViewModel: ObservableObject {
@Published var seedingTimeLimit = ""

@Published var categories: [Category] = [defaultCategory]
@Published var tagsArr: [String] = [defaultTag]
@Published var tagsArr: [String] = []

@Published var isAppeared = false

init(torrentUrls: [URL]) {
self.torrentUrls = torrentUrls
}

func getTag() -> String { tags.count > 1 ? "\(tags.count)" + " Tags" : (tags.first ?? "Untagged") }

func checkTorrentType() -> Void {
if torrentUrls.isEmpty { return }

Expand Down Expand Up @@ -107,12 +114,11 @@ class TorrentAddViewModel: ObservableObject {
func addTorrent(then dismiss: () -> Void) {
DispatchQueue.main.async {
let category = self.category == Self.defaultCategory ? "" : self.category.name
let tags = self.tags == Self.defaultTag ? "" : self.tags

if self.torrentType == .magnet {
qBittorrent.addMagnetTorrent(torrent: URLQueryItem(name: "urls", value: self.magnetURL), savePath: self.savePath, cookie: self.cookie, category: category, tags: tags, skipChecking: self.skipChecking, paused: self.paused, sequentialDownload: self.sequentialDownload, dlLimit: Int(self.downloadLimit) ?? -1, upLimit: Int(self.uploadLimit) ?? -1, ratioLimit: Float(self.ratioLimit) ?? -1.0, seedingTimeLimit: Int(self.seedingTimeLimit) ?? -1)
qBittorrent.addMagnetTorrent(torrent: URLQueryItem(name: "urls", value: self.magnetURL), savePath: self.savePath, cookie: self.cookie, category: category, tags: self.tagsString, skipChecking: self.skipChecking, paused: self.paused, sequentialDownload: self.sequentialDownload, dlLimit: Int(self.downloadLimit) ?? -1, upLimit: Int(self.uploadLimit) ?? -1, ratioLimit: Float(self.ratioLimit) ?? -1.0, seedingTimeLimit: Int(self.seedingTimeLimit) ?? -1)
} else {
qBittorrent.addFileTorrent(torrents: self.fileContent, savePath: self.savePath, cookie: self.cookie, category: self.category.name, tags: self.tags, skipChecking: self.skipChecking, paused: self.paused, sequentialDownload: self.sequentialDownload, dlLimit: Int(self.downloadLimit) ?? -1, upLimit: Int(self.uploadLimit) ?? -1, ratioLimit: Float(self.ratioLimit) ?? -1.0, seedingTimeLimit: Int(self.seedingTimeLimit) ?? -1)
qBittorrent.addFileTorrent(torrents: self.fileContent, savePath: self.savePath, cookie: self.cookie, category: category, tags: self.tagsString, skipChecking: self.skipChecking, paused: self.paused, sequentialDownload: self.sequentialDownload, dlLimit: Int(self.downloadLimit) ?? -1, upLimit: Int(self.uploadLimit) ?? -1, ratioLimit: Float(self.ratioLimit) ?? -1.0, seedingTimeLimit: Int(self.seedingTimeLimit) ?? -1)
}
}
dismiss()
Expand All @@ -135,15 +141,15 @@ class TorrentAddViewModel: ObservableObject {
qBittorrent.getCategories(completionHandler: { categories in
DispatchQueue.main.async {
// Append sorted list of Category objects to ensure "Uncategorized" always appears at the top
self.categories += categories.map { $1 }.sorted { $0.name < $1.name }
self.categories = [Self.defaultCategory] + categories.map { $1 }.sorted { $0.name < $1.name }
}
})
}

func getTags() {
qBittorrent.getTags(completionHandler: { tags in
DispatchQueue.main.async {
self.tagsArr.append(contentsOf: tags)
self.tagsArr = tags
}
})
}
Expand Down
49 changes: 44 additions & 5 deletions qBitControl/Views/TorrentViews/TorrentAddView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ struct TorrentAddView: View {
viewModel.getSavePath()
viewModel.getCategories()
viewModel.getTags()
viewModel.checkTorrentType()

if(!viewModel.isAppeared) {
viewModel.isAppeared.toggle()
viewModel.checkTorrentType()
}
}
.toolbar() {
ToolbarItem(placement: .navigationBarLeading) {
Expand Down Expand Up @@ -99,6 +103,35 @@ struct TorrentAddView: View {
}
.fileImporter(isPresented: $viewModel.isFileImporter, allowedContentTypes: [.data], allowsMultipleSelection: true, onCompletion: viewModel.handleTorrentFiles)
}

func changeTagsView() -> some View {
VStack {
Form {
if viewModel.tagsArr.count > 1 {
List(viewModel.tagsArr, id: \.self) { tag in
Button {
if !viewModel.selectedTags.contains(tag) {
viewModel.selectedTags.insert(tag)
} else {
viewModel.selectedTags.remove(tag)
}
} label: {
HStack {
Text(tag)
.foregroundStyle(.foreground)
Spacer()
if viewModel.selectedTags.contains(tag) {
Image(systemName: "checkmark")
.foregroundColor(.accentColor)
}
}
}
}
}
}
.navigationTitle("Tags")
}
}

func torrentOptionsView() -> some View {
Group {
Expand All @@ -116,11 +149,17 @@ struct TorrentAddView: View {
if !viewModel.autoTmmEnabled { viewModel.savePath = category.savePath }
}

Picker("Tags", selection: $viewModel.tags) {
if(!viewModel.tagsArr.isEmpty) {
ForEach(viewModel.tagsArr, id: \.self) { tag in Text(tag).tag(tag) }
}
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

0 comments on commit 377a36a

Please sign in to comment.