initial UI creation

This commit is contained in:
Bnyro 2022-11-17 18:16:12 +01:00
parent 6c5cb27eb8
commit 79b08c2e5a
11 changed files with 156 additions and 20 deletions

View File

@ -144,7 +144,7 @@ interface PipedApi {
): PlaylistId
@GET("user/playlists")
suspend fun playlists(@Header("Authorization") token: String): List<Playlists>
suspend fun getUserPlaylists(@Header("Authorization") token: String): List<Playlists>
@POST("user/playlists/rename")
suspend fun renamePlaylist(

View File

@ -14,3 +14,13 @@ fun Context.toastFromMainThread(stringId: Int) {
).show()
}
}
fun Context.toastFromMainThread(text: String) {
Handler(Looper.getMainLooper()).post {
Toast.makeText(
this,
text,
Toast.LENGTH_SHORT
).show()
}
}

View File

@ -50,7 +50,7 @@ class AddToPlaylistDialog : DialogFragment() {
private fun fetchPlaylists() {
lifecycleScope.launchWhenCreated {
val response = try {
RetrofitInstance.authApi.playlists(token)
RetrofitInstance.authApi.getUserPlaylists(token)
} catch (e: IOException) {
println(e)
Log.e(TAG(), "IOException, you might not have internet connection")

View File

@ -4,11 +4,20 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import com.github.libretube.R
import com.github.libretube.databinding.FragmentHomeBinding
import com.github.libretube.ui.base.BaseFragment
import com.github.libretube.ui.models.HomeModel
import com.github.libretube.util.LocaleHelper
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class HomeFragment : BaseFragment() {
private lateinit var binding: FragmentHomeBinding
private val viewModel: HomeModel by activityViewModels()
override fun onCreateView(
inflater: LayoutInflater,
@ -18,4 +27,27 @@ class HomeFragment : BaseFragment() {
binding = FragmentHomeBinding.inflate(layoutInflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.featuredTV.setOnClickListener {
findNavController().navigate(R.id.subscriptionsFragment)
}
binding.trendingTV.setOnClickListener {
findNavController().navigate(R.id.trendsFragment)
}
binding.playlistsTV.setOnClickListener {
findNavController().navigate(R.id.libraryFragment)
}
lifecycleScope.launch(Dispatchers.IO) {
viewModel.fetchHome(requireContext(), LocaleHelper.getTrendingRegion(requireContext()))
}
viewModel.trending.observe(viewLifecycleOwner) {
}
}
}

View File

@ -99,7 +99,7 @@ class LibraryFragment : BaseFragment() {
binding.playlistRefresh.isRefreshing = true
lifecycleScope.launchWhenCreated {
var playlists = try {
RetrofitInstance.authApi.playlists(token)
RetrofitInstance.authApi.getUserPlaylists(token)
} catch (e: IOException) {
println(e)
Log.e(TAG(), "IOException, you might not have internet connection")

View File

@ -10,21 +10,18 @@ import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import com.github.libretube.R
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.databinding.FragmentTrendsBinding
import com.github.libretube.extensions.TAG
import com.github.libretube.ui.activities.SettingsActivity
import com.github.libretube.ui.adapters.VideosAdapter
import com.github.libretube.ui.base.BaseFragment
import com.github.libretube.util.LocaleHelper
import com.github.libretube.util.PreferenceHelper
import com.google.android.material.snackbar.Snackbar
import retrofit2.HttpException
import java.io.IOException
class TrendsFragment : BaseFragment() {
private lateinit var binding: FragmentTrendsBinding
private lateinit var region: String
override fun onCreateView(
inflater: LayoutInflater,
@ -37,16 +34,6 @@ class TrendsFragment : BaseFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val regionPref = PreferenceHelper.getString(PreferenceKeys.REGION, "sys")
// get the system default country if auto region selected
region = if (regionPref == "sys") {
LocaleHelper
.getDetectedCountry(requireContext(), "UK")
.uppercase()
} else {
regionPref
}
fetchTrending()
binding.homeRefresh.isEnabled = true
@ -58,7 +45,9 @@ class TrendsFragment : BaseFragment() {
private fun fetchTrending() {
lifecycleScope.launchWhenCreated {
val response = try {
RetrofitInstance.api.getTrending(region)
RetrofitInstance.api.getTrending(
LocaleHelper.getTrendingRegion(requireContext())
)
} catch (e: IOException) {
println(e)
Log.e(TAG(), "IOException, you might not have internet connection")

View File

@ -0,0 +1,45 @@
package com.github.libretube.ui.models
import android.content.Context
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.obj.Playlists
import com.github.libretube.api.obj.StreamItem
import com.github.libretube.extensions.toastFromMainThread
import com.github.libretube.util.PreferenceHelper
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class HomeModel : ViewModel() {
val feed = MutableLiveData<List<StreamItem>>()
var trending = MutableLiveData<List<StreamItem>>()
val playlists = MutableLiveData<List<Playlists>>()
suspend fun fetchHome(context: Context, trendingRegion: String) {
val token = PreferenceHelper.getToken()
val appContext = context.applicationContext
runOrError(appContext) {
trending.postValue(RetrofitInstance.api.getTrending(trendingRegion))
}
runOrError(appContext) {
feed.postValue(RetrofitInstance.authApi.getFeed(token))
}
runOrError(appContext) {
playlists.postValue(RetrofitInstance.authApi.getUserPlaylists(token))
}
}
private fun runOrError(context: Context, action: suspend () -> Unit) {
CoroutineScope(Dispatchers.IO).launch {
try {
action.invoke()
} catch (e: Exception) {
e.localizedMessage?.let { context.toastFromMainThread(it) }
}
}
}
}

View File

@ -126,4 +126,16 @@ object LocaleHelper {
}
return locales
}
fun getTrendingRegion(context: Context): String {
val regionPref = PreferenceHelper.getString(PreferenceKeys.REGION, "sys")
// get the system default country if auto region selected
return if (regionPref == "sys") {
getDetectedCountry(context, "UK")
.uppercase()
} else {
regionPref
}
}
}

View File

@ -1,6 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:orientation="vertical">
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="@+id/featuredTV"
style="@style/HomeCategoryTitle"
android:text="@string/featured" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/featuredRV"
android:layout_width="wrap_content"
android:layout_height="210dp"
android:layout_marginHorizontal="10dp" />
<TextView
android:id="@+id/trendingTV"
style="@style/HomeCategoryTitle"
android:text="@string/trending" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/trendingRV"
android:layout_width="match_parent"
android:layout_height="210dp"
android:layout_marginHorizontal="10dp" />
<TextView
android:id="@+id/playlistsTV"
style="@style/HomeCategoryTitle"
android:text="@string/playlists" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/playlistsRV"
android:layout_width="match_parent"
android:layout_height="210dp"
android:layout_marginHorizontal="10dp" />
</LinearLayout>

View File

@ -379,6 +379,8 @@
<string name="limit_to_runtime">Limit to runtime</string>
<string name="open_queue_from_notification">Open queue from notification</string>
<string name="trends">Trends</string>
<string name="featured">Featured</string>
<string name="trending">What\'s trending now</string>
<!-- Notification channel strings -->
<string name="download_channel_name">Download Service</string>

View File

@ -206,4 +206,16 @@
<item name="android:drawableTint" tools:targetApi="m">?android:attr/textColorPrimary</item>
</style>
<style name="HomeCategoryTitle">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">20sp</item>
<item name="android:textColor">?attr/colorControlNormal</item>
<item name="android:padding">15dp</item>
<item name="background">?attr/selectableItemBackground</item>
<item name="android:layout_marginTop">10dp</item>
</style>
</resources>