Thorium-android-app/app/src/main/java/net/schueller/peertube/fragment/VideoMetaDataFragment.kt

230 lines
8.7 KiB
Kotlin
Raw Normal View History

2022-01-01 01:53:43 +01:00
/*
* Copyright (C) 2020 Stefan Schüller <sschueller@techdroid.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.schueller.peertube.fragment
import android.app.Activity
import android.content.Context
2022-01-08 08:48:41 +01:00
import android.content.res.ColorStateList
import android.os.Bundle
2022-01-01 01:53:43 +01:00
import android.util.Log
2022-01-08 08:48:41 +01:00
import android.view.LayoutInflater
2022-01-01 01:53:43 +01:00
import android.view.View
2022-01-08 08:48:41 +01:00
import android.view.ViewGroup
import android.widget.TextView
2022-01-01 01:53:43 +01:00
import androidx.fragment.app.Fragment
2022-01-08 08:48:41 +01:00
import androidx.fragment.app.activityViewModels
2022-01-01 01:53:43 +01:00
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.mikepenz.iconics.Iconics
2022-01-08 08:48:41 +01:00
import net.schueller.peertube.R
2022-01-01 01:53:43 +01:00
import net.schueller.peertube.adapter.MultiViewRecycleViewAdapter
2022-01-08 08:48:41 +01:00
import net.schueller.peertube.database.VideoViewModel
import net.schueller.peertube.helper.APIUrlHelper
import net.schueller.peertube.helper.ErrorHelper
2022-01-01 01:53:43 +01:00
import net.schueller.peertube.model.CommentThread
import net.schueller.peertube.model.Rating
import net.schueller.peertube.model.Video
import net.schueller.peertube.model.VideoList
import net.schueller.peertube.model.ui.VideoMetaViewItem
2022-01-08 08:48:41 +01:00
import net.schueller.peertube.network.GetVideoDataService
import net.schueller.peertube.network.RetrofitInstance
import net.schueller.peertube.service.VideoPlayerService
2022-01-01 01:53:43 +01:00
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
class VideoMetaDataFragment : Fragment() {
private var videoRating: Rating? = null
private var defaultTextColor: ColorStateList? = null
private var recyclerView: RecyclerView? = null
private var mMultiViewAdapter: MultiViewRecycleViewAdapter? = null
private lateinit var videoDescriptionFragment: VideoDescriptionFragment
2022-01-08 08:48:41 +01:00
private val mVideoViewModel: VideoViewModel by activityViewModels()
2022-01-01 01:53:43 +01:00
var isLeaveAppExpected = false
private set
override fun onCreateView(
2022-01-08 08:48:41 +01:00
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
2022-01-01 01:53:43 +01:00
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_video_meta, container, false)
}
override fun onPause() {
isLeaveAppExpected = false
super.onPause()
}
fun showDescriptionFragment(video: Video) {
// show full description fragment
videoDescriptionFragment = VideoDescriptionFragment.newInstance(video, this)
childFragmentManager.beginTransaction()
2022-01-08 08:48:41 +01:00
.add(R.id.video_meta_data_fragment, videoDescriptionFragment, VideoDescriptionFragment.TAG).commit()
2022-01-01 01:53:43 +01:00
}
fun hideDescriptionFragment() {
2022-01-08 08:48:41 +01:00
val fragment: Fragment? = childFragmentManager.findFragmentByTag(VideoDescriptionFragment.TAG)
2022-01-01 01:53:43 +01:00
if (fragment != null) {
childFragmentManager.beginTransaction().remove(fragment).commit()
}
}
fun updateVideoMeta(video: Video, mService: VideoPlayerService?) {
// Remove description if it is open as we are loading a new video
hideDescriptionFragment()
val context = context
val activity: Activity? = activity
val apiBaseURL = APIUrlHelper.getUrlWithVersion(context)
val videoDataService = RetrofitInstance.getRetrofitInstance(
2022-01-08 08:48:41 +01:00
apiBaseURL,
APIUrlHelper.useInsecureConnection(context)
2022-01-01 01:53:43 +01:00
).create(
2022-01-08 08:48:41 +01:00
GetVideoDataService::class.java
2022-01-01 01:53:43 +01:00
)
// related videos
recyclerView = activity!!.findViewById(R.id.relatedVideosView)
val layoutManager: RecyclerView.LayoutManager = LinearLayoutManager(this@VideoMetaDataFragment.context)
recyclerView?.layoutManager = layoutManager
mMultiViewAdapter = MultiViewRecycleViewAdapter(this)
recyclerView?.adapter = mMultiViewAdapter
val videoMetaViewItem = VideoMetaViewItem()
videoMetaViewItem.video = video
mMultiViewAdapter?.setVideoMeta(videoMetaViewItem)
loadVideos()
// loadComments(video.id)
// mMultiViewAdapter?.setVideoComment()
// videoOwnerSubscribeButton
// description
// video player options
val videoOptions = activity.findViewById<TextView>(R.id.exo_more)
videoOptions.setText(R.string.video_more_icon)
Iconics.Builder().on(videoOptions).build()
videoOptions.setOnClickListener {
val videoOptionsFragment = VideoOptionsFragment.newInstance(mService, video.files)
videoOptionsFragment.show(
2022-01-08 08:48:41 +01:00
getActivity()!!.supportFragmentManager,
VideoOptionsFragment.TAG
2022-01-01 01:53:43 +01:00
)
}
}
private fun loadComments(videoId: Int) {
val context = context
val start = 0
val count = 1
val sort = "-createdAt"
// We set this to default to null so that on initial start there are videos listed.
val apiBaseURL = APIUrlHelper.getUrlWithVersion(context)
val service =
2022-01-08 08:48:41 +01:00
RetrofitInstance.getRetrofitInstance(apiBaseURL, APIUrlHelper.useInsecureConnection(context)).create(
GetVideoDataService::class.java
)
2022-01-01 01:53:43 +01:00
val call: Call<CommentThread> = service.getCommentThreads(videoId, start, count, sort)
call.enqueue(object : Callback<CommentThread?> {
override fun onResponse(call: Call<CommentThread?>, response: Response<CommentThread?>) {
if (response.body() != null) {
val commentThread = response.body()
if (commentThread != null) {
2022-01-08 08:48:41 +01:00
mMultiViewAdapter!!.setVideoComment(commentThread)
2022-01-01 01:53:43 +01:00
}
}
}
override fun onFailure(call: Call<CommentThread?>, t: Throwable) {
Log.wtf("err", t.fillInStackTrace())
ErrorHelper.showToastFromCommunicationError(this@VideoMetaDataFragment.context, t)
}
})
}
private fun loadVideos() {
val context = context
val start = 0
val count = 6
val sort = "-createdAt"
val filter: String? = null
val sharedPref = context?.getSharedPreferences(
2022-01-08 08:48:41 +01:00
context.packageName + "_preferences",
Context.MODE_PRIVATE
2022-01-01 01:53:43 +01:00
)
var nsfw = "false"
var languages: Set<String>? = emptySet()
if (sharedPref != null) {
nsfw = if (sharedPref.getBoolean(getString(R.string.pref_show_nsfw_key), false)) "both" else "false"
languages = sharedPref.getStringSet(getString(R.string.pref_video_language_key), null)
}
// We set this to default to null so that on initial start there are videos listed.
val apiBaseURL = APIUrlHelper.getUrlWithVersion(context)
val service =
2022-01-08 08:48:41 +01:00
RetrofitInstance.getRetrofitInstance(apiBaseURL, APIUrlHelper.useInsecureConnection(context)).create(
GetVideoDataService::class.java
)
2022-01-01 01:53:43 +01:00
val call: Call<VideoList> = service.getVideosData(start, count, sort, nsfw, filter, languages)
/*Log the URL called*/Log.d("URL Called", call.request().url.toString() + "")
// Toast.makeText(VideoListActivity.this, "URL Called: " + call.request().url(), Toast.LENGTH_SHORT).show();
call.enqueue(object : Callback<VideoList?> {
override fun onResponse(call: Call<VideoList?>, response: Response<VideoList?>) {
if (response.body() != null) {
val videoList = response.body()
if (videoList != null) {
mMultiViewAdapter!!.setVideoListData(videoList)
}
}
}
override fun onFailure(call: Call<VideoList?>, t: Throwable) {
Log.wtf("err", t.fillInStackTrace())
ErrorHelper.showToastFromCommunicationError(this@VideoMetaDataFragment.context, t)
}
})
}
2022-01-08 08:48:41 +01:00
fun saveToPlaylist(video: Video) {
val playlistVideo: net.schueller.peertube.database.Video = net.schueller.peertube.database.Video(videoUUID = video.uuid, videoName = video.name, videoDescription = video.description)
2022-01-08 08:48:41 +01:00
mVideoViewModel.insert(playlistVideo)
}
2022-01-01 01:53:43 +01:00
companion object {
const val TAG = "VMDF"
}
}