From 9bf8ea0483db403740e6d4e37b7b07b2dda1c085 Mon Sep 17 00:00:00 2001 From: ivan agosto Date: Thu, 24 Jan 2019 15:56:20 -0600 Subject: [PATCH] Adapter and model of commentaries maked --- .../org/libre/agosto/p2play/MainActivity.kt | 24 ++++--- .../agosto/p2play/ReproductorActivity.kt | 46 ++++++++++-- .../p2play/adapters/CommentariesAdapter.kt | 71 +++++++++++++++++++ .../agosto/p2play/models/CommentaryModel.kt | 9 +++ .../main/res/layout/activity_reproductor.xml | 8 +++ app/src/main/res/layout/view_commentary.xml | 48 +++++++++++++ 6 files changed, 188 insertions(+), 18 deletions(-) create mode 100644 app/src/main/java/org/libre/agosto/p2play/adapters/CommentariesAdapter.kt create mode 100644 app/src/main/java/org/libre/agosto/p2play/models/CommentaryModel.kt create mode 100644 app/src/main/res/layout/view_commentary.xml diff --git a/app/src/main/java/org/libre/agosto/p2play/MainActivity.kt b/app/src/main/java/org/libre/agosto/p2play/MainActivity.kt index b58ab01..e236c93 100644 --- a/app/src/main/java/org/libre/agosto/p2play/MainActivity.kt +++ b/app/src/main/java/org/libre/agosto/p2play/MainActivity.kt @@ -27,11 +27,11 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte private lateinit var recyclerView: RecyclerView private lateinit var viewAdapter: RecyclerView.Adapter<*> private lateinit var viewManager: RecyclerView.LayoutManager - val client: Videos = Videos() + private val client: Videos = Videos() private lateinit var lastItem: MenuItem private lateinit var subItem: MenuItem lateinit var myMenu: Menu - val _db = Database(this) + private val _db = Database(this) var section: String = "" @@ -67,7 +67,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte } // Generic function for set data to RecyclerView - fun setData(data:ArrayList){ + private fun setData(data:ArrayList){ viewAdapter = VideosAdapter(data) recyclerView = findViewById(R.id.list).apply { @@ -84,7 +84,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte swipeContainer.isRefreshing = false } - fun refresh(){ + private fun refresh(){ swipeContainer.isRefreshing = true when(section){ "local" -> this.getLocalVideos() @@ -100,7 +100,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte } } - fun getSubscriptionVideos(){ + private fun getSubscriptionVideos(){ if(ManagerSingleton.user.status != 1){ ManagerSingleton.Toast("Inicia session primero") startActivity(Intent(this, LoginActivity::class.java)) @@ -118,7 +118,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte } // Last videos - fun getLastVideos(){ + private fun getLastVideos(){ swipeContainer.isRefreshing = true section = "last" setTitle(R.string.title_recent) @@ -130,8 +130,8 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte } } - // - fun getPopularVideos(){ + // Popular videos + private fun getPopularVideos(){ swipeContainer.isRefreshing = true section = "popular" setTitle(R.string.title_popular) @@ -143,7 +143,8 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte } } - fun getLocalVideos(){ + // Local videos + private fun getLocalVideos(){ swipeContainer.isRefreshing = true section = "local" setTitle(R.string.title_local) @@ -155,7 +156,8 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte } } - fun getMyVideos(){ + // Videos of user + private fun getMyVideos(){ swipeContainer.isRefreshing = true section = "my_videos" setTitle(R.string.title_myVideos) @@ -266,7 +268,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte } } - fun logout(){ + private fun logout(){ if(::myMenu.isInitialized){ myMenu.findItem(R.id.action_login).isVisible = true myMenu.findItem(R.id.action_logout).isVisible = false 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 1877e22..e2a0a0a 100644 --- a/app/src/main/java/org/libre/agosto/p2play/ReproductorActivity.kt +++ b/app/src/main/java/org/libre/agosto/p2play/ReproductorActivity.kt @@ -6,10 +6,14 @@ import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.os.Looper import android.support.v4.content.ContextCompat +import android.support.v7.widget.LinearLayoutManager +import android.support.v7.widget.RecyclerView import android.util.Log import android.view.View import com.squareup.picasso.Picasso +import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_reproductor.* +import org.libre.agosto.p2play.adapters.CommentariesAdapter import org.libre.agosto.p2play.ajax.Actions import org.libre.agosto.p2play.models.VideoModel @@ -17,6 +21,11 @@ class ReproductorActivity : AppCompatActivity() { lateinit var video:VideoModel private val _actions: Actions = Actions() + // Commentaries adapter values + 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_reproductor) @@ -30,6 +39,30 @@ class ReproductorActivity : AppCompatActivity() { videoView.settings.allowUniversalAccessFromFileURLs = true videoView.settings.domStorageEnabled = true + try { + + viewManager = LinearLayoutManager(this) + + // Set data for RecyclerView + viewAdapter = CommentariesAdapter(arrayListOf()) + + recyclerView = findViewById(R.id.listCommentaries).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 + } + + }catch (err: Exception){ + err.printStackTrace() + Log.d("Error", err?.message) + } + try { this.video = this.intent.extras.getSerializable("video") as VideoModel tittleVideoTxt.text = this.video.name @@ -47,13 +80,12 @@ class ReproductorActivity : AppCompatActivity() { Log.d("Error", err?.message) } - // subscribeBtn.setOnClickListener { ManagerSingleton.Toast(getString(R.string.comming)) } subscribeBtn.setOnClickListener { subscribe() } likeLayout.setOnClickListener { rate("like") } dislikeLayout.setOnClickListener { rate("dislike") } } - fun subscribe(){ + private fun subscribe(){ val account = this.video.userUuid+"@"+this.video.userHost AsyncTask.execute { if (Looper.myLooper() == null) @@ -68,7 +100,7 @@ class ReproductorActivity : AppCompatActivity() { } } - fun unSubscribe(){ + private fun unSubscribe(){ val account = this.video.userUuid+"@"+this.video.userHost AsyncTask.execute { if (Looper.myLooper() == null) @@ -83,7 +115,7 @@ class ReproductorActivity : AppCompatActivity() { } } - fun rate(rate: String){ + private fun rate(rate: String){ AsyncTask.execute { if (Looper.myLooper() == null) Looper.prepare() @@ -104,7 +136,7 @@ class ReproductorActivity : AppCompatActivity() { } } - fun getRate(){ + private fun getRate(){ AsyncTask.execute { if (Looper.myLooper() == null) Looper.prepare() @@ -128,7 +160,7 @@ class ReproductorActivity : AppCompatActivity() { } } - fun getSubscription(){ + private fun getSubscription(){ val account = this.video.userUuid+"@"+this.video.userHost AsyncTask.execute { if (Looper.myLooper() == null) @@ -140,7 +172,7 @@ class ReproductorActivity : AppCompatActivity() { } } - fun changeSubscribeBtn(subscribed: Boolean){ + private fun changeSubscribeBtn(subscribed: Boolean){ if(subscribed){ subscribeBtn.text = getText(R.string.unSubscribeBtn) subscribeBtn.setOnClickListener { this.unSubscribe() } 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 new file mode 100644 index 0000000..0df1b35 --- /dev/null +++ b/app/src/main/java/org/libre/agosto/p2play/adapters/CommentariesAdapter.kt @@ -0,0 +1,71 @@ +package org.libre.agosto.p2play.adapters + +import android.app.Activity +import android.content.Intent +import android.graphics.drawable.Drawable +import android.os.AsyncTask +import android.support.v7.widget.RecyclerView +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +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.models.CommentaryModel +import org.libre.agosto.p2play.models.VideoModel +import java.io.InputStream +import java.io.Serializable +import java.net.URL + +class CommentariesAdapter(private val myDataset: ArrayList) : + RecyclerView.Adapter() { + + // Provide a reference to the views for each data item + // Complex data items may need more than one view per item, and + // you provide access to all the views for a data item in a view holder. + // Each data item is just a string in this case that is shown in a TextView. + class ViewHolder(val view: View) : RecyclerView.ViewHolder(view){ + val userImg: ImageView + val username: TextView + val commentary: TextView + + init { + // Define click listener for the ViewHolder's View + username = view.findViewById(R.id.userTxt) + commentary = view.findViewById(R.id.userCommentary) + userImg = view.findViewById(R.id.userCommentImg) + } + } + + + // Create new views (invoked by the layout manager) + override fun onCreateViewHolder(parent: ViewGroup, + viewType: Int): CommentariesAdapter.ViewHolder { + // create a new view + val view = LayoutInflater.from(parent.context) + .inflate(R.layout.view_video, parent, false) as View + // set the view's size, margins, paddings and layout parameters + return ViewHolder(view) + } + + // Replace the contents of a view (invoked by the layout manager) + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + // - get element from your dataset at this position + // - replace the contents of the view with that element + holder.username.text = myDataset[position].username + + if(myDataset[position].userImageUrl!="") + Picasso.get().load("https://"+ManagerSingleton.url+myDataset[position].userImageUrl).into(holder.userImg); + + holder.commentary.text = myDataset[position].commentary + + } + + // Return the size of your dataset (invoked by the layout manager) + override fun getItemCount() = myDataset.size +} \ No newline at end of file diff --git a/app/src/main/java/org/libre/agosto/p2play/models/CommentaryModel.kt b/app/src/main/java/org/libre/agosto/p2play/models/CommentaryModel.kt new file mode 100644 index 0000000..2ab6093 --- /dev/null +++ b/app/src/main/java/org/libre/agosto/p2play/models/CommentaryModel.kt @@ -0,0 +1,9 @@ +package org.libre.agosto.p2play.models + +class CommentaryModel ( + var id: Int = 0, + var uuid: Int = 0, + var username: String = "", + var userImageUrl: String = "", + var commentary: String = "" +) \ No newline at end of file diff --git a/app/src/main/res/layout/activity_reproductor.xml b/app/src/main/res/layout/activity_reproductor.xml index a4195ff..29361e9 100644 --- a/app/src/main/res/layout/activity_reproductor.xml +++ b/app/src/main/res/layout/activity_reproductor.xml @@ -227,6 +227,14 @@ + + diff --git a/app/src/main/res/layout/view_commentary.xml b/app/src/main/res/layout/view_commentary.xml new file mode 100644 index 0000000..7f42519 --- /dev/null +++ b/app/src/main/res/layout/view_commentary.xml @@ -0,0 +1,48 @@ + + + + + + + + + + + + + + + \ No newline at end of file