From 398265c6ed7411d23b5e9c26b28b76e9180539a6 Mon Sep 17 00:00:00 2001 From: ivan agosto Date: Sun, 24 Feb 2019 10:34:12 -0600 Subject: [PATCH] Channel view maked and working --- app/src/main/AndroidManifest.xml | 6 +- .../libre/agosto/p2play/ChannelActivity.kt | 149 +++++++++++++++++ .../agosto/p2play/ReproductorActivity.kt | 14 +- .../p2play/adapters/CommentariesAdapter.kt | 11 +- .../agosto/p2play/adapters/VideosAdapter.kt | 12 +- .../org/libre/agosto/p2play/ajax/Channels.kt | 4 +- .../org/libre/agosto/p2play/ajax/Client.kt | 1 + .../org/libre/agosto/p2play/ajax/Videos.kt | 20 +++ app/src/main/res/layout/activity_channel.xml | 154 ++++++++++++++++++ app/src/main/res/values/strings.xml | 5 + 10 files changed, 359 insertions(+), 17 deletions(-) create mode 100644 app/src/main/java/org/libre/agosto/p2play/ChannelActivity.kt create mode 100644 app/src/main/res/layout/activity_channel.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 461580c..e03938d 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -12,6 +12,8 @@ android:roundIcon="@mipmap/ic_p2play" android:supportsRtl="true" android:theme="@style/P2playTheme"> + @@ -27,9 +29,9 @@ android:theme="@style/P2playTheme.NoActionBar" /> + android:theme="@style/P2playTheme.noBar" /> diff --git a/app/src/main/java/org/libre/agosto/p2play/ChannelActivity.kt b/app/src/main/java/org/libre/agosto/p2play/ChannelActivity.kt new file mode 100644 index 0000000..dc58c7b --- /dev/null +++ b/app/src/main/java/org/libre/agosto/p2play/ChannelActivity.kt @@ -0,0 +1,149 @@ +package org.libre.agosto.p2play + +import android.os.AsyncTask +import android.support.v7.app.AppCompatActivity +import android.os.Bundle +import android.support.v7.widget.LinearLayoutManager +import android.support.v7.widget.RecyclerView +import android.view.View +import com.squareup.picasso.Picasso +import kotlinx.android.synthetic.main.activity_channel.* +import org.libre.agosto.p2play.adapters.VideosAdapter +import org.libre.agosto.p2play.ajax.Actions +import org.libre.agosto.p2play.ajax.Channels +import org.libre.agosto.p2play.ajax.Videos +import org.libre.agosto.p2play.models.ChannelModel +import org.libre.agosto.p2play.models.VideoModel + +class ChannelActivity : AppCompatActivity() { + private lateinit var channelId: String + private lateinit var channel: ChannelModel + private var isSubcribed: Boolean = false + private val _channel = Channels() + private val _videos = Videos() + private val _actions = Actions() + + private lateinit var recyclerView: RecyclerView + private lateinit var viewAdapter: RecyclerView.Adapter + private lateinit var viewManager: RecyclerView.LayoutManager + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_channel) + + channelId = this.intent.extras.getString("channel") + + viewManager = LinearLayoutManager(this) + + subcriptionBtn.setOnClickListener { + subscribeAction() + } + } + + override fun onResume() { + super.onResume() + + getChannel() + getSubscription() + getVideos() + + if(ManagerSingleton.user.status == 1) { + subcriptionBtn.visibility = View.VISIBLE + getSubscription() + } + } + + private fun getChannel() { + AsyncTask.execute { + channel = _channel.getChannelInfo(channelId) + runOnUiThread { + usernameProfile.text = channel.name + hostTxt.text = channel.host + subcriptionsTxt.text = channel.followers.toString() + if(channel.channelImg != "") + Picasso.get().load("https://${ManagerSingleton.url}${channel.channelImg}").into(channelImg) + } + } + } + + private fun subscribe() { + AsyncTask.execute { + val res = _actions.subscribe(ManagerSingleton.token.token, channel.getAccount()) + runOnUiThread { + if(res == 1){ + subcriptionBtn.text = getString(R.string.unSubscribeBtn) + ManagerSingleton.Toast(getString(R.string.subscribeMsg), this) + getSubscription() + } + else { + ManagerSingleton.Toast(getString(R.string.errorMsg), this) + } + } + } + } + + private fun unSubscribe() { + AsyncTask.execute { + val res = _actions.unSubscribe(ManagerSingleton.token.token, channel.getAccount()) + runOnUiThread { + if(res == 1){ + subcriptionBtn.text = getString(R.string.subscribeBtn) + ManagerSingleton.Toast(getString(R.string.unSubscribeMsg), this) + getSubscription() + } + else { + ManagerSingleton.Toast(getString(R.string.errorMsg), this) + } + } + } + } + + private fun subscribeAction() { + if(isSubcribed) + unSubscribe() + else + subscribe() + } + + private fun getSubscription() { + AsyncTask.execute { + isSubcribed = _actions.getSubscription(ManagerSingleton.token.token, channel.getAccount()) + runOnUiThread { + if(isSubcribed){ + subcriptionBtn.text = getText(R.string.unSubscribeBtn) + } + else { + subcriptionBtn.text = getText(R.string.subscribeBtn) + } + } + } + } + + private fun getVideos() { + AsyncTask.execute { + val videos = _videos.channelVideos(channel.getAccount(), 0) + runOnUiThread { + initRecycler(videos) + } + } + } + + // Generic function for set data to RecyclerView + private fun initRecycler(data: ArrayList){ + // val data = arrayListOf() + viewAdapter = VideosAdapter(data) + + recyclerView = findViewById(R.id.listVideosChannel).apply { + // use this setting to improve performance if you know that changes + // in content do not change the layout size of the RecyclerView + setHasFixedSize(true) + + // use a linear layout manager + layoutManager = viewManager + + // specify an viewAdapter (see also next example) + adapter = viewAdapter + } + // swipeContainer.isRefreshing = false + } +} \ No newline at end of file diff --git a/app/src/main/java/org/libre/agosto/p2play/ReproductorActivity.kt b/app/src/main/java/org/libre/agosto/p2play/ReproductorActivity.kt index 981946f..4370d7f 100644 --- a/app/src/main/java/org/libre/agosto/p2play/ReproductorActivity.kt +++ b/app/src/main/java/org/libre/agosto/p2play/ReproductorActivity.kt @@ -2,6 +2,7 @@ package org.libre.agosto.p2play import android.annotation.SuppressLint import android.app.Activity +import android.content.Intent import android.content.pm.ActivityInfo import android.graphics.Bitmap import android.graphics.BitmapFactory @@ -73,7 +74,6 @@ class ReproductorActivity : AppCompatActivity() { // Load the video videoView.loadUrl("https://"+ManagerSingleton.url+this.video.embedUrl) - } catch (err:Exception){ err.printStackTrace() @@ -89,14 +89,19 @@ class ReproductorActivity : AppCompatActivity() { dislikeLayout.setOnClickListener { rate("dislike") } commentaryBtn.setOnClickListener { makeComment() } showMoreBtn.setOnClickListener { getDescription() } + + userImg.setOnClickListener { + val intent = Intent(this, ChannelActivity::class.java) + intent.putExtra("channel", video.getAccount()) + startActivity(intent) + } } private fun subscribe(){ - val account = this.video.nameChannel+"@"+this.video.userHost AsyncTask.execute { if (Looper.myLooper() == null) Looper.prepare() - val res = this._actions.subscribe(ManagerSingleton.token.token, account) + val res = this._actions.subscribe(ManagerSingleton.token.token, video.getAccount()) if (res == 1) { runOnUiThread { ManagerSingleton.Toast(getString(R.string.subscribeMsg), this) @@ -107,11 +112,10 @@ class ReproductorActivity : AppCompatActivity() { } private fun unSubscribe(){ - val account = this.video.nameChannel+"@"+this.video.userHost AsyncTask.execute { if (Looper.myLooper() == null) Looper.prepare() - val res = this._actions.unSubscribe(ManagerSingleton.token.token, account) + val res = this._actions.unSubscribe(ManagerSingleton.token.token, video.getAccount()) if (res == 1) { runOnUiThread { ManagerSingleton.Toast(getString(R.string.unSubscribeMsg), this) diff --git a/app/src/main/java/org/libre/agosto/p2play/adapters/CommentariesAdapter.kt b/app/src/main/java/org/libre/agosto/p2play/adapters/CommentariesAdapter.kt index e02e325..96165ec 100644 --- a/app/src/main/java/org/libre/agosto/p2play/adapters/CommentariesAdapter.kt +++ b/app/src/main/java/org/libre/agosto/p2play/adapters/CommentariesAdapter.kt @@ -14,10 +14,7 @@ import android.widget.ImageView import android.widget.LinearLayout import android.widget.TextView import com.squareup.picasso.Picasso -import org.libre.agosto.p2play.MainActivity -import org.libre.agosto.p2play.ManagerSingleton -import org.libre.agosto.p2play.R -import org.libre.agosto.p2play.ReproductorActivity +import org.libre.agosto.p2play.* import org.libre.agosto.p2play.models.CommentaryModel import org.libre.agosto.p2play.models.VideoModel import java.io.InputStream @@ -63,6 +60,12 @@ class CommentariesAdapter(private val myDataset: ArrayList) : // - replace the contents of the view with that element holder.username.text = myDataset[position].username + // holder.userImg.setOnClickListener { + // val intent = Intent(holder.context, ChannelActivity::class.java) + // intent.putExtra("channel", myDataset[position]) + // holder.context.startActivity(intent) + // } + if(myDataset[position].userImageUrl!="") Picasso.get().load("https://"+ManagerSingleton.url+myDataset[position].userImageUrl).into(holder.userImg); diff --git a/app/src/main/java/org/libre/agosto/p2play/adapters/VideosAdapter.kt b/app/src/main/java/org/libre/agosto/p2play/adapters/VideosAdapter.kt index 4a5624c..8281661 100644 --- a/app/src/main/java/org/libre/agosto/p2play/adapters/VideosAdapter.kt +++ b/app/src/main/java/org/libre/agosto/p2play/adapters/VideosAdapter.kt @@ -13,10 +13,7 @@ import android.widget.ImageView import android.widget.LinearLayout import android.widget.TextView import com.squareup.picasso.Picasso -import org.libre.agosto.p2play.MainActivity -import org.libre.agosto.p2play.ManagerSingleton -import org.libre.agosto.p2play.R -import org.libre.agosto.p2play.ReproductorActivity +import org.libre.agosto.p2play.* import org.libre.agosto.p2play.models.VideoModel import java.io.InputStream import java.io.Serializable @@ -69,6 +66,13 @@ class VideosAdapter(private val myDataset: ArrayList) : intent.putExtra("video", myDataset[position] as Serializable) holder.context.startActivity(intent) } + + holder.userImg.setOnClickListener { + val intent = Intent(holder.context, ChannelActivity::class.java) + intent.putExtra("channel", myDataset[position].getAccount()) + holder.context.startActivity(intent) + } + if(myDataset[position].userImageUrl!="") Picasso.get().load("https://"+ManagerSingleton.url+myDataset[position].userImageUrl).into(holder.userImg) else diff --git a/app/src/main/java/org/libre/agosto/p2play/ajax/Channels.kt b/app/src/main/java/org/libre/agosto/p2play/ajax/Channels.kt index 798fed1..906b907 100644 --- a/app/src/main/java/org/libre/agosto/p2play/ajax/Channels.kt +++ b/app/src/main/java/org/libre/agosto/p2play/ajax/Channels.kt @@ -16,8 +16,8 @@ class Channels: Client() { return channel } - fun getChannelInfo(channelId: String): ChannelModel { - val con = this._newCon("video-channels/$channelId", "GET") + fun getChannelInfo(account: String): ChannelModel { + val con = this._newCon("video-channels/$account", "GET") var channel = ChannelModel() try { if(con.responseCode == 200){ diff --git a/app/src/main/java/org/libre/agosto/p2play/ajax/Client.kt b/app/src/main/java/org/libre/agosto/p2play/ajax/Client.kt index 815779f..b4d627c 100644 --- a/app/src/main/java/org/libre/agosto/p2play/ajax/Client.kt +++ b/app/src/main/java/org/libre/agosto/p2play/ajax/Client.kt @@ -5,6 +5,7 @@ import android.util.Log import org.libre.agosto.p2play.ManagerSingleton import org.libre.agosto.p2play.models.HostModel import java.io.InputStreamReader +import java.io.Reader import java.net.HttpURLConnection import java.net.URL diff --git a/app/src/main/java/org/libre/agosto/p2play/ajax/Videos.kt b/app/src/main/java/org/libre/agosto/p2play/ajax/Videos.kt index 341b786..fb47ff2 100644 --- a/app/src/main/java/org/libre/agosto/p2play/ajax/Videos.kt +++ b/app/src/main/java/org/libre/agosto/p2play/ajax/Videos.kt @@ -154,4 +154,24 @@ class Videos: Client() { con.disconnect() return description } + + fun channelVideos(account: String, start: Int): ArrayList { + val count = ManagerSingleton.videos_count + val params = "start=$start&count=$count" + val con = this._newCon("video-channels/$account/videos","GET") + var videos = arrayListOf() + try { + if (con.responseCode == 200) { + val response = InputStreamReader(con.inputStream) + val data = JsonReader(response) + videos = parseVideos(data) + data.close() + } + } catch(err:Exception){ + err.printStackTrace() + } + + con.disconnect() + return videos + } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_channel.xml b/app/src/main/res/layout/activity_channel.xml new file mode 100644 index 0000000..c78b087 --- /dev/null +++ b/app/src/main/res/layout/activity_channel.xml @@ -0,0 +1,154 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +