Skip to content

Commit

Permalink
Sonar: Reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
XITRIX committed Jul 29, 2024
1 parent 2aeb5b2 commit c305ec2
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions iTorrent/Screens/Rss/Details/RssDetailsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ class RssDetailsViewModel: BaseViewModelWith<RssItemModel> {

private extension RssDetailsViewModel {
func prepareDownload() async {
if let link = rssModel.link,
let magnet = MagnetURI(with: link)
// MARK: - Try download magnet
if let magnet = MagnetURI(with: rssModel.enclosure?.url) ?? // Check enclosure
MagnetURI(with: rssModel.link) // Otherwise check link
{
guard !TorrentService.shared.checkTorrentExists(with: magnet.infoHashes) else {
downloadType = .added
Expand All @@ -61,31 +62,23 @@ private extension RssDetailsViewModel {
return
}

if let link = rssModel.link,
let file = await TorrentFile(remote: link)
{
guard !TorrentService.shared.checkTorrentExists(with: file.infoHashes) else {
downloadType = .added
return
}

downloadType = .torrent
download = { [unowned self] in
navigate(to: TorrentAddViewModel.self, with: .init(torrentFile: file, completion: { [weak self] added in
guard added else { return }
self?.downloadType = .added
}), by: .present(wrapInNavigation: true))
}
return
}
// MARK: - Try download file
let file: TorrentFile?

if let link = rssModel.enclosure?.url,
let file = await TorrentFile(remote: link)
{
// Check enclosure
if let temp = await TorrentFile(remote: rssModel.enclosure?.url) { file = temp }
// Otherwise check link
else if let temp = await TorrentFile(remote: rssModel.link) { file = temp }
// Otherwise nothing to download
else { file = nil }

if let file {
guard !TorrentService.shared.checkTorrentExists(with: file.infoHashes) else {
downloadType = .added
return
}

downloadType = .torrent
download = { [unowned self] in
navigate(to: TorrentAddViewModel.self, with: .init(torrentFile: file, completion: { [weak self] added in
Expand All @@ -97,3 +90,17 @@ private extension RssDetailsViewModel {
}
}
}

private extension TorrentFile {
convenience init?(remote url: URL?) async {
guard let url else { return nil }
await self.init(remote: url)
}
}

private extension MagnetURI {
convenience init?(with url: URL?) {
guard let url else { return nil }
self.init(with: url)
}
}

0 comments on commit c305ec2

Please sign in to comment.