-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from TheXtremeLabs/develop
Develop
- Loading branch information
Showing
12 changed files
with
362 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
app/src/main/java/com/riders/thelab/data/local/model/Download.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.riders.thelab.data.local.model | ||
|
||
import java.io.File | ||
|
||
sealed class Download { | ||
data class Started(val started: Boolean) : Download() | ||
data class Progress(val percent: Int) : Download() | ||
data class Done(val done: Boolean) : Download() | ||
data class Error(val error: Boolean) : Download() | ||
data class Finished(val file: File) : Download() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
app/src/main/java/com/riders/thelab/ui/download/DownloadActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package com.riders.thelab.ui.download | ||
|
||
import android.annotation.SuppressLint | ||
import android.os.Bundle | ||
import androidx.activity.viewModels | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.core.content.ContextCompat | ||
import com.riders.thelab.R | ||
import com.riders.thelab.data.local.model.Download | ||
import com.riders.thelab.databinding.ActivityDownloadBinding | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import kotlinx.coroutines.* | ||
import timber.log.Timber | ||
import java.net.UnknownHostException | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
@AndroidEntryPoint | ||
class DownloadActivity | ||
: AppCompatActivity(), CoroutineScope { | ||
|
||
override val coroutineContext: CoroutineContext | ||
get() = Dispatchers.Main + Job() | ||
|
||
|
||
private var _viewBinding: ActivityDownloadBinding? = null | ||
|
||
// This property is only valid between onCreateView and | ||
// onDestroyView. | ||
private val binding get() = _viewBinding!! | ||
|
||
private val mViewModel: DownloadViewModel by viewModels() | ||
|
||
|
||
@DelicateCoroutinesApi | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
_viewBinding = ActivityDownloadBinding.inflate(layoutInflater) | ||
setContentView(binding.root) | ||
|
||
initViewsModelsObservers() | ||
|
||
downloadFile() | ||
} | ||
|
||
override fun onDestroy() { | ||
super.onDestroy() | ||
Timber.e("onDestroy()") | ||
_viewBinding = null | ||
} | ||
|
||
@SuppressLint("SetTextI18n") | ||
private fun initViewsModelsObservers() { | ||
mViewModel.getPercentData().observe(this, { | ||
binding.progressBar.progress = it | ||
}) | ||
|
||
mViewModel.getDownloadStatus().observe(this, { | ||
when (it) { | ||
is Download.Started -> { | ||
Timber.d("${it.started}") | ||
binding.tvDownloadStatus.text = "Started" | ||
} | ||
|
||
is Download.Done -> { | ||
Timber.d("${it.done}") | ||
binding.progressBar.progress = 100 | ||
binding.progressBar.setIndicatorColor( | ||
ContextCompat.getColor(this@DownloadActivity, R.color.success) | ||
) | ||
|
||
binding.tvDownloadStatus.text = "Success ! Download Done" | ||
} | ||
|
||
is Download.Error -> { | ||
Timber.d("${it.error}") | ||
showDownloadError("Error ! Download Failed") | ||
} | ||
is Download.Finished -> { // Ignored | ||
} | ||
is Download.Progress -> { // Ignored | ||
} | ||
} | ||
}) | ||
} | ||
|
||
|
||
@SuppressLint("SetTextI18n") | ||
@DelicateCoroutinesApi | ||
private fun downloadFile() { | ||
|
||
GlobalScope.launch { | ||
try { | ||
supervisorScope { | ||
mViewModel.downloadFile() | ||
} | ||
} catch (exception: Exception) { | ||
exception.printStackTrace() | ||
|
||
if (exception is UnknownHostException) { | ||
Timber.e("Check your connection, cannot reach host ${exception.message}, cause : ${exception.cause}") | ||
showDownloadError("Check your connection, cannot reach host ${exception.message}") | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun showDownloadError(message: String) { | ||
CoroutineScope(coroutineContext).launch { | ||
binding.progressBar.progress = 100 | ||
binding.progressBar.setIndicatorColor( | ||
ContextCompat.getColor(this@DownloadActivity, R.color.error) | ||
) | ||
binding.tvDownloadStatus.text = message | ||
} | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
app/src/main/java/com/riders/thelab/ui/download/DownloadViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.riders.thelab.ui.download | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import com.riders.thelab.data.IRepository | ||
import com.riders.thelab.data.local.model.Download | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.collect | ||
import kotlinx.coroutines.withContext | ||
import timber.log.Timber | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class DownloadViewModel @Inject constructor( | ||
private val repository: IRepository | ||
) : ViewModel() { | ||
|
||
private val downloadStatus: MutableLiveData<Download> = MutableLiveData() | ||
private val percentData: MutableLiveData<Int> = MutableLiveData() | ||
|
||
fun getDownloadStatus(): LiveData<Download> { | ||
return downloadStatus | ||
} | ||
|
||
fun getPercentData(): LiveData<Int> { | ||
return percentData | ||
} | ||
|
||
|
||
suspend fun downloadFile() { | ||
Timber.d("downloadFile()") | ||
repository.getBulkDownload() | ||
.collect { download -> | ||
|
||
withContext(Dispatchers.Main) { | ||
when (download) { | ||
is Download.Started -> { | ||
downloadStatus.value = download | ||
} | ||
|
||
is Download.Progress -> { | ||
// update ui with progress | ||
Timber.d("percent : ${download.percent}%") | ||
|
||
percentData.value = download.percent | ||
} | ||
|
||
is Download.Done -> { | ||
downloadStatus.value = download | ||
} | ||
|
||
is Download.Error -> { | ||
downloadStatus.value = download | ||
} | ||
|
||
is Download.Finished -> { | ||
// update ui with file | ||
Timber.d("download status : ${download.file.toString()}") | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:tint="#FFFFFF" | ||
android:viewportWidth="24" | ||
android:viewportHeight="24"> | ||
<path | ||
android:fillColor="@android:color/white" | ||
android:pathData="M16,13h-3V3h-2v10H8l4,4 4,-4zM4,19v2h16v-2H4z" /> | ||
</vector> |
Oops, something went wrong.