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

471 lines
17 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
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-04-11 05:15:06 +02:00
import android.view.View
2024-05-26 23:47:25 +02:00
import android.widget.ImageView
import android.widget.TextView
2024-03-23 00:23:16 +01:00
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.core.view.GravityCompat
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
import org.libre.agosto.p2play.adapters.VideosAdapter
import org.libre.agosto.p2play.ajax.Videos
2024-05-26 23:47:25 +02:00
import org.libre.agosto.p2play.databinding.ActivityMainBinding
2024-05-15 02:39:45 +02:00
import org.libre.agosto.p2play.helpers.getViewManager
2018-08-18 07:04:31 +02:00
import org.libre.agosto.p2play.models.VideoModel
2024-04-11 05:15:06 +02:00
import org.libre.agosto.p2play.singletons.PlaybackSingleton
2018-08-18 07:04:31 +02:00
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
lateinit var myMenu: Menu
2024-04-07 02:03:23 +02:00
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
2024-05-26 23:47:25 +02:00
private lateinit var binding: ActivityMainBinding
2018-08-18 07:04:31 +02:00
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
2024-05-26 23:47:25 +02:00
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
setSupportActionBar(binding.toolbar)
2018-08-18 07:04:31 +02:00
2024-05-26 23:47:25 +02:00
val toggle = ActionBarDrawerToggle(this, binding.drawerLayout, binding.toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
binding.drawerLayout.addDrawerListener(toggle)
2018-08-18 07:04:31 +02:00
toggle.syncState()
2024-05-26 23:47:25 +02:00
binding.navView.setNavigationItemSelectedListener(this)
2019-01-30 19:40:36 +01:00
2024-05-15 02:39:45 +02:00
viewManager = getViewManager(this, resources)
2018-08-18 07:04:31 +02:00
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()
2024-05-26 23:47:25 +02:00
binding.content.swipeContainer.setOnRefreshListener {
2018-09-11 00:45:51 +02:00
this.refresh()
}
2019-01-30 19:40:36 +01:00
2024-05-26 23:47:25 +02:00
binding.content.mini.miniPlayerImage.setOnClickListener { this.resumeVideo() }
binding.content.mini.miniPlayerTitle.setOnClickListener { this.resumeVideo() }
binding.content.mini.miniPlayerAuthor.setOnClickListener { this.resumeVideo() }
// binding.content.mini.setOnClickListener { this.resumeVideo() }
binding.content.mini.miniPlayPause.setOnClickListener { this.playPausePlayer() }
2024-04-11 05:15:06 +02:00
2019-01-30 19:40:36 +01:00
Handler().postDelayed({
// Title for nav_bar
2024-05-26 23:47:25 +02:00
binding.navView.getHeaderView(0).findViewById<TextView>(R.id.side_emailTxt).text = getString(R.string.nav_header_subtitle) + " " + this.packageManager.getPackageInfo(this.packageName, 0).versionName
2019-01-30 19:40:36 +01:00
}, 2000)
2018-08-18 07:04:31 +02:00
}
// Generic function for set data to RecyclerView
2024-04-06 22:38:04 +02:00
private fun initRecycler() {
2019-02-09 21:56:56 +01:00
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
2024-05-26 23:47:25 +02:00
if (!binding.content.swipeContainer.isRefreshing) {
2024-04-06 22:38:04 +02:00
if (!canScrollVertically(1)) {
2019-02-09 21:56:56 +01:00
loadMore()
}
}
}
})
2018-08-18 07:04:31 +02:00
}
2024-05-26 23:47:25 +02:00
binding.content.swipeContainer.isRefreshing = false
2019-02-09 21:56:56 +01:00
}
2024-04-06 22:38:04 +02:00
private fun addVideos(videos: ArrayList<VideoModel>) {
2024-05-26 23:47:25 +02:00
binding.content.swipeContainer.isRefreshing = true
2019-02-09 21:56:56 +01:00
try {
2024-04-06 22:38:04 +02:00
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)
2024-04-06 22:38:04 +02:00
} catch (err: Exception) {
2019-02-09 21:56:56 +01:00
err.printStackTrace()
2024-04-07 02:03:23 +02:00
ManagerSingleton.toast(getString(R.string.errorMsg), this)
2019-02-09 21:56:56 +01:00
}
2024-05-26 23:47:25 +02:00
binding.content.swipeContainer.isRefreshing = false
2018-09-11 00:45:51 +02:00
}
2018-08-18 07:04:31 +02:00
2024-04-06 22:38:04 +02:00
private fun refresh() {
2024-05-26 23:47:25 +02:00
binding.content.swipeContainer.isRefreshing = true
2019-02-09 21:56:56 +01:00
this.pagination = 0
2024-04-06 22:38:04 +02:00
when (section) {
2018-09-11 00:45:51 +02:00
"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" -> {
2024-04-06 22:38:04 +02:00
if (ManagerSingleton.token.token != "") {
2018-09-11 00:45:51 +02:00
this.getMyVideos()
2024-04-06 22:38:04 +02:00
} else {
2018-09-11 00:45:51 +02:00
this.getLastVideos()
2024-04-06 22:38:04 +02:00
}
2018-09-11 00:45:51 +02:00
}
}
2018-08-18 07:04:31 +02:00
}
2024-04-06 22:38:04 +02:00
private fun getSubscriptionVideos() {
if (ManagerSingleton.user.status != 1) {
2024-04-07 02:03:23 +02:00
ManagerSingleton.toast("Inicia session primero", this)
startActivity(Intent(this, LoginActivity::class.java))
return
}
2024-05-26 23:47:25 +02:00
binding.content.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
2024-04-06 22:38:04 +02:00
private fun getLastVideos() {
2024-05-26 23:47:25 +02:00
binding.content.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
2024-04-06 22:38:04 +02:00
private fun getPopularVideos() {
2024-05-26 23:47:25 +02:00
binding.content.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
2024-04-06 22:38:04 +02:00
private fun getTrengindVideos() {
2024-05-26 23:47:25 +02:00
binding.content.swipeContainer.isRefreshing = true
2019-02-18 01:34:38 +01:00
section = "trending"
setTitle(R.string.title_trending)
AsyncTask.execute {
val videos = client.getTrendingVideos(this.pagination)
runOnUiThread {
this.addVideos(videos)
}
}
}
// Local videos
2024-04-06 22:38:04 +02:00
private fun getLocalVideos() {
2024-05-26 23:47:25 +02:00
binding.content.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
2024-04-06 22:38:04 +02:00
private fun getMyVideos() {
2024-05-26 23:47:25 +02:00
binding.content.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
2024-04-06 22:38:04 +02:00
private fun getHistory() {
2024-05-26 23:47:25 +02:00
binding.content.swipeContainer.isRefreshing = true
2019-10-18 18:14:44 +02:00
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
2024-04-06 22:38:04 +02:00
private fun getMostLiked() {
2024-05-26 23:47:25 +02:00
binding.content.swipeContainer.isRefreshing = true
2019-12-13 16:53:10 +01:00
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() {
2024-05-26 23:47:25 +02:00
if (binding.drawerLayout.isDrawerOpen(GravityCompat.START)) {
binding.drawerLayout.closeDrawer(GravityCompat.START)
2024-04-06 22:38:04 +02:00
} else if (!section.equals("trending")) {
2024-04-06 02:47:08 +02:00
// Hot fix
pagination = 0
this.getTrengindVideos()
2024-04-06 22:38:04 +02: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
2024-04-06 22:38:04 +02:00
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
2019-02-09 21:56:56 +01:00
override fun onQueryTextChange(p0: String?): Boolean {
return true
}
override fun onQueryTextSubmit(p0: String?): Boolean {
2024-04-06 22:38:04 +02:00
if (!p0.isNullOrBlank()) {
2019-02-09 21:56:56 +01:00
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 -> {
2024-04-01 00:59:57 +02:00
val intent = Intent(this, SettingsActivity2::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){
2024-04-06 22:38:04 +02:00
// lastItem.isChecked = false
2018-08-18 07:04:31 +02:00
// }
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
}
2024-05-26 23:47:25 +02:00
binding.drawerLayout.closeDrawer(GravityCompat.START)
2018-08-18 07:04:31 +02:00
return true
}
override fun onResume() {
super.onResume()
setSideData()
2024-04-11 05:15:06 +02:00
if (PlaybackSingleton.player != null && PlaybackSingleton.player!!.isPlaying) {
PlaybackSingleton.runMediaSession(this)
2024-05-26 23:47:25 +02:00
binding.content.mini.miniPlayerTitle.text = PlaybackSingleton.video!!.name
binding.content.mini.miniPlayerAuthor.text = PlaybackSingleton.video!!.username
Picasso.get().load("https://${ManagerSingleton.url}${PlaybackSingleton.video!!.thumbUrl}").into(binding.content.mini.miniPlayerImage)
binding.content.mini.miniPlayPause.setImageResource(R.drawable.ic_pause_24)
binding.content.mini.miniPlayer.visibility = View.VISIBLE
2024-04-11 05:15:06 +02:00
} else {
2024-05-26 23:47:25 +02:00
binding.content.mini.miniPlayer.visibility = View.GONE
2024-04-11 05:15:06 +02:00
}
}
override fun onDestroy() {
if (PlaybackSingleton.player != null) {
PlaybackSingleton.release()
}
super.onDestroy()
2018-08-18 07:04:31 +02:00
}
2024-04-06 22:38:04 +02:00
private fun setSideData() {
if (ManagerSingleton.user.status == 1) {
2024-05-26 23:47:25 +02:00
binding.navView.menu.findItem(R.id.ml).isVisible = true
2019-10-18 18:14:44 +02:00
2024-05-26 23:47:25 +02:00
val headerView = binding.navView.getHeaderView(0)
headerView.findViewById<TextView>(R.id.side_usernameTxt).text = ManagerSingleton.user.username
headerView.findViewById<TextView>(R.id.side_emailTxt).text = ManagerSingleton.user.email
if (ManagerSingleton.user.avatar != "" && headerView.findViewById<ImageView>(R.id.side_imageView) != null) {
Picasso.get().load("https://" + ManagerSingleton.url + ManagerSingleton.user.avatar).into(headerView.findViewById<ImageView>(R.id.side_imageView))
2018-08-18 07:04:31 +02:00
}
2024-05-26 23:47:25 +02:00
headerView.findViewById<ImageView>(R.id.side_imageView).setOnClickListener {
2024-04-06 22:38:04 +02:00
pagination = 0
2024-03-23 00:23:16 +01:00
getMyVideos()
2024-05-26 23:47:25 +02:00
binding.drawerLayout.closeDrawer(GravityCompat.START)
2024-04-06 22:38:04 +02:00
}
if (::myMenu.isInitialized) {
2018-08-18 07:04:31 +02:00
myMenu.findItem(R.id.action_login).isVisible = false
myMenu.findItem(R.id.action_logout).isVisible = true
}
2024-03-28 03:54:14 +01:00
} else {
2024-05-26 23:47:25 +02:00
binding.navView.menu.findItem(R.id.ml).isVisible = false
2018-08-18 07:04:31 +02:00
}
}
2024-04-06 22:38:04 +02:00
private fun logout() {
if (::myMenu.isInitialized) {
2018-08-18 07:04:31 +02:00
myMenu.findItem(R.id.action_login).isVisible = true
myMenu.findItem(R.id.action_logout).isVisible = false
}
2024-05-26 23:47:25 +02:00
val headerView = binding.navView.getHeaderView(0)
binding.navView.menu.findItem(R.id.ml).isVisible = false
headerView.findViewById<TextView>(R.id.side_usernameTxt).text = getString(R.string.nav_header_title)
headerView.findViewById<TextView>(R.id.side_emailTxt).text = getString(R.string.nav_header_subtitle) + " " + this.packageManager.getPackageInfo(this.packageName, 0).versionName
headerView.findViewById<ImageView>(R.id.side_imageView).setImageResource(R.drawable.default_avatar)
headerView.findViewById<ImageView>(R.id.side_imageView).setOnClickListener { }
2024-04-07 02:03:23 +02:00
db.logout()
2018-08-18 07:04:31 +02:00
ManagerSingleton.logout()
2018-09-15 22:41:50 +02:00
this.refresh()
2024-04-07 02:03:23 +02: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
}
2024-04-06 22:38:04 +02:00
private fun loadMore() {
2024-05-26 23:47:25 +02:00
binding.content.swipeContainer.isRefreshing = true
2024-04-07 02:03:23 +02:00
this.pagination += ManagerSingleton.videosCount
2019-02-09 21:56:56 +01:00
2024-04-06 22:38:04 +02:00
when (section) {
2019-02-09 21:56:56 +01:00
"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" -> {
2024-04-06 22:38:04 +02:00
if (ManagerSingleton.token.token != "") {
2019-02-09 21:56:56 +01:00
this.getMyVideos()
2024-04-06 22:38:04 +02:00
} else {
2019-02-09 21:56:56 +01:00
this.getLastVideos()
2024-04-06 22:38:04 +02:00
}
2019-02-09 21:56:56 +01:00
}
2019-12-13 16:53:10 +01:00
"liked" -> this.getMostLiked()
2019-02-09 21:56:56 +01:00
}
}
2024-04-06 22:38:04 +02:00
private fun searchVideos() {
2024-05-26 23:47:25 +02:00
binding.content.swipeContainer.isRefreshing = true
2019-02-09 21:56:56 +01:00
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)
}
}
}
2024-04-11 05:15:06 +02:00
2024-04-16 06:07:14 +02:00
private fun resumeVideo() {
2024-04-11 05:15:06 +02:00
val intent = Intent(this, ReproductorActivity::class.java)
intent.putExtra("resume", true)
startActivity(intent)
}
2024-04-16 06:07:14 +02:00
private fun playPausePlayer() {
2024-04-11 05:15:06 +02:00
PlaybackSingleton.player?.let {
if (it.isPlaying) {
it.pause()
2024-05-26 23:47:25 +02:00
binding.content.mini.miniPlayPause.setImageResource(R.drawable.ic_play_arrow_24)
2024-04-11 05:15:06 +02:00
} else {
it.play()
2024-05-26 23:47:25 +02:00
binding.content.mini.miniPlayPause.setImageResource(R.drawable.ic_pause_24)
2024-04-11 05:15:06 +02:00
}
}
}
2018-08-18 07:04:31 +02:00
}