p2play-app-android/app/src/main/java/org/libre/agosto/p2play/ReproductorActivity.kt

164 lines
6.0 KiB
Kotlin
Raw Normal View History

2018-08-18 07:04:31 +02:00
package org.libre.agosto.p2play
2018-10-10 19:24:35 +02:00
import android.opengl.Visibility
import android.os.AsyncTask
2018-08-18 07:04:31 +02:00
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Looper
2018-10-10 19:24:35 +02:00
import android.support.v4.content.ContextCompat
2018-08-18 07:04:31 +02:00
import android.util.Log
2018-10-10 19:24:35 +02:00
import android.view.View
2018-08-18 07:04:31 +02:00
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.activity_reproductor.*
2018-10-10 19:24:35 +02:00
import org.libre.agosto.p2play.ajax.Actions
2018-08-18 07:04:31 +02:00
import org.libre.agosto.p2play.models.VideoModel
class ReproductorActivity : AppCompatActivity() {
lateinit var video:VideoModel
2018-10-10 19:24:35 +02:00
private val _actions: Actions = Actions()
2018-08-18 07:04:31 +02:00
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_reproductor)
ManagerSingleton.context = this
videoView.settings.javaScriptEnabled = true
videoView.settings.allowContentAccess = true
videoView.settings.javaScriptCanOpenWindowsAutomatically = true
videoView.settings.allowFileAccess = true
videoView.settings.allowFileAccessFromFileURLs = true
videoView.settings.allowUniversalAccessFromFileURLs = true
videoView.settings.domStorageEnabled = true
2018-08-18 07:04:31 +02:00
try {
this.video = this.intent.extras.getSerializable("video") as VideoModel
tittleVideoTxt.text = this.video.name
2018-09-11 00:45:51 +02:00
viewsTxt.text = this.video.views.toString() + ' ' + getString(R.string.view_text)
2018-08-18 07:04:31 +02:00
userTxt.text = this.video.username
descriptionVideoTxt.text = this.video.description
if(this.video.userImageUrl!="")
Picasso.get().load("https://"+ManagerSingleton.url+this.video.userImageUrl).into(userImg)
videoView.loadUrl("https://"+ManagerSingleton.url+this.video.embedUrl)
Log.d("url", videoView.url)
}
catch (err:Exception){
err.printStackTrace()
Log.d("Error", err?.message)
}
// subscribeBtn.setOnClickListener { ManagerSingleton.Toast(getString(R.string.comming)) }
subscribeBtn.setOnClickListener { subscribe() }
2018-10-10 19:24:35 +02:00
likeLayout.setOnClickListener { rate("like") }
dislikeLayout.setOnClickListener { rate("dislike") }
}
fun subscribe(){
val account = this.video.userUuid+"@"+this.video.userHost
AsyncTask.execute {
if (Looper.myLooper() == null)
Looper.prepare()
2018-10-10 19:24:35 +02:00
val res = this._actions.subscribe(ManagerSingleton.token.token, account)
if (res == 1) {
runOnUiThread {
ManagerSingleton.Toast(getString(R.string.subscribeMsg))
this.changeSubscribeBtn(true)
}
}
}
}
fun unSubscribe(){
val account = this.video.userUuid+"@"+this.video.userHost
AsyncTask.execute {
if (Looper.myLooper() == null)
Looper.prepare()
val res = this._actions.unSubscribe(ManagerSingleton.token.token, account)
if (res == 1) {
runOnUiThread {
ManagerSingleton.Toast(getString(R.string.unSubscribeMsg))
this.changeSubscribeBtn(false)
}
}
}
2018-08-18 07:04:31 +02:00
}
2018-10-10 19:24:35 +02:00
fun rate(rate: String){
AsyncTask.execute {
if (Looper.myLooper() == null)
Looper.prepare()
val res = this._actions.rate(ManagerSingleton.token.token, this.video.id, rate)
if (res == 1) {
runOnUiThread {
ManagerSingleton.Toast(getString(R.string.rateMsg))
if(rate=="like"){
likeLayout.setBackgroundColor(ContextCompat.getColor(this, R.color.colorLike))
dislikeLayout.background = null
}
else if(rate=="dislike"){
dislikeLayout.setBackgroundColor(ContextCompat.getColor(this, R.color.colorDislike))
likeLayout.background = null
}
}
}
}
}
fun getRate(){
AsyncTask.execute {
if (Looper.myLooper() == null)
Looper.prepare()
val rate = this._actions.getRate(ManagerSingleton.token.token, this.video.id)
runOnUiThread {
when (rate){
"like" -> {
likeLayout.setBackgroundColor(ContextCompat.getColor(this, R.color.colorLike))
dislikeLayout.background = null
}
"dislike" -> {
dislikeLayout.setBackgroundColor(ContextCompat.getColor(this, R.color.colorDislike))
likeLayout.background = null
}
else -> {
likeLayout.background = null
dislikeLayout.background = null
}
}
}
}
}
fun getSubscription(){
val account = this.video.userUuid+"@"+this.video.userHost
AsyncTask.execute {
if (Looper.myLooper() == null)
Looper.prepare()
val isSubscribed = this._actions.getSubscription(ManagerSingleton.token.token, account)
runOnUiThread {
this.changeSubscribeBtn(isSubscribed)
}
}
}
fun changeSubscribeBtn(subscribed: Boolean){
if(subscribed){
subscribeBtn.text = getText(R.string.unSubscribeBtn)
subscribeBtn.setOnClickListener { this.unSubscribe() }
}
else{
subscribeBtn.text = getText(R.string.subscribeBtn)
subscribeBtn.setOnClickListener { this.subscribe() }
}
}
2018-10-10 19:24:35 +02:00
override fun onResume() {
super.onResume()
if(ManagerSingleton.user.status == 1) {
this.getRate()
this.getSubscription()
2018-10-10 19:24:35 +02:00
actionsLayout.visibility = View.VISIBLE
subscribeBtn.visibility = View.VISIBLE
}
}
2018-08-18 07:04:31 +02:00
}