Skip to content

Commit c305ec2

Browse files
committed
Sonar: Reduce code duplication
1 parent 2aeb5b2 commit c305ec2

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

iTorrent/Screens/Rss/Details/RssDetailsViewModel.swift

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ class RssDetailsViewModel: BaseViewModelWith<RssItemModel> {
4545

4646
private extension RssDetailsViewModel {
4747
func prepareDownload() async {
48-
if let link = rssModel.link,
49-
let magnet = MagnetURI(with: link)
48+
// MARK: - Try download magnet
49+
if let magnet = MagnetURI(with: rssModel.enclosure?.url) ?? // Check enclosure
50+
MagnetURI(with: rssModel.link) // Otherwise check link
5051
{
5152
guard !TorrentService.shared.checkTorrentExists(with: magnet.infoHashes) else {
5253
downloadType = .added
@@ -61,31 +62,23 @@ private extension RssDetailsViewModel {
6162
return
6263
}
6364

64-
if let link = rssModel.link,
65-
let file = await TorrentFile(remote: link)
66-
{
67-
guard !TorrentService.shared.checkTorrentExists(with: file.infoHashes) else {
68-
downloadType = .added
69-
return
70-
}
7165

72-
downloadType = .torrent
73-
download = { [unowned self] in
74-
navigate(to: TorrentAddViewModel.self, with: .init(torrentFile: file, completion: { [weak self] added in
75-
guard added else { return }
76-
self?.downloadType = .added
77-
}), by: .present(wrapInNavigation: true))
78-
}
79-
return
80-
}
66+
// MARK: - Try download file
67+
let file: TorrentFile?
8168

82-
if let link = rssModel.enclosure?.url,
83-
let file = await TorrentFile(remote: link)
84-
{
69+
// Check enclosure
70+
if let temp = await TorrentFile(remote: rssModel.enclosure?.url) { file = temp }
71+
// Otherwise check link
72+
else if let temp = await TorrentFile(remote: rssModel.link) { file = temp }
73+
// Otherwise nothing to download
74+
else { file = nil }
75+
76+
if let file {
8577
guard !TorrentService.shared.checkTorrentExists(with: file.infoHashes) else {
8678
downloadType = .added
8779
return
8880
}
81+
8982
downloadType = .torrent
9083
download = { [unowned self] in
9184
navigate(to: TorrentAddViewModel.self, with: .init(torrentFile: file, completion: { [weak self] added in
@@ -97,3 +90,17 @@ private extension RssDetailsViewModel {
9790
}
9891
}
9992
}
93+
94+
private extension TorrentFile {
95+
convenience init?(remote url: URL?) async {
96+
guard let url else { return nil }
97+
await self.init(remote: url)
98+
}
99+
}
100+
101+
private extension MagnetURI {
102+
convenience init?(with url: URL?) {
103+
guard let url else { return nil }
104+
self.init(with: url)
105+
}
106+
}

0 commit comments

Comments
 (0)