Search videos and infinite scroll

This commit is contained in:
ivan agosto 2019-02-09 14:56:56 -06:00
parent 305acc87e7
commit c07c15c766
11 changed files with 216 additions and 41 deletions

View File

@ -15,6 +15,7 @@ import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.widget.ImageView
import android.widget.SearchView
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.app_bar_main.*
@ -26,7 +27,7 @@ import org.libre.agosto.p2play.models.VideoModel
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
private lateinit var recyclerView: RecyclerView
private lateinit var viewAdapter: RecyclerView.Adapter<*>
private lateinit var viewAdapter: RecyclerView.Adapter<VideosAdapter.ViewHolder>
private lateinit var viewManager: RecyclerView.LayoutManager
private val client: Videos = Videos()
private lateinit var lastItem: MenuItem
@ -34,6 +35,8 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
lateinit var myMenu: Menu
private val _db = Database(this)
var section: String = ""
var searchVal: String = ""
var pagination = 0
override fun onCreate(savedInstanceState: Bundle?) {
@ -46,11 +49,13 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
.setAction("Action", null).show()
} */
val toggle = ActionBarDrawerToggle(
this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
val toggle = ActionBarDrawerToggle(this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
drawer_layout.addDrawerListener(toggle)
toggle.syncState()
// Search bar
// Context for ManagerSingleton
ManagerSingleton.context = this
@ -58,8 +63,8 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
viewManager = LinearLayoutManager(this)
// Set data for RecyclerView
this.setData(arrayListOf())
// Init RecyclerView
this.initRecycler()
this.getLastVideos()
@ -74,7 +79,8 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
}
// Generic function for set data to RecyclerView
private fun setData(data:ArrayList<VideoModel>){
private fun initRecycler(){
val data = arrayListOf<VideoModel>()
viewAdapter = VideosAdapter(data)
recyclerView = findViewById<RecyclerView>(R.id.list).apply {
@ -87,17 +93,48 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
// specify an viewAdapter (see also next example)
adapter = viewAdapter
this.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
if(!swipeContainer.isRefreshing){
if(!canScrollVertically(1)){
loadMore()
}
}
}
})
}
swipeContainer.isRefreshing = false
// swipeContainer.isRefreshing = false
}
private fun addVideos(videos: ArrayList<VideoModel>){
this.swipeContainer.isRefreshing = true
try {
if(this.pagination == 0){
(viewAdapter as VideosAdapter)?.clearData()
recyclerView.scrollToPosition(0)
}
(viewAdapter as VideosAdapter)?.addData(videos)
}catch (err: Exception){
err.printStackTrace()
ManagerSingleton.Toast(getString(R.string.errorMsg))
}
this.swipeContainer.isRefreshing = false
}
private fun refresh(){
swipeContainer.isRefreshing = true
this.pagination = 0
when(section){
"local" -> this.getLocalVideos()
"popular" -> this.getPopularVideos()
"last" -> this.getLastVideos()
"sub" -> this.getSubscriptionVideos()
"search" -> this.searchVideos()
"my_videos" -> {
if(ManagerSingleton.token.token != "")
this.getMyVideos()
@ -117,9 +154,9 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
section = "sub"
setTitle(R.string.title_subscriptions)
AsyncTask.execute {
val videos = client.videoSubscriptions(ManagerSingleton.token.token)
val videos = client.videoSubscriptions(ManagerSingleton.token.token, this.pagination)
runOnUiThread {
this.setData(videos)
this.addVideos(videos)
}
}
}
@ -130,9 +167,9 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
section = "last"
setTitle(R.string.title_recent)
AsyncTask.execute {
val videos = client.getLastVideos()
val videos = client.getLastVideos(this.pagination)
runOnUiThread {
this.setData(videos)
this.addVideos(videos)
}
}
}
@ -143,9 +180,9 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
section = "popular"
setTitle(R.string.title_popular)
AsyncTask.execute {
val videos = client.getPopularVideos()
val videos = client.getPopularVideos(this.pagination)
runOnUiThread {
this.setData(videos)
this.addVideos(videos)
}
}
}
@ -156,9 +193,9 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
section = "local"
setTitle(R.string.title_local)
AsyncTask.execute {
val videos = client.getLocalVideos()
val videos = client.getLocalVideos(this.pagination)
runOnUiThread {
this.setData(videos)
this.addVideos(videos)
}
}
}
@ -169,9 +206,9 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
section = "my_videos"
setTitle(R.string.title_myVideos)
AsyncTask.execute {
val videos = client.myVideos(ManagerSingleton.token.token)
val videos = client.myVideos(ManagerSingleton.token.token, this.pagination)
runOnUiThread {
this.setData(videos)
this.addVideos(videos)
}
}
}
@ -187,6 +224,26 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.main, menu)
val searchItem = menu.findItem(R.id.app_bar_search)
val searchView = searchItem.actionView as SearchView
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener{
override fun onQueryTextChange(p0: String?): Boolean {
return true
}
override fun onQueryTextSubmit(p0: String?): Boolean {
if(!p0.isNullOrBlank()){
searchVal = p0
searchVideos()
}
return true
}
})
myMenu = menu
setSideData()
return true
@ -228,6 +285,9 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
// lastItem.isChecked = false
// }
lastItem = item
// item.isChecked = true
when (item.itemId) {
R.id.nav_subscriptions->{
@ -293,5 +353,35 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
}
private fun loadMore(){
swipeContainer.isRefreshing = true
this.pagination += 30
when(section){
"local" -> this.getLocalVideos()
"popular" -> this.getPopularVideos()
"last" -> this.getLastVideos()
"sub" -> this.getSubscriptionVideos()
"search" -> this.searchVideos()
"my_videos" -> {
if(ManagerSingleton.token.token != "")
this.getMyVideos()
else
this.getLastVideos()
}
}
}
private fun searchVideos(){
swipeContainer.isRefreshing = true
section = "search"
setTitle(this.searchVal)
AsyncTask.execute {
val videos = client.search(this.searchVal, this.pagination)
runOnUiThread {
this.addVideos(videos)
}
}
}
}

