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

434 lines
14 KiB
Kotlin
Raw Normal View History

2018-08-18 07:04:31 +02:00
package org.libre.agosto.p2play
import android.content.Intent
import android.os.AsyncTask
2024-03-23 00:23:16 +01:00
import android.os.Build
2018-08-18 07:04:31 +02:00
import android.os.Bundle
2019-01-30 19:40:36 +01:00
import android.os.Handler
2018-08-18 07:04:31 +02:00
import android.view.Menu
import android.view.MenuItem
2024-03-23 00:23:16 +01:00
import android.view.WindowManager
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.core.view.GravityCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.navigation.NavigationView
2018-08-18 07:04:31 +02:00
import com.squareup.picasso.Picasso
2024-03-23 00:23:16 +01:00
import kotlinx.android.synthetic.main.activity_main.drawer_layout
import kotlinx.android.synthetic.main.activity_main.nav_view
import kotlinx.android.synthetic.main.app_bar_main.toolbar
import kotlinx.android.synthetic.main.content_main.swipeContainer
2018-08-18 07:04:31 +02:00
import kotlinx.android.synthetic.main.nav_header_main.*
import org.libre.agosto.p2play.adapters.VideosAdapter
import org.libre.agosto.p2play.ajax.Videos
import org.libre.agosto.p2play.models.VideoModel
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
private lateinit var recyclerView: RecyclerView
2019-02-09 21:56:56 +01:00
private lateinit var viewAdapter: RecyclerView.Adapter<VideosAdapter.ViewHolder>
2018-08-18 07:04:31 +02:00
private lateinit var viewManager: RecyclerView.LayoutManager
private val client: Videos = Videos()
2018-08-18 07:04:31 +02:00
private lateinit var lastItem: MenuItem
private lateinit var subItem: MenuItem
2018-08-18 07:04:31 +02:00
lateinit var myMenu: Menu
private val _db = Database(this)
2018-09-11 00:45:51 +02:00
var section: String = ""
2019-02-09 21:56:56 +01:00
var searchVal: String = ""
var pagination = 0
2018-08-18 07:04:31 +02:00
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
/* fab.setOnClickListener { view ->
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
} */
2019-02-09 21:56:56 +01:00
val toggle = ActionBarDrawerToggle(this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
2018-08-18 07:04:31 +02:00
drawer_layout.addDrawerListener(toggle)
toggle.syncState()
nav_view.setNavigationItemSelectedListener(this)
2019-01-30 19:40:36 +01:00
2018-08-18 07:04:31 +02:00
viewManager = LinearLayoutManager(this)
2019-02-09 21:56:56 +01:00
// Init RecyclerView
this.initRecycler()
2018-08-18 07:04:31 +02:00
2019-02-18 01:34:38 +01:00
this.getTrengindVideos()
swipeContainer.setOnRefreshListener {
2018-09-11 00:45:51 +02:00
this.refresh()
}
2019-01-30 19:40:36 +01:00
Handler().postDelayed({
// Title for nav_bar
side_emailTxt?.text = getString(R.string.nav_header_subtitle) + " " + this.packageManager.getPackageInfo(this.packageName, 0).versionName
}, 2000)
2018-08-18 07:04:31 +02:00
}
// Generic function for set data to RecyclerView
2019-02-09 21:56:56 +01:00
private fun initRecycler(){
val data = arrayListOf<VideoModel>()
2018-08-18 07:04:31 +02:00
viewAdapter = VideosAdapter(data)
recyclerView = findViewById<RecyclerView>(R.id.list).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
2019-02-09 21:56:56 +01:00
this.addOnScrollListener(object : RecyclerView.OnScrollListener() {
2024-03-19 01:45:04 +01:00
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
2019-02-09 21:56:56 +01:00
super.onScrolled(recyclerView, dx, dy)
2024-03-19 01:45:04 +01:00
// super.onScrolled(recyclerView!!, dx, dy)
2019-02-09 21:56:56 +01:00
if(!swipeContainer.isRefreshing){
if(!canScrollVertically(1)){
loadMore()
}
}
}
})
2018-08-18 07:04:31 +02:00
}
2024-03-23 00:23:16 +01:00
swipeContainer.isRefreshing = false
2019-02-09 21:56:56 +01:00
}
private fun addVideos(videos: ArrayList<VideoModel>){
this.swipeContainer.isRefreshing = true
try {
if(this.pagination == 0){
2019-02-10 18:41:56 +01:00
(viewAdapter as VideosAdapter).clearData()
2019-02-09 21:56:56 +01:00
recyclerView.scrollToPosition(0)
}
2019-02-12 18:45:24 +01:00
2019-02-10 18:41:56 +01:00
(viewAdapter as VideosAdapter).addData(videos)
2019-02-09 21:56:56 +01:00
}catch (err: Exception){
err.printStackTrace()
2019-02-10 18:41:56 +01:00
ManagerSingleton.Toast(getString(R.string.errorMsg), this)
2019-02-09 21:56:56 +01:00
}
this.swipeContainer.isRefreshing = false
2018-09-11 00:45:51 +02:00
}
2018-08-18 07:04:31 +02:00
private fun refresh(){
2018-09-11 00:45:51 +02:00
swipeContainer.isRefreshing = true
2019-02-09 21:56:56 +01:00
this.pagination = 0
2018-09-11 00:45:51 +02:00
when(section){
"local" -> this.getLocalVideos()
"popular" -> this.getPopularVideos()
2019-02-18 01:34:38 +01:00
"trending" -> this.getTrengindVideos()
2018-09-11 00:45:51 +02:00
"last" -> this.getLastVideos()
"sub" -> this.getSubscriptionVideos()
2019-02-09 21:56:56 +01:00
"search" -> this.searchVideos()
2018-09-11 00:45:51 +02:00
"my_videos" -> {
if(ManagerSingleton.token.token != "")
this.getMyVideos()
else
this.getLastVideos()
}
}
2018-08-18 07:04:31 +02:00
}
private fun getSubscriptionVideos(){
if(ManagerSingleton.user.status != 1){
2019-02-10 18:41:56 +01:00
ManagerSingleton.Toast("Inicia session primero", this)
startActivity(Intent(this, LoginActivity::class.java))
return
}
swipeContainer.isRefreshing = true
section = "sub"
setTitle(R.string.title_subscriptions)
AsyncTask.execute {
2019-02-09 21:56:56 +01:00
val videos = client.videoSubscriptions(ManagerSingleton.token.token, this.pagination)
runOnUiThread {
2019-02-09 21:56:56 +01:00
this.addVideos(videos)
}
}
}
// Last videos
private fun getLastVideos(){
2018-09-15 22:41:50 +02:00
swipeContainer.isRefreshing = true
2018-09-11 00:45:51 +02:00
section = "last"
2018-08-18 07:04:31 +02:00
setTitle(R.string.title_recent)
AsyncTask.execute {
2019-02-09 21:56:56 +01:00
val videos = client.getLastVideos(this.pagination)
2018-08-18 07:04:31 +02:00
runOnUiThread {
2019-02-09 21:56:56 +01:00
this.addVideos(videos)
2018-08-18 07:04:31 +02:00
}
}
}
// Popular videos
private fun getPopularVideos(){
2018-09-15 22:41:50 +02:00
swipeContainer.isRefreshing = true
2018-09-11 00:45:51 +02:00
section = "popular"
2018-08-18 07:04:31 +02:00
setTitle(R.string.title_popular)
AsyncTask.execute {
2019-02-09 21:56:56 +01:00
val videos = client.getPopularVideos(this.pagination)
2018-08-18 07:04:31 +02:00
runOnUiThread {
2019-02-09 21:56:56 +01:00
this.addVideos(videos)
2018-08-18 07:04:31 +02:00
}
}
}
2019-02-18 01:34:38 +01:00
// Trending videos
private fun getTrengindVideos(){
swipeContainer.isRefreshing = true
section = "trending"
setTitle(R.string.title_trending)
AsyncTask.execute {
val videos = client.getTrendingVideos(this.pagination)
runOnUiThread {
this.addVideos(videos)
}
}
}
// Local videos
private fun getLocalVideos(){
2018-09-15 22:41:50 +02:00
swipeContainer.isRefreshing = true
2018-09-11 00:45:51 +02:00
section = "local"
2018-08-18 07:04:31 +02:00
setTitle(R.string.title_local)
AsyncTask.execute {
2019-02-09 21:56:56 +01:00
val videos = client.getLocalVideos(this.pagination)
2018-08-18 07:04:31 +02:00
runOnUiThread {
2019-02-09 21:56:56 +01:00
this.addVideos(videos)
2018-08-18 07:04:31 +02:00
}
}
}
// Videos of user
private fun getMyVideos(){
2018-09-15 22:41:50 +02:00
swipeContainer.isRefreshing = true
2018-09-11 00:45:51 +02:00
section = "my_videos"
2018-08-18 07:04:31 +02:00
setTitle(R.string.title_myVideos)
AsyncTask.execute {
2019-02-09 21:56:56 +01:00
val videos = client.myVideos(ManagerSingleton.token.token, this.pagination)
2018-08-18 07:04:31 +02:00
runOnUiThread {
2019-02-09 21:56:56 +01:00
this.addVideos(videos)
2018-08-18 07:04:31 +02:00
}
}
}
2019-10-18 18:14:44 +02:00
// Videos history of user
private fun getHistory(){
swipeContainer.isRefreshing = true
section = "my_videos"
setTitle(R.string.nav_history)
AsyncTask.execute {
val videos = client.videoHistory(ManagerSingleton.token.token, this.pagination)
runOnUiThread {
this.addVideos(videos)
}
}
}
2019-12-13 16:53:10 +01:00
// Most liked
private fun getMostLiked(){
swipeContainer.isRefreshing = true
section = "liked"
setTitle(R.string.nav_likes)
AsyncTask.execute {
val videos = client.getMostLikedVideos(this.pagination)
runOnUiThread {
this.addVideos(videos)
}
}
}
2018-08-18 07:04:31 +02:00
override fun onBackPressed() {
if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
2024-03-23 00:23:16 +01:00
drawer_layout.closeDrawer(GravityCompat.START)
2019-12-13 16:53:10 +01:00
}
else if(!section.equals("trending")) {
2024-03-23 00:23:16 +01:00
this.getTrengindVideos()
2019-12-13 16:53:10 +01:00
}
else {
2018-08-18 07:04:31 +02:00
super.onBackPressed()
}
}
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)
2019-02-09 21:56:56 +01:00
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
2019-02-10 07:03:21 +01:00
pagination = 0
searchView.onActionViewCollapsed()
2019-02-09 21:56:56 +01:00
searchVideos()
}
return true
}
})
2018-08-18 07:04:31 +02:00
myMenu = menu
setSideData()
return true
}
override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
myMenu = menu!!
return super.onPrepareOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
when (item.itemId) {
R.id.action_settings -> {
val intent = Intent(this, SettingsActivity::class.java)
startActivity(intent)
return true
}
2018-08-18 07:04:31 +02:00
R.id.action_login -> {
val intent = Intent(this, LoginActivity::class.java)
startActivity(intent)
return true
}
R.id.action_logout -> {
logout()
return true
}
else -> return super.onOptionsItemSelected(item)
}
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
// Handle navigation view item clicks here.
// if(::lastItem.isInitialized){
// lastItem.isChecked = false
// }
lastItem = item
2019-02-12 18:45:24 +01:00
pagination = 0
2019-02-09 21:56:56 +01:00
2018-08-18 07:04:31 +02:00
// item.isChecked = true
when (item.itemId) {
2019-02-18 01:34:38 +01:00
R.id.nav_subscriptions -> getSubscriptionVideos()
R.id.nav_popular -> getPopularVideos()
R.id.nav_trending -> getTrengindVideos()
R.id.nav_recent -> getLastVideos()
R.id.nav_local -> getLocalVideos()
R.id.nav_about -> {
2018-08-27 04:36:20 +02:00
val intent = Intent(this, AboutActivity::class.java)
startActivity(intent)
}
2019-10-18 18:14:44 +02:00
R.id.nav_history -> getHistory()
R.id.nav_myVideos -> getMyVideos()
2019-12-13 16:53:10 +01:00
R.id.nav_likes -> getMostLiked()
2018-08-18 07:04:31 +02:00
}
drawer_layout.closeDrawer(GravityCompat.START)
return true
}
override fun onResume() {
super.onResume()
setSideData()
}
private fun setSideData(){
if(ManagerSingleton.user.status == 1){
2019-10-18 18:14:44 +02:00
nav_view.menu.findItem(R.id.ml).isVisible = true
2018-08-18 07:04:31 +02:00
side_usernameTxt?.text = ManagerSingleton.user.username
side_emailTxt?.text = ManagerSingleton.user.email
2019-10-18 18:14:44 +02:00
if(ManagerSingleton.user.avatar!="" && side_imageView != null) {
Picasso.get().load("https://" + ManagerSingleton.url + ManagerSingleton.user.avatar).into(side_imageView)
2018-08-18 07:04:31 +02:00
}
2024-03-23 00:23:16 +01:00
side_imageView?.setOnClickListener {
pagination = 0
getMyVideos()
drawer_layout.closeDrawer(GravityCompat.START)
}
2018-08-18 07:04:31 +02:00
if(::myMenu.isInitialized){
myMenu.findItem(R.id.action_login).isVisible = false
myMenu.findItem(R.id.action_logout).isVisible = true
2019-10-18 18:14:44 +02:00
2018-08-18 07:04:31 +02:00
}
2024-03-28 03:54:14 +01:00
} else {
nav_view.menu.findItem(R.id.ml).isVisible = false
2018-08-18 07:04:31 +02:00
}
}
private fun logout(){
2018-08-18 07:04:31 +02:00
if(::myMenu.isInitialized){
myMenu.findItem(R.id.action_login).isVisible = true
myMenu.findItem(R.id.action_logout).isVisible = false
}
2024-03-23 00:23:16 +01:00
//nav_view.menu.findItem(R.id.ml).isVisible = false
2018-08-18 07:04:31 +02:00
side_usernameTxt?.text = getString(R.string.nav_header_title)
side_emailTxt?.text = getString(R.string.nav_header_subtitle) + " " + this.packageManager.getPackageInfo(this.packageName, 0).versionName
2019-03-19 03:58:42 +01:00
side_imageView?.setImageResource(R.drawable.default_avatar)
2018-08-18 07:04:31 +02:00
side_imageView?.setOnClickListener { }
_db.logout()
ManagerSingleton.logout()
2018-09-15 22:41:50 +02:00
this.refresh()
2019-02-10 18:41:56 +01:00
ManagerSingleton.Toast(getString(R.string.logout_msg), this)
2024-03-28 03:54:14 +01:00
setSideData()
2018-08-18 07:04:31 +02:00
}
2019-02-09 21:56:56 +01:00
private fun loadMore(){
swipeContainer.isRefreshing = true
2019-02-12 18:45:24 +01:00
this.pagination += ManagerSingleton.videos_count
2019-02-09 21:56:56 +01:00
when(section){
"local" -> this.getLocalVideos()
"popular" -> this.getPopularVideos()
2019-02-18 01:34:38 +01:00
"trending" -> this.getTrengindVideos()
2019-02-09 21:56:56 +01:00
"last" -> this.getLastVideos()
"sub" -> this.getSubscriptionVideos()
"search" -> this.searchVideos()
"my_videos" -> {
if(ManagerSingleton.token.token != "")
this.getMyVideos()
else
this.getLastVideos()
}
2019-12-13 16:53:10 +01:00
"liked" -> this.getMostLiked()
2019-02-09 21:56:56 +01:00
}
}
private fun searchVideos(){
swipeContainer.isRefreshing = true
section = "search"
2019-02-10 18:41:56 +01:00
this.title = this.searchVal
2019-02-09 21:56:56 +01:00
AsyncTask.execute {
val videos = client.search(this.searchVal, this.pagination)
runOnUiThread {
this.addVideos(videos)
}
}
}
2018-08-18 07:04:31 +02:00
}