Touch: fix Downloading

This commit is contained in:
Felix 2019-04-29 08:31:17 +02:00
parent e9ab5526e9
commit e14811ccbe
2 changed files with 46 additions and 42 deletions

View file

@ -1,17 +1,21 @@
package de.nicidienase.chaosflix.touch.eventdetails package de.nicidienase.chaosflix.touch.eventdetails
import android.Manifest
import android.arch.lifecycle.Observer import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders import android.arch.lifecycle.ViewModelProviders
import android.content.Context import android.content.Context
import android.content.DialogInterface import android.content.DialogInterface
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri import android.net.Uri
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.support.design.widget.Snackbar import android.support.design.widget.Snackbar
import android.support.v7.app.AlertDialog import android.support.v7.app.AlertDialog
import android.support.v7.app.AppCompatActivity import android.support.v7.app.AppCompatActivity
import android.view.Menu import android.view.Menu
import de.nicidienase.chaosflix.common.ChaosflixUtil import de.nicidienase.chaosflix.common.ChaosflixUtil
import de.nicidienase.chaosflix.common.OfflineItemManager
import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.Event import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.Event
import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.Recording import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.Recording
import de.nicidienase.chaosflix.common.viewmodel.DetailsViewModel import de.nicidienase.chaosflix.common.viewmodel.DetailsViewModel
@ -30,6 +34,7 @@ class EventDetailsActivity : AppCompatActivity(),
private lateinit var castService: CastService private lateinit var castService: CastService
private var selectDialog: AlertDialog? = null private var selectDialog: AlertDialog? = null
private var pendingDownload: Bundle? = null
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -43,9 +48,9 @@ class EventDetailsActivity : AppCompatActivity(),
if(liveEvent == null){ if(liveEvent == null){
return@Observer return@Observer
} }
val localFile = liveEvent.data?.getString(DetailsViewModel.KEY_LOCAL_PATH)
val recording = liveEvent.data?.getParcelable<Recording>(DetailsViewModel.RECORDING)
val event = liveEvent.data?.getParcelable<Event>(DetailsViewModel.EVENT) val event = liveEvent.data?.getParcelable<Event>(DetailsViewModel.EVENT)
val recording = liveEvent.data?.getParcelable<Recording>(DetailsViewModel.RECORDING)
val localFile = liveEvent.data?.getString(DetailsViewModel.KEY_LOCAL_PATH)
val selectItems: Array<Recording>? = liveEvent.data?.getParcelableArray(DetailsViewModel.KEY_SELECT_RECORDINGS) as Array<Recording>? val selectItems: Array<Recording>? = liveEvent.data?.getParcelableArray(DetailsViewModel.KEY_SELECT_RECORDINGS) as Array<Recording>?
when(liveEvent.state){ when(liveEvent.state){
DetailsViewModel.State.DisplayEvent -> { DetailsViewModel.State.DisplayEvent -> {
@ -73,7 +78,20 @@ class EventDetailsActivity : AppCompatActivity(),
DetailsViewModel.State.DownloadRecording -> { DetailsViewModel.State.DownloadRecording -> {
if (event != null && selectItems != null) { if (event != null && selectItems != null) {
selectRecording(event,selectItems.asList()) { e,r -> selectRecording(event,selectItems.asList()) { e,r ->
viewModel.download(e,r) viewModel.download(e,r).observe(this, Observer {
when(it?.state){
OfflineItemManager.State.Downloading -> {
Snackbar.make(fragment_container, "Download started", Snackbar.LENGTH_LONG).show()
}
OfflineItemManager.State.PermissionRequired -> {
pendingDownload = liveEvent.data
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
this.requestPermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
WRITE_PERMISSION_REQUEST)
}
}
}
})
} }
} }
} }
@ -99,6 +117,23 @@ class EventDetailsActivity : AppCompatActivity(),
showFragmentForEvent(event) showFragmentForEvent(event)
} }
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
if(requestCode == WRITE_PERMISSION_REQUEST){
if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
pendingDownload?.let {
val recording = it.getParcelable<Recording>(DetailsViewModel.RECORDING)
val event = it.getParcelable<Event>(DetailsViewModel.EVENT)
if(event != null && recording != null){
viewModel.download(event, recording)
}
}
pendingDownload = null
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
}
private fun showSnackbar(message: String, duration: Int = Snackbar.LENGTH_LONG ){ private fun showSnackbar(message: String, duration: Int = Snackbar.LENGTH_LONG ){
Snackbar.make(fragment_container, message, duration) Snackbar.make(fragment_container, message, duration)
@ -154,7 +189,7 @@ class EventDetailsActivity : AppCompatActivity(),
invalidateOptionsMenu() invalidateOptionsMenu()
} }
override fun playItem(event: Event, recording: Recording, localFile: String?) { private fun playItem(event: Event, recording: Recording, localFile: String? = null) {
if (castService.connected) { if (castService.connected) {
castService.loadMediaAndPlay(recording, event) castService.loadMediaAndPlay(recording, event)
} else { } else {
@ -176,10 +211,12 @@ class EventDetailsActivity : AppCompatActivity(),
companion object { companion object {
private val EXTRA_EVENT = "extra_event" private const val EXTRA_EVENT = "extra_event"
private val EXTRA_URI = "extra_uri" private const val EXTRA_URI = "extra_uri"
private const val WRITE_PERMISSION_REQUEST = 23
private val TAG = EventDetailsActivity::class.java.simpleName private val TAG = EventDetailsActivity::class.java.simpleName
fun launch(context: Context, event: Event) { fun launch(context: Context, event: Event) {
val intent = Intent(context, EventDetailsActivity::class.java) val intent = Intent(context, EventDetailsActivity::class.java)
intent.putExtra(EXTRA_EVENT, event) intent.putExtra(EXTRA_EVENT, event)

View file

@ -143,9 +143,6 @@ class EventDetailsFragment : Fragment() {
} }
private fun play() { private fun play() {
if(listener == null){
return
}
viewModel.playEvent(event) viewModel.playEvent(event)
} }
@ -191,7 +188,7 @@ class EventDetailsFragment : Fragment() {
} }
override fun onOptionsItemSelected(item: MenuItem?): Boolean { override fun onOptionsItemSelected(item: MenuItem?): Boolean {
when (item!!.itemId) { when (item?.itemId) {
android.R.id.home -> { android.R.id.home -> {
activity?.finish() activity?.finish()
return true return true
@ -208,7 +205,7 @@ class EventDetailsFragment : Fragment() {
R.id.action_unbookmark -> { R.id.action_unbookmark -> {
viewModel.removeBookmark(event.guid) viewModel.removeBookmark(event.guid)
watchlistItem = null watchlistItem = null
listener!!.invalidateOptionsMenu() listener?.invalidateOptionsMenu()
return true return true
} }
R.id.action_download -> { R.id.action_download -> {
@ -227,7 +224,7 @@ class EventDetailsFragment : Fragment() {
val shareIntent = Intent(Intent.ACTION_SEND, Uri.parse(event.frontendLink)) val shareIntent = Intent(Intent.ACTION_SEND, Uri.parse(event.frontendLink))
shareIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.watch_this)) shareIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.watch_this))
shareIntent.putExtra(Intent.EXTRA_TEXT, event.frontendLink) shareIntent.putExtra(Intent.EXTRA_TEXT, event.frontendLink)
shareIntent.setType("text/plain") shareIntent.type = "text/plain"
startActivity(shareIntent) startActivity(shareIntent)
return true return true
} }
@ -239,39 +236,9 @@ class EventDetailsFragment : Fragment() {
} }
} }
private var recordingToDownload: Recording? = null
private fun downloadRecording(recording: Recording) {
recordingToDownload = recording
viewModel.download(event, recording).observe(this, Observer {
when(it?.state){
OfflineItemManager.State.Downloading -> {
layout?.let {
Snackbar.make(it, "Download started", Snackbar.LENGTH_LONG).show()
}
}
OfflineItemManager.State.PermissionRequired -> {
this.requestPermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
WRITE_PERMISSION_REQUEST)
}
}
})
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
if(requestCode == WRITE_PERMISSION_REQUEST){
if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
recordingToDownload?.let { viewModel.download(event, it) }
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
}
interface OnEventDetailsFragmentInteractionListener { interface OnEventDetailsFragmentInteractionListener {
fun onToolbarStateChange() fun onToolbarStateChange()
fun invalidateOptionsMenu() fun invalidateOptionsMenu()
fun playItem(event: Event, recording: Recording, localFile: String? = null)
} }
companion object { companion object {