Checking if you rate the video and if are subscribed to the channel

This commit is contained in:
ivan agosto 2018-10-11 11:56:29 -05:00
parent a92064552e
commit 09f8337ff0
5 changed files with 151 additions and 2 deletions

View File

@ -131,7 +131,6 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/instant_run_split_apk_resources" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/javac" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/lint_jar" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifest-checker" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/merged_assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/merged_manifests" />

View File

@ -11,7 +11,8 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_p2play"
android:supportsRtl="true"
android:theme="@style/P2playTheme">
android:theme="@style/P2playTheme"
android:hardwareAccelerated="true">
<activity android:name=".HostActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

View File

@ -62,6 +62,22 @@ class ReproductorActivity : AppCompatActivity() {
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)
}
}
}
@ -88,9 +104,58 @@ class ReproductorActivity : AppCompatActivity() {
}
}
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() }
}
}
override fun onResume() {
super.onResume()
if(ManagerSingleton.user.status == 1) {
this.getRate()
this.getSubscription()
actionsLayout.visibility = View.VISIBLE
subscribeBtn.visibility = View.VISIBLE
}

View File

@ -1,5 +1,8 @@
package org.libre.agosto.p2play.ajax
import android.util.JsonReader
import java.io.InputStreamReader
class Actions: Client() {
fun subscribe(token: String, account: String):Int{
@ -21,6 +24,53 @@ class Actions: Client() {
return response
}
fun unSubscribe(token: String, account: String):Int{
var con=this._newCon("users/me/subscriptions/$account","DELETE", token)
var response = 0
try {
if (con.responseCode == 204) {
response = 1
}
}
catch (err: Exception){
err.printStackTrace()
response = -1
}
return response
}
fun getSubscription(token: String, account: String): Boolean{
var con=this._newCon("users/me/subscriptions/exist?uris=$account","GET", token)
var isSubscribed = false
try {
if (con.responseCode == 200) {
var response = InputStreamReader(con.inputStream)
var data = JsonReader(response)
data.beginObject()
while (data.hasNext()){
val key = data.nextName()
when (key.toString()) {
account->{
isSubscribed = data.nextBoolean()
}
else->{
data.skipValue()
}
}
}
}
}
catch (err: Exception){
err.printStackTrace()
isSubscribed = false
}
return isSubscribed
}
fun rate(token: String, id_video: Int, rate: String):Int{
var con=this._newCon("videos/$id_video/rate","PUT", token)
val params:String= "rating=$rate"
@ -39,4 +89,35 @@ class Actions: Client() {
return response
}
fun getRate(token: String, id_video: Int):String{
var con=this._newCon("users/me/videos/$id_video/rating","GET", token)
var rating = "none"
try {
if (con.responseCode == 200) {
var response = InputStreamReader(con.inputStream)
var data = JsonReader(response)
data.beginObject()
while (data.hasNext()){
val key = data.nextName()
when (key.toString()) {
"rating"->{
rating = data.nextString()
}
else->{
data.skipValue()
}
}
}
}
}
catch (err: Exception){
err.printStackTrace()
rating = "none"
}
return rating
}
}

View File

@ -9,8 +9,11 @@
<string name="shareBtn">Share</string>
<string name="reportBtn">Report</string>
<string name="unSubscribeBtn">Unsubscribe</string>
<!-- Messages -->
<string name="subscribeMsg">You are subscribed to this channel</string>
<string name="rateMsg">You are rated the video</string>
<string name="unSubscribeMsg">You are unsubscribed to this channel</string>
</resources>