Skip to content

Commit

Permalink
convert to using parse torrent result
Browse files Browse the repository at this point in the history
  • Loading branch information
arranlomas committed Nov 5, 2017
1 parent a776f28 commit 741a3d5
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 50 deletions.
19 changes: 12 additions & 7 deletions app/src/main/java/com/shwifty/tex/dialogs/DialogManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import android.widget.ArrayAdapter
import android.widget.EditText
import android.widget.Spinner
import com.afollestad.materialdialogs.MaterialDialog
import com.crashlytics.android.Crashlytics
import com.schiwfty.torrentwrapper.models.TorrentFile
import com.schiwfty.torrentwrapper.models.TorrentInfo
import com.schiwfty.torrentwrapper.repositories.ITorrentRepository
import com.schiwfty.torrentwrapper.utils.ParseTorrentResult
import com.schiwfty.torrentwrapper.utils.getFullPath
import com.shwifty.tex.R
import com.shwifty.tex.models.TorrentSearchCategory
import com.shwifty.tex.models.TorrentSearchSortType
import com.shwifty.tex.utils.logTorrentParseError
import com.shwifty.tex.views.main.MainEventHandler

/**
Expand Down Expand Up @@ -93,9 +96,9 @@ class DialogManager : IDialogManager {
.content(R.string.delete_torrent_dialog_text)
.positiveText(R.string.delete)
.onPositive { dialog, _ ->
torrentRepository.getTorrentInfo(torrentInfo.info_hash)
.subscribe({
it?.let {
torrentRepository.downloadTorrentInfo(torrentInfo.info_hash)
.subscribe({ result ->
result.unwrapIfSuccess {
val deleted = torrentRepository.deleteTorrentInfoFromStorage(it)
if (deleted) {
it.fileList.forEach {
Expand All @@ -105,17 +108,19 @@ class DialogManager : IDialogManager {
} else {
onError.invoke()
}
}
}?.let { result.logTorrentParseError() }
}, {
it.printStackTrace()
})
dialog.dismiss()
}
.negativeText(R.string.keep)
.onNegative { dialog, _ ->
torrentRepository.getTorrentInfo(torrentInfo.info_hash)
.subscribe({
it?.let { torrentRepository.deleteTorrentInfoFromStorage(it) }
torrentRepository.downloadTorrentInfo(torrentInfo.info_hash)
.subscribe({ result ->
result.unwrapIfSuccess {
torrentRepository.deleteTorrentInfoFromStorage(it)
} ?: let { result.logTorrentParseError() }
}, {
it.printStackTrace()
})
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/com/shwifty/tex/utils/TorrentExtensions.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.shwifty.tex.utils

import com.crashlytics.android.Crashlytics
import com.google.android.gms.cast.MediaInfo
import com.google.android.gms.cast.MediaMetadata
import com.schiwfty.torrentwrapper.models.TorrentFile
import com.schiwfty.torrentwrapper.utils.ParseTorrentResult
import com.schiwfty.torrentwrapper.utils.getFullPath
import com.schiwfty.torrentwrapper.utils.getShareableDataUrl

Expand All @@ -18,4 +20,12 @@ fun TorrentFile.buildMediaInfo(mime: String): MediaInfo {
.setContentType(mime)
.setMetadata(movieMetadata)
.build()
}

fun ParseTorrentResult.logTorrentParseError() {
if (this is ParseTorrentResult.Error) {
val error = this.exception
error.printStackTrace()
Crashlytics.logException(error)
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.shwifty.tex.views.addtorrent.mvp

import android.os.Bundle
import com.schiwfty.torrentwrapper.models.TorrentInfo
import com.schiwfty.torrentwrapper.repositories.ITorrentRepository
import com.schiwfty.torrentwrapper.utils.ParseTorrentResult
import com.schiwfty.torrentwrapper.utils.findHashFromMagnet
import com.schiwfty.torrentwrapper.utils.findNameFromMagnet
import com.schiwfty.torrentwrapper.utils.findTrackersFromMagnet
import com.shwifty.tex.utils.logTorrentParseError
import com.shwifty.tex.views.base.BasePresenter
import java.net.URLDecoder

Expand Down Expand Up @@ -36,16 +37,20 @@ class AddTorrentPresenter(val torrentRepository: ITorrentRepository) : BasePrese
}

torrentRepository.getAllTorrentsFromStorage()
.subscribe(object : BaseSubscriber<List<TorrentInfo>>() {
.subscribe(object : BaseSubscriber<List<ParseTorrentResult>>() {
override fun onCompleted() {
}

override fun onStart() {
}

override fun onNext(torrents: List<TorrentInfo>) {
override fun onNext(results: List<ParseTorrentResult>) {
var alreadyExists = false
torrents.forEach { if (it.info_hash == torrentHash) alreadyExists = true }
results.forEach { result ->
result.unwrapIfSuccess {
if (it.info_hash == torrentHash) alreadyExists = true
} ?: let { result.logTorrentParseError() }
}
this@AddTorrentPresenter.alreadyExisted = alreadyExists
fetchTorrent()
}
Expand All @@ -55,10 +60,12 @@ class AddTorrentPresenter(val torrentRepository: ITorrentRepository) : BasePrese
override fun notifyBackPressed() {
if (!alreadyExisted) {
torrentRepository.getAllTorrentsFromStorage()
.subscribe(object : BaseSubscriber<List<TorrentInfo>>() {
override fun onNext(torrents: List<TorrentInfo>) {
torrents.forEach {
if (it.info_hash == torrentHash) torrentRepository.deleteTorrentInfoFromStorage(it)
.subscribe(object : BaseSubscriber<List<ParseTorrentResult>>() {
override fun onNext(torrents: List<ParseTorrentResult>) {
torrents.forEach { result ->
result.unwrapIfSuccess {
if (it.info_hash == torrentHash) torrentRepository.deleteTorrentInfoFromStorage(it)
} ?: let { result.logTorrentParseError() }
}
}
})
Expand All @@ -67,15 +74,12 @@ class AddTorrentPresenter(val torrentRepository: ITorrentRepository) : BasePrese

private fun fetchTorrent() {
val hash = torrentHash ?: return
torrentRepository.getTorrentInfo(hash)
.subscribe(object : BaseSubscriber<TorrentInfo?>() {
override fun onNext(pair: TorrentInfo?) {
torrentRepository.downloadTorrentInfo(hash)
.subscribe(object : BaseSubscriber<ParseTorrentResult>() {
override fun onNext(result: ParseTorrentResult) {
mvpView.setLoading(false)
mvpView.notifyTorrentAdded()
}

override fun onError(e: Throwable?) {
super.onError(e)
if (result is ParseTorrentResult.Error) super.onError(result.exception)
}
})
.addSubscription()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.shwifty.tex.views.base.BaseContract
interface AllContract {
interface View: BaseContract.MvpView {
fun showAllTorrents(torrentInfoList: List<TorrentInfo>)
fun showSomeTorrentsCouldNotBeLoaded(torrentCount: Int)
}

interface Presenter: BaseContract.Presenter<View> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,9 @@ class AllFragment : BaseFragment(), AllContract.View {
override fun setLoading(loading: Boolean) {
allTorrentsSwipeRefresh.isRefreshing = loading
}

override fun showSomeTorrentsCouldNotBeLoaded(torrentCount: Int) {
showError(getString(R.string.error_all_torrents_some_could_not_be_parsed, torrentCount))
}

}
20 changes: 16 additions & 4 deletions app/src/main/java/com/shwifty/tex/views/all/mvp/AllPresenter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.shwifty.tex.views.all.mvp
import android.content.Context
import com.schiwfty.torrentwrapper.models.TorrentInfo
import com.schiwfty.torrentwrapper.repositories.ITorrentRepository
import com.schiwfty.torrentwrapper.utils.ParseTorrentResult
import com.shwifty.tex.utils.logTorrentParseError
import com.shwifty.tex.views.base.BasePresenter

/**
Expand All @@ -16,7 +18,7 @@ class AllPresenter(val torrentRepository: ITorrentRepository) : BasePresenter<Al
super.attachView(mvpView)
torrentRepository.torrentInfoDeleteListener
.subscribe(object : BaseSubscriber<TorrentInfo>() {
override fun onNext(pair: TorrentInfo?) {
override fun onNext(result: TorrentInfo?) {
mvpView.setLoading(false)
refresh()
}
Expand All @@ -26,10 +28,20 @@ class AllPresenter(val torrentRepository: ITorrentRepository) : BasePresenter<Al

override fun refresh() {
torrentRepository.getAllTorrentsFromStorage()
.subscribe(object : BaseSubscriber<List<TorrentInfo>>() {
override fun onNext(torrentInfos: List<TorrentInfo>) {
.subscribe(object : BaseSubscriber<List<ParseTorrentResult>>() {
override fun onNext(results: List<ParseTorrentResult>) {
val success = results.filter { it is ParseTorrentResult.Success }
val error = results.filter { it is ParseTorrentResult.Error }
if (error.isNotEmpty()) {
mvpView.showSomeTorrentsCouldNotBeLoaded(error.size)
error.forEach { it.logTorrentParseError() }
}
mvpView.setLoading(false)
mvpView.showAllTorrents(torrentInfos)
val torrentInfos = mutableListOf<TorrentInfo>()
success.forEach { result ->
result.unwrapIfSuccess { torrentInfos.add(it) } ?: let { result.logTorrentParseError() }
}
mvpView.showAllTorrents(torrentInfos.toList())
}
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class FileDownloadPresenter(val torrentRepository: ITorrentRepository) : BasePre

torrentRepository.torrentFileDeleteListener
.subscribe(object : BaseSubscriber<TorrentFile>() {
override fun onNext(pair: TorrentFile?) {
override fun onNext(result: TorrentFile?) {
mvpView.setLoading(false)
refresh()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package com.shwifty.tex.views.showtorrent.mvp

import android.os.Bundle
import android.view.MenuItem
import com.schiwfty.torrentwrapper.confluence.Confluence
import com.schiwfty.torrentwrapper.models.TorrentInfo
import com.schiwfty.torrentwrapper.repositories.ITorrentRepository
import com.schiwfty.torrentwrapper.utils.ParseTorrentResult
import com.schiwfty.torrentwrapper.utils.findHashFromMagnet
import com.schiwfty.torrentwrapper.utils.findNameFromMagnet
import com.schiwfty.torrentwrapper.utils.findTrackersFromMagnet
import com.shwifty.tex.R
import com.shwifty.tex.utils.logTorrentParseError
import com.shwifty.tex.views.addtorrent.mvp.AddTorrentActivity
import com.shwifty.tex.views.base.BasePresenter
import java.net.URLDecoder
Expand Down Expand Up @@ -38,7 +39,7 @@ class TorrentInfoPresenter(val torrentRepository: ITorrentRepository) : BasePres

torrentRepository.torrentInfoDeleteListener
.subscribe(object : BaseSubscriber<TorrentInfo>() {
override fun onNext(pair: TorrentInfo?) {
override fun onNext(result: TorrentInfo?) {
mvpView.setLoading(false)
mvpView.dismiss()
}
Expand All @@ -48,15 +49,17 @@ class TorrentInfoPresenter(val torrentRepository: ITorrentRepository) : BasePres

override fun fetchTorrent() {
val hash = torrentHash
torrentRepository.getTorrentInfo(hash)
.subscribe(object : BaseSubscriber<TorrentInfo>(){
override fun onNext(pair: TorrentInfo?) {
torrentRepository.downloadTorrentInfo(hash)
.subscribe(object : BaseSubscriber<ParseTorrentResult>() {
override fun onNext(result: ParseTorrentResult) {
mvpView.setLoading(false)
torrentInfo = pair
torrentName = pair?.name
pair?.info_hash?.let { torrentHash = it }
torrentTrackers = pair?.announceList
mvpView.notifyTorrentAdded()
result.unwrapIfSuccess {
torrentInfo = it
torrentName = it.name
torrentHash = it.info_hash
torrentTrackers = it.announceList
} ?: let { result.logTorrentParseError() }
}
})
.addSubscription()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ class SplashPresenter : BasePresenter<SplashContract.View>(), SplashContract.Pre
torrentRepository.getStatus()
.retry()
.subscribe(object : BaseSubscriber<String>() {
override fun onNext(pair: String?) {
override fun onNext(result: String?) {
mvpView.setLoading(false)
pair?.let {
result?.let {
subscriptions.unsubscribe()
mvpView.showSuccess(R.string.splash_start_confluence_success)
mvpView.progressToMain()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.shwifty.tex.views.torrentdetails.mvp

import android.os.Bundle
import com.schiwfty.torrentwrapper.models.TorrentInfo
import com.schiwfty.torrentwrapper.repositories.ITorrentRepository
import com.schiwfty.torrentwrapper.utils.ParseTorrentResult
import com.shwifty.tex.utils.logTorrentParseError
import com.shwifty.tex.views.base.BasePresenter

/**
Expand All @@ -20,11 +21,13 @@ class TorrentDetailsPresenter(val torrentRepository: ITorrentRepository) : BaseP
}

override fun loadTorrent(torrentHash: String) {
torrentRepository.getTorrentInfo(torrentHash)
.subscribe(object : BaseSubscriber<TorrentInfo>() {
override fun onNext(pair: TorrentInfo?) {
torrentRepository.downloadTorrentInfo(torrentHash)
.subscribe(object : BaseSubscriber<ParseTorrentResult>() {
override fun onNext(result: ParseTorrentResult) {
mvpView.setLoading(false)
pair?.let { mvpView.setupViewFromTorrentInfo(it) }
result.unwrapIfSuccess {
mvpView.setupViewFromTorrentInfo(it)
} ?: let { result.logTorrentParseError() }
}
})
.addSubscription()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.shwifty.tex.views.torrentfiles.mvp

import android.os.Bundle
import com.schiwfty.torrentwrapper.confluence.Confluence
import com.schiwfty.torrentwrapper.models.TorrentFile
import com.schiwfty.torrentwrapper.models.TorrentInfo
import com.schiwfty.torrentwrapper.repositories.ITorrentRepository
import com.schiwfty.torrentwrapper.utils.ParseTorrentResult
import com.shwifty.tex.utils.logTorrentParseError
import com.shwifty.tex.views.base.BasePresenter
import com.shwifty.tex.views.main.MainEventHandler
import com.shwifty.tex.views.torrentfiles.list.TorrentFilesAdapter
Expand All @@ -23,11 +23,11 @@ class TorrentFilesPresenter(val torrentRepository: ITorrentRepository) : BasePre
}

override fun loadTorrent(torrentHash: String) {
torrentRepository.getTorrentInfo(torrentHash)
.subscribe(object : BaseSubscriber<TorrentInfo>() {
override fun onNext(pair: TorrentInfo?) {
torrentRepository.downloadTorrentInfo(torrentHash)
.subscribe(object : BaseSubscriber<ParseTorrentResult>() {
override fun onNext(result: ParseTorrentResult) {
mvpView.setLoading(false)
pair?.let { mvpView.setupViewFromTorrentInfo(it) }
result.unwrapIfSuccess { mvpView.setupViewFromTorrentInfo(it) } ?: let { result.logTorrentParseError() }
}
})
.addSubscription()
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<string name="error_activity_not_found">Error, No activity found to open this file</string>
<string name="error_cannot_open_torrent">Error, A problem occurred trying to open the torrent</string>
<string name="error_search_server_unreachable">Oops, Looks like our search server is down</string>

<string name="error_all_torrents_some_could_not_be_parsed">%1$d torrents could not be loaded, those torrent files have been deleted. Please manually redownload them</string>

<!--list item download-->
<string name="file_name">File Name</string>
Expand Down
Binary file added release-apks/2.0.9.apk
Binary file not shown.
Binary file modified torrentwrapper/torrentwrapper.aar
Binary file not shown.

0 comments on commit 741a3d5

Please sign in to comment.