diff --git a/qBitControl/Classes/qBittorrentClass.swift b/qBitControl/Classes/qBittorrentClass.swift index 3bb30ee..bc0e490 100644 --- a/qBitControl/Classes/qBittorrentClass.swift +++ b/qBitControl/Classes/qBittorrentClass.swift @@ -215,6 +215,10 @@ class qBittorrent { qBitRequest.requestTorrentManagement(request: request) } + static func pauseAllTorrents() { + pauseTorrent(hash: "all") + } + static func resumeTorrent(hash: String) { let path = "/api/v2/torrents/resume" @@ -223,6 +227,10 @@ class qBittorrent { qBitRequest.requestTorrentManagement(request: request) } + static func resumeAllTorrents() { + resumeTorrent(hash: "all") + } + static func recheckTorrent(hash: String) { let path = "/api/v2/torrents/recheck" diff --git a/qBitControl/DemoView/TorrentListViewDemo.swift b/qBitControl/DemoView/TorrentListViewDemo.swift index d62b306..56d2ed3 100644 --- a/qBitControl/DemoView/TorrentListViewDemo.swift +++ b/qBitControl/DemoView/TorrentListViewDemo.swift @@ -32,6 +32,7 @@ struct TorrentListViewDemo: View { let defaults = UserDefaults.standard + @State private var alertIdentifier: AlertIdentifier? func torrentListHeader() -> some View { HStack(spacing: 3) { @@ -147,13 +148,48 @@ struct TorrentListViewDemo: View { } .toolbar() { ToolbarItem(placement: .navigationBarLeading) { - Button { - //qBittorrent.setCookie(cookie: "") - isDemo = false + Menu { + Button { + alertIdentifier = AlertIdentifier(id: .resumeAll) + } label: { + Image(systemName: "play") + .rotationEffect(.degrees(180)) + Text("Resume All Tasks") + } + + Button { + alertIdentifier = AlertIdentifier(id: .pauseAll) + } label: { + Image(systemName: "pause") + .rotationEffect(.degrees(180)) + Text("Pause All Tasks") + } + + Button(role: .destructive) { + alertIdentifier = AlertIdentifier(id: .logOut) + } label: { + Image(systemName: "rectangle.portrait.and.arrow.forward") + .rotationEffect(.degrees(180)) + Text("Log out") + } } label: { - Image(systemName: "rectangle.portrait.and.arrow.forward") - .rotationEffect(.degrees(180)) - Text("Log out") + Image(systemName: "ellipsis.circle") + }.alert(item: $alertIdentifier) { alert in + switch(alert.id) { + case .resumeAll: + return Alert(title: Text("Confirm Resume All"), message: Text("Are you sure you want to resume all tasks?"), primaryButton: .default(Text("Resume")) { + //qBittorrent.resumeAllTorrents() + }, secondaryButton: .cancel()) + case .pauseAll: + return Alert(title: Text("Confirm Pause All"), message: Text("Are you sure you want to pause all tasks?"), primaryButton: .default(Text("Pause")) { + //qBittorrent.pauseAllTorrents() + }, secondaryButton: .cancel()) + case .logOut: + return Alert(title: Text("Confirm Logout"), message: Text("Are you sure you want to log out?"), primaryButton: .destructive(Text("Log Out")) { + qBittorrent.setCookie(cookie: "") + isDemo = false + }, secondaryButton: .cancel()) + } } } ToolbarItem(placement: .navigationBarTrailing) { diff --git a/qBitControl/Structures.swift b/qBitControl/Structures.swift index 143dad3..7160d78 100644 --- a/qBitControl/Structures.swift +++ b/qBitControl/Structures.swift @@ -351,3 +351,11 @@ struct RSS: Decodable { self.articles = try container.decode([Article].self, forKey: .articles).sorted(by: { $0.date > $1.date }) } } + +struct AlertIdentifier: Identifiable { + enum Choice { + case resumeAll, pauseAll, logOut + } + + var id: Choice +} diff --git a/qBitControl/TorrentView/TorrentListView.swift b/qBitControl/TorrentView/TorrentListView.swift index e94101b..db0a806 100644 --- a/qBitControl/TorrentView/TorrentListView.swift +++ b/qBitControl/TorrentView/TorrentListView.swift @@ -30,6 +30,8 @@ struct TorrentListView: View { @Binding var isLoggedIn: Bool + @State private var alertIdentifier: AlertIdentifier? + let defaults = UserDefaults.standard func getTorrents() { @@ -165,14 +167,59 @@ struct TorrentListView: View { } .toolbar() { ToolbarItem(placement: .navigationBarLeading) { - Button { + Menu { + Button { + alertIdentifier = AlertIdentifier(id: .resumeAll) + } label: { + Image(systemName: "play") + .rotationEffect(.degrees(180)) + Text("Resume All Tasks") + } + + Button { + alertIdentifier = AlertIdentifier(id: .pauseAll) + } label: { + Image(systemName: "pause") + .rotationEffect(.degrees(180)) + Text("Pause All Tasks") + } + + Button(role: .destructive) { + alertIdentifier = AlertIdentifier(id: .logOut) + } label: { + Image(systemName: "rectangle.portrait.and.arrow.forward") + .rotationEffect(.degrees(180)) + Text("Log out") + } + } label: { + Image(systemName: "ellipsis.circle") + }.alert(item: $alertIdentifier) { alert in + switch(alert.id) { + case .resumeAll: + return Alert(title: Text("Confirm Resume All"), message: Text("Are you sure you want to resume all tasks?"), primaryButton: .default(Text("Resume")) { + qBittorrent.resumeAllTorrents() + }, secondaryButton: .cancel()) + case .pauseAll: + return Alert(title: Text("Confirm Pause All"), message: Text("Are you sure you want to pause all tasks?"), primaryButton: .default(Text("Pause")) { + qBittorrent.pauseAllTorrents() + }, secondaryButton: .cancel()) + case .logOut: + return Alert(title: Text("Confirm Logout"), message: Text("Are you sure you want to log out?"), primaryButton: .destructive(Text("Log Out")) { + qBittorrent.setCookie(cookie: "") + isLoggedIn = false + }, secondaryButton: .cancel()) + } + } + + //Image(systemName: "ellipsis.circle") + /*Button { qBittorrent.setCookie(cookie: "") isLoggedIn = false } label: { Image(systemName: "rectangle.portrait.and.arrow.forward") .rotationEffect(.degrees(180)) Text("Log out") - } + }*/ } ToolbarItem(placement: .navigationBarTrailing) { Button {