Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import com.github.livingwithhippos.unchained.utilities.extension.cancelIfActive
import com.github.livingwithhippos.unchained.utilities.postEvent
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
Expand All @@ -37,7 +36,7 @@ constructor(
val downloadLiveData = MutableLiveData<Event<DownloadItem?>>()
val errorsLiveData = MutableLiveData<Event<List<UnchainedNetworkException>>>()

private var job = Job()
private var job: Job? = null

fun getFullTorrentInfo(id: String) {
viewModelScope.launch {
Expand All @@ -48,21 +47,19 @@ constructor(

fun pollTorrentStatus(id: String) {
// todo: test if I need to recreate a job when it is cancelled
job.cancelIfActive()
job = Job()
job?.cancelIfActive()

val scope = CoroutineScope(job + Dispatchers.IO)
job =
viewModelScope.launch(Dispatchers.IO) {
// / maybe job.isActive?
while (isActive) {
val torrentData = torrentsRepository.getTorrentInfo(id)
if (torrentData != null) torrentLiveData.postEvent(torrentData)
if (endedStatusList.contains(torrentData?.status)) job?.cancelIfActive()

scope.launch {
// / maybe job.isActive?
while (isActive) {
val torrentData = torrentsRepository.getTorrentInfo(id)
if (torrentData != null) torrentLiveData.postEvent(torrentData)
if (endedStatusList.contains(torrentData?.status)) job.cancelIfActive()

delay(2000.milliseconds)
delay(2000.milliseconds)
}
}
}
}

fun deleteTorrent(id: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import com.github.livingwithhippos.unchained.utilities.extension.cancelIfActive
import com.github.livingwithhippos.unchained.utilities.postEvent
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
Expand All @@ -39,7 +38,7 @@ constructor(
val torrentLiveData = MutableLiveData<Event<TorrentEvent>>()
val structureLiveData = MutableLiveData<Event<Node<TorrentFileItem>>>()

private var job = Job()
private var job: Job? = null

fun fetchAddedMagnet(magnet: String) {
viewModelScope.launch {
Expand Down Expand Up @@ -103,48 +102,46 @@ constructor(
return
}

job.cancelIfActive()
job = Job()

val scope = CoroutineScope(job + Dispatchers.IO)

scope.launch {
var selected = false
// / maybe job.isActive?
while (isActive) {
if (!selected) {
when (val selectResponse = torrentsRepository.selectFiles(id, files)) {
is EitherResult.Failure -> {
if (selectResponse.failure is EmptyBodyError) {
Timber.d(
"Select torrent files success returned ${selectResponse.failure.returnCode}"
)
job?.cancelIfActive()

job =
viewModelScope.launch(Dispatchers.IO) {
var selected = false
// / maybe job.isActive?
while (isActive) {
if (!selected) {
when (val selectResponse = torrentsRepository.selectFiles(id, files)) {
is EitherResult.Failure -> {
if (selectResponse.failure is EmptyBodyError) {
Timber.d(
"Select torrent files success returned ${selectResponse.failure.returnCode}"
)
selected = true
} else {
Timber.e(
"Exception during torrent files selection call: ${selectResponse.failure}"
)
}
}
is EitherResult.Success -> {
Timber.d("Select torrent files success")
selected = true
} else {
Timber.e(
"Exception during torrent files selection call: ${selectResponse.failure}"
)
}
}
is EitherResult.Success -> {
Timber.d("Select torrent files success")
selected = true
}
}
}

if (selected) {
val torrentItem: TorrentItem? = torrentsRepository.getTorrentInfo(id)
if (torrentItem != null) {
if (!beforeSelectionStatusList.contains(torrentItem.status)) {
job.cancelIfActive()
torrentLiveData.postEvent(TorrentEvent.FilesSelected(torrentItem))
if (selected) {
val torrentItem: TorrentItem? = torrentsRepository.getTorrentInfo(id)
if (torrentItem != null) {
if (!beforeSelectionStatusList.contains(torrentItem.status)) {
job?.cancelIfActive()
torrentLiveData.postEvent(TorrentEvent.FilesSelected(torrentItem))
}
}
}
delay(1500.milliseconds)
}
delay(1500.milliseconds)
}
}
}

fun triggerTorrentEvent(event: TorrentEvent) {
Expand Down
Loading