Skip to content

Commit

Permalink
[General] Improvements & fixes #12, #13
Browse files Browse the repository at this point in the history
  • Loading branch information
KaustubhPatange committed Aug 16, 2020
1 parent 82fd6d5 commit 06f0b02
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
19 changes: 18 additions & 1 deletion app/src/main/java/com/kpstv/yts/data/models/TorrentJob.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,21 @@ data class TorrentJob (
var status: String,
val peers: Int,
val magnetHash: String
) : Serializable
) : Serializable {
companion object {
fun from(model: Torrent, status: String = "Paused") =
TorrentJob(
title = model.title,
bannerUrl = model.banner_url,
progress = 0,
seeds = model.seeds,
downloadSpeed = 0f,
currentSize = 0,
totalSize = model.size,
isPlay = false,
status = status,
peers = model.peers,
magnetHash = model.hash
)
}
}
31 changes: 31 additions & 0 deletions app/src/main/java/com/kpstv/yts/services/DownloadService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import android.os.PowerManager.WakeLock
import android.text.Html
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.lifecycle.ViewModelProvider
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.github.se_bastiaan.torrentstream.StreamStatus
import com.github.se_bastiaan.torrentstream.TorrentOptions
Expand All @@ -32,6 +33,7 @@ import com.kpstv.yts.AppInterface.Companion.STORAGE_LOCATION
import com.kpstv.yts.AppInterface.Companion.TORRENT_NOT_SUPPORTED
import com.kpstv.yts.AppInterface.Companion.formatDownloadSpeed
import com.kpstv.yts.R
import com.kpstv.yts.adapters.HistoryModel
import com.kpstv.yts.data.db.repository.DownloadRepository
import com.kpstv.yts.data.db.repository.PauseRepository
import com.kpstv.yts.data.models.Torrent
Expand All @@ -42,6 +44,7 @@ import com.kpstv.yts.extensions.utils.AppUtils.Companion.getVideoDuration
import com.kpstv.yts.extensions.utils.AppUtils.Companion.saveImageFromUrl
import com.kpstv.yts.receivers.CommonBroadCast
import com.kpstv.yts.ui.activities.DownloadActivity
import com.kpstv.yts.ui.viewmodels.MainViewModel
import dagger.hilt.android.AndroidEntryPoint
import java.io.File
import java.text.SimpleDateFormat
Expand Down Expand Up @@ -490,10 +493,38 @@ class DownloadService : IntentService("blank") {
return System.currentTimeMillis() / 1000
}

private fun saveInterruptDownloads() {
val model = currentTorrentModel
val currentTorrent = currentModel
if (model != null && currentTorrent != null) {
pauseRepository.savePauseModel(
Model.response_pause(
job = TorrentJob.from(model),
hash = model.hash,
saveLocation = currentTorrent.saveLocation.path,
torrent = model
)
)
}
for (job in pendingJobs) {
pauseRepository.savePauseModel(
Model.response_pause(
job = TorrentJob.from(job),
hash = job.hash,
saveLocation = null,
torrent = job
)
)
}
}

override fun onDestroy() {

DS_LOG("=> onDestroy() called")

/** Save all interrupted downloads */
saveInterruptDownloads()

/** Update receiver to know that all jobs completed */
updateEmptyQueue()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class DownloadActivity : AppCompatActivity() {
pauseAdapter.updateModels(it)
if (pauseAdapter.itemCount > 0) {
binding.layoutPauseQueue.show()
binding.layoutJobEmptyQueue.hide()
} else
binding.layoutPauseQueue.hide()
DA_LOG("Updating models")
Expand Down Expand Up @@ -384,7 +385,6 @@ class DownloadActivity : AppCompatActivity() {
}

private fun showJobEmptyQueue() {

if (PAUSE_BUTTON_CLICKED) {
PAUSE_BUTTON_CLICKED = false
return
Expand Down

0 comments on commit 06f0b02

Please sign in to comment.