Skip to content

Commit

Permalink
More fixes and improvements, specially to the torrent file / magnet l…
Browse files Browse the repository at this point in the history
…ink handlers.
  • Loading branch information
edualm committed Dec 12, 2020
1 parent 2abad0e commit 54043e2
Show file tree
Hide file tree
Showing 9 changed files with 446 additions and 279 deletions.
74 changes: 41 additions & 33 deletions SeedTruck.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions Shared/Presenters/TorrentDetailsPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class TorrentDetailsPresenter: ObservableObject {
if case let Result.success(success) = result, success {
self.currentAlert = nil

DispatchQueue.main.async {
NotificationCenter.default.post(name: .updateTorrentListView, object: nil)
}

onSuccess?()
} else {
self.currentAlert = .init(id: .error)
Expand All @@ -72,10 +76,6 @@ class TorrentDetailsPresenter: ObservableObject {
case .prepareForRemoval(let deletingFiles):
server.connection.perform(.remove(deletingData: deletingFiles), on: torrent, completionHandler: successCheck)

DispatchQueue.main.async {
NotificationCenter.default.post(name: .updateTorrentListView, object: nil)
}

actionToCommit = nil
isLoading = true
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// NewServerView+Extensions.swift
// NewServerView+Shared.swift
// SeedTruck
//
// Created by Eduardo Almeida on 12/12/2020.
Expand Down
139 changes: 139 additions & 0 deletions Shared/Views/Shared Extensions/TorrentHandlerView+Shared.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
//
// TorrentHandlerView+Shared.swift
// SeedTruck
//
// Created by Eduardo Almeida on 12/12/2020.
//

import SwiftUI

extension TorrentHandlerView {

struct NoServersWarningView: View {

var body: some View {
GroupBox(label: Label("Oops!", systemImage: "exclamationmark.triangle")) {
HStack {
Text("You must first configure at least one server in the app in order to be able to add a torrent!")
Spacer()
}.padding(.top)
}
}
}

struct InfoSectionView: View {

let torrent: LocalTorrent

var body: some View {
Group {
if let name = torrent.name {
HStack {
Text("Name")
Spacer()
Text(name)
.foregroundColor(.secondary)
}
}

if let size = torrent.size {
HStack {
Text("Size")
Spacer()
Text(ByteCountFormatter.humanReadableFileSize(bytes: Int64(size)))
.foregroundColor(.secondary)
}
}

if let files = torrent.files {
HStack {
Text("Files")
Spacer()
Text("\(files.count)")
.foregroundColor(.secondary)
}
}

if let isPrivate = torrent.isPrivate {
HStack {
Text("Private")
Spacer()
Text(isPrivate ? "Yes" : "No")
.foregroundColor(.secondary)
}
}
}
}
}

var showingError: Binding<Bool> {
Binding<Bool>(
get: { errorMessage != nil },
set: {
if $0 == false {
errorMessage = nil
}
}
)
}

func onAppear() {
if let s = server {
selectedServers = [s]
} else if serverConnections.count == 1 {
selectedServers = [serverConnections[0]]
}
}

func startDownload() {
processing = true

var remaining = selectedServers.count
var errors: [(Server, Error)] = []

selectedServers.forEach { server in
server.connection.addTorrent(torrent) {
switch $0 {
case .success:
()

case .failure(let error):
errors.append((server, error))
}

remaining = remaining - 1

if remaining == 0 {
processing = false

if errors.count == 0 {
closeHandler?()
} else {
errorMessage = "An error has occurred while adding the torrent to the following servers:\n\n" +
"\(errors.map { "\"\($0.0.name)\": \($0.1.localizedDescription)\n" })\n" +
"Please look at the inserted data and try again."
}
}
}
}
}

var processingBody: some View {
ProgressView()
.progressViewStyle(CircularProgressViewStyle())
.padding()
}

var sharedBody: some View {
Group {
if processing {
processingBody
} else {
normalBody
}
}
.navigationTitle("Add Torrent")
.padding()
.onAppear(perform: onAppear)
}
}
22 changes: 15 additions & 7 deletions Shared/Views/TorrentDetailsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ struct TorrentDetailsView: View {

let torrent: RemoteTorrent

@State var shouldShowEmptyView: Bool = false

@ObservedObject var presenter: TorrentDetailsPresenter

var innerBody: some View {
Expand Down Expand Up @@ -251,6 +253,8 @@ struct TorrentDetailsView: View {
primaryButton: .destructive(Text("Confirm")) {
self.presenter.perform(.commit) {
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) {
shouldShowEmptyView = true

self.presentation.wrappedValue.dismiss()
}
}
Expand All @@ -265,13 +269,17 @@ struct TorrentDetailsView: View {
}

var body: some View {
#if os(macOS)
innerBody.padding()
#elseif os(tvOS) || os(watchOS)
innerBody.navigationBarTitle("Torrent Detail")
#else
innerBody.navigationBarTitle("Torrent Detail").padding()
#endif
if shouldShowEmptyView {
EmptyView()
} else {
#if os(macOS)
innerBody.padding()
#elseif os(tvOS) || os(watchOS)
innerBody.navigationBarTitle("Torrent Detail")
#else
innerBody.navigationBarTitle("Torrent Detail").padding()
#endif
}
}
}

Expand Down
Loading

0 comments on commit 54043e2

Please sign in to comment.