View File

@ -52,6 +52,7 @@ class ReproductorActivity : AppCompatActivity() {
viewsTxt.text = this.video.views.toString() + ' ' + getString(R.string.view_text)
userTxt.text = this.video.username
descriptionVideoTxt.text = this.video.description.toString()
hostTxt.text = this.video.userHost.toString()
// Check if user had profile image
if(this.video.userImageUrl!="")

View File

@ -20,6 +20,7 @@ import org.libre.agosto.p2play.models.VideoModel
import java.io.InputStream
import java.io.Serializable
import java.net.URL
import java.util.concurrent.TimeUnit
class VideosAdapter(private val myDataset: ArrayList<VideoModel>) :
RecyclerView.Adapter<VideosAdapter.ViewHolder>() {
@ -66,14 +67,40 @@ class VideosAdapter(private val myDataset: ArrayList<VideoModel>) :
ManagerSingleton.context!!.startActivity(intent)
}
if(myDataset[position].userImageUrl!="")
Picasso.get().load("https://"+ManagerSingleton.url+myDataset[position].userImageUrl).into(holder.userImg);
Picasso.get().load("https://"+ManagerSingleton.url+myDataset[position].userImageUrl).into(holder.userImg)
else
Picasso.get().load(R.drawable.default_avatar).into(holder.userImg)
val viewsText = ManagerSingleton.context!!.getString(R.string.view_text)
val timeText = ManagerSingleton.context!!.getString(R.string.time_text)
holder.description.text = myDataset[position].username+" - "+myDataset[position].views+" "+viewsText+" - "+myDataset[position].duration+" "+timeText
var timeText = ManagerSingleton.context!!.getString(R.string.timeSec_text)
var timeString = myDataset[position].duration.toString()
val seconds = myDataset[position].duration.toInt();
if(seconds > 60 && seconds < (60 * 60)){
timeText = ManagerSingleton.context!!.getString(R.string.timeMin_text)
timeString = (seconds / 60).toString() + ":" + (seconds % 60).toString()
}
else if(seconds > (60 * 60)){
timeText = ManagerSingleton.context!!.getString(R.string.timeHrs_text)
timeString = (seconds / 60 / 60).toString() + ":" + (seconds / 60 % 60).toString()
}
holder.description.text = myDataset[position].username+" - "+myDataset[position].views+" "+viewsText+" - "+timeString+" "+timeText
}
// Return the size of your dataset (invoked by the layout manager)
override fun getItemCount() = myDataset.size
fun clearData(){
myDataset.clear()
notifyDataSetChanged()
}
fun addData(newItems: ArrayList<VideoModel>){
val lastPos = myDataset.size - 1
myDataset.addAll(newItems)
notifyItemRangeInserted(lastPos, newItems.size)
}
}

View File

@ -9,7 +9,7 @@ import java.io.InputStreamReader
class Videos: Client() {
fun parseVideos(data: JsonReader): ArrayList<VideoModel>{
private fun parseVideos(data: JsonReader): ArrayList<VideoModel>{
var videos = arrayListOf<VideoModel>()
data.beginObject()
while (data.hasNext()){
@ -93,8 +93,8 @@ class Videos: Client() {
}
private fun getVideos(start:Int, count:Int, sort:String = "-publishedAt", filter:String = ""):ArrayList<VideoModel>{
val nfsw = ManagerSingleton.nfsw
var params = "start=$start&count=$count&sort=$sort&nsfw=$nfsw"
val nsfw = ManagerSingleton.nfsw
var params = "start=$start&count=$count&sort=$sort&nsfw=$nsfw"
if(filter != "")
params+="&filter=$filter"
var con=this._newCon("videos?$params","GET")
@ -125,8 +125,9 @@ class Videos: Client() {
return this.getVideos(start, count,"-publishedAt", "local")
}
fun myVideos(token: String): ArrayList<VideoModel>{
var con=this._newCon("users/me/videos","GET", token)
fun myVideos(token: String, start: Int = 0, count: Int = 30): ArrayList<VideoModel>{
val params = "start=$start&count=$count"
var con=this._newCon("users/me/videos?$params","GET", token)
var videos = arrayListOf<VideoModel>()
try {
if (con.responseCode == 200) {
@ -142,8 +143,28 @@ class Videos: Client() {
return videos
}
fun videoSubscriptions(token: String): ArrayList<VideoModel>{
var con=this._newCon("users/me/subscriptions/videos","GET", token)
fun videoSubscriptions(token: String, start: Int = 0, count: Int = 30): ArrayList<VideoModel>{
val params = "start=$start&count=$count"
var con=this._newCon("users/me/subscriptions/videos?$params","GET", token)
var videos = arrayListOf<VideoModel>()
try {
if (con.responseCode == 200) {
var response = InputStreamReader(con.inputStream)
var data = JsonReader(response)
videos = parseVideos(data)
}
} catch(err:Exception){
err?.printStackTrace()
Log.d("TypeErr",err?.message ,err.cause)
Log.d("Error","fallo la coneccion")
}
return videos
}
fun search(text: String, start: Int = 0, count: Int = 30): ArrayList<VideoModel>{
val nsfw = ManagerSingleton.nfsw
val params = "search=$text&start=$start&count=$count&nsfw=$nsfw"
var con=this._newCon("search/videos?$params","GET")
var videos = arrayListOf<VideoModel>()
try {
if (con.responseCode == 200) {

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zM9.5,14C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14z"/>
</vector>

View File

@ -37,6 +37,7 @@
android:layout_height="wrap_content"
android:paddingStart="5dp"
android:textAppearance="@android:style/TextAppearance.Material.Large"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
@ -147,21 +148,38 @@
android:scaleType="centerInside"
app:srcCompat="@drawable/default_avatar" />
<TextView
android:id="@+id/userTxt"
android:layout_width="70dp"
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="2"
android:textAppearance="@android:style/TextAppearance.Material.Large"
android:textColor="@android:color/black" />
android:orientation="vertical">
<TextView
android:id="@+id/userTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@android:style/TextAppearance.Material.Large"
android:textColor="@android:color/black"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/hostTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="10sp"
android:textStyle="italic" />
</LinearLayout>
<Button
android:id="@+id/subscribeBtn"
style="@android:style/Widget.Holo.Button.Small"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/subscribeBtn"
android:textSize="10sp"
android:visibility="gone" />
</LinearLayout>

View File

@ -3,13 +3,13 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@android:color/darker_gray"
android:background="@android:color/holo_orange_dark"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<ImageView

View File

@ -48,16 +48,15 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength="70"
android:text="TextView"
android:textSize="18sp"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="@+id/descriptionTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="@android:color/darker_gray" />
android:textColor="@android:color/darker_gray"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>

View File

@ -2,6 +2,12 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/app_bar_search"
android:icon="@drawable/ic_search_black_24dp"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="always"/>
<item
android:id="@+id/action_login"
android:title="@string/action_login"

View File

@ -43,7 +43,9 @@
<string name="title_subscriptions">Suscripciones</string>
<string name="title_myVideos">Mis videos</string>
<string name="view_text">vistas</string>
<string name="time_text">segundos</string>
<string name="timeSec_text">segundos</string>
<string name="timeMin_text">minutos</string>
<string name="timeHrs_text">horas</string>
<string name="nav_header_title">Inicia session</string>
<!-- Toast msg -->
<string name="logout_msg">Te has desconectado</string>

View File

@ -53,7 +53,9 @@
<string name="title_local">Local videos</string>
<string name="title_myVideos">My videos</string>
<string name="view_text">views</string>
<string name="time_text">seconds</string>
<string name="timeSec_text">seconds</string>
<string name="timeMin_text">minutes</string>
<string name="timeHrs_text">hours</string>
<string name="nav_header_title">Log In</string>
<string name="nav_header_subtitle" translatable="false">P2Play</string>
<!-- Toast msg -->