add a new empty home fragment

This commit is contained in:
Bnyro 2022-11-17 17:23:10 +01:00
parent 6e3965c51e
commit 6c5cb27eb8
9 changed files with 167 additions and 123 deletions

View File

@ -314,6 +314,8 @@ class MainActivity : BaseActivity() {
when (intent?.getStringExtra("fragmentToOpen")) { when (intent?.getStringExtra("fragmentToOpen")) {
"home" -> "home" ->
navController.navigate(R.id.homeFragment) navController.navigate(R.id.homeFragment)
"trends" ->
navController.navigate(R.id.trendsFragment)
"subscriptions" -> "subscriptions" ->
navController.navigate(R.id.subscriptionsFragment) navController.navigate(R.id.subscriptionsFragment)
"library" -> "library" ->

View File

@ -1,30 +1,14 @@
package com.github.libretube.ui.fragments package com.github.libretube.ui.fragments
import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
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.FragmentHomeBinding import com.github.libretube.databinding.FragmentHomeBinding
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.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 HomeFragment : BaseFragment() { class HomeFragment : BaseFragment() {
private lateinit var binding: FragmentHomeBinding private lateinit var binding: FragmentHomeBinding
private lateinit var region: String
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, inflater: LayoutInflater,
@ -34,74 +18,4 @@ class HomeFragment : BaseFragment() {
binding = FragmentHomeBinding.inflate(layoutInflater, container, false) binding = FragmentHomeBinding.inflate(layoutInflater, container, false)
return binding.root return binding.root
} }
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
binding.homeRefresh.setOnRefreshListener {
fetchTrending()
}
}
private fun fetchTrending() {
lifecycleScope.launchWhenCreated {
val response = try {
RetrofitInstance.api.getTrending(region)
} catch (e: IOException) {
println(e)
Log.e(TAG(), "IOException, you might not have internet connection")
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
return@launchWhenCreated
} catch (e: HttpException) {
Log.e(TAG(), "HttpException, unexpected response")
Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show()
return@launchWhenCreated
} finally {
binding.homeRefresh.isRefreshing = false
}
runOnUiThread {
binding.progressBar.visibility = View.GONE
// show a [SnackBar] if there are no trending videos available
if (response.isEmpty()) {
Snackbar.make(
binding.root,
R.string.change_region,
Snackbar.LENGTH_LONG
)
.setAction(
R.string.settings
) {
startActivity(
Intent(
context,
SettingsActivity::class.java
)
)
}
.show()
return@runOnUiThread
}
binding.recview.adapter = VideosAdapter(
response.toMutableList(),
childFragmentManager
)
binding.recview.layoutManager = VideosAdapter.getLayout(requireContext())
}
}
}
} }

View File

@ -0,0 +1,107 @@
package com.github.libretube.ui.fragments
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
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,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentTrendsBinding.inflate(layoutInflater, container, false)
return binding.root
}
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
binding.homeRefresh.setOnRefreshListener {
fetchTrending()
}
}
private fun fetchTrending() {
lifecycleScope.launchWhenCreated {
val response = try {
RetrofitInstance.api.getTrending(region)
} catch (e: IOException) {
println(e)
Log.e(TAG(), "IOException, you might not have internet connection")
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
return@launchWhenCreated
} catch (e: HttpException) {
Log.e(TAG(), "HttpException, unexpected response")
Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show()
return@launchWhenCreated
} finally {
binding.homeRefresh.isRefreshing = false
}
runOnUiThread {
binding.progressBar.visibility = View.GONE
// show a [SnackBar] if there are no trending videos available
if (response.isEmpty()) {
Snackbar.make(
binding.root,
R.string.change_region,
Snackbar.LENGTH_LONG
)
.setAction(
R.string.settings
) {
startActivity(
Intent(
context,
SettingsActivity::class.java
)
)
}
.show()
return@runOnUiThread
}
binding.recview.adapter = VideosAdapter(
response.toMutableList(),
childFragmentManager
)
binding.recview.layoutManager = VideosAdapter.getLayout(requireContext())
}
}
}
}

View File

@ -1,35 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent">
tools:context=".ui.fragments.HomeFragment">
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/home_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recview"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/progressBar"
app:layout_constraintTop_toTopOf="parent" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.fragments.TrendsFragment">
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/home_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recview"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/progressBar"
app:layout_constraintTop_toTopOf="parent" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -6,6 +6,12 @@
android:icon="@drawable/ic_home" android:icon="@drawable/ic_home"
android:title="@string/startpage" /> android:title="@string/startpage" />
<item
android:visible="false"
android:id="@+id/trendsFragment"
android:icon="@drawable/ic_trending"
android:title="@string/trends" />
<item <item
android:id="@+id/subscriptionsFragment" android:id="@+id/subscriptionsFragment"
android:icon="@drawable/ic_subscriptions" android:icon="@drawable/ic_subscriptions"
@ -22,10 +28,4 @@
android:icon="@drawable/ic_download_filled" android:icon="@drawable/ic_download_filled"
android:title="@string/downloads" /> android:title="@string/downloads" />
<item
android:id="@+id/watchHistoryFragment"
android:visible="false"
android:icon="@drawable/ic_history_filled"
android:title="@string/history" />
</menu> </menu>

View File

@ -4,12 +4,16 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav" android:id="@+id/nav"
app:startDestination="@id/homeFragment"> app:startDestination="@id/homeFragment">
<fragment <fragment
android:id="@+id/homeFragment" android:id="@+id/homeFragment"
android:name="com.github.libretube.ui.fragments.HomeFragment" android:name="com.github.libretube.ui.fragments.HomeFragment"
android:label="fragment_home" android:label="fragment_home"
tools:layout="@layout/fragment_home" /> tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/trendsFragment"
android:name="com.github.libretube.ui.fragments.TrendsFragment"
android:label="fragment_trends"
tools:layout="@layout/fragment_trends" />
<fragment <fragment
android:id="@+id/subscriptionsFragment" android:id="@+id/subscriptionsFragment"
android:name="com.github.libretube.ui.fragments.SubscriptionsFragment" android:name="com.github.libretube.ui.fragments.SubscriptionsFragment"

View File

@ -378,6 +378,7 @@
<string name="auto_quality">Auto</string> <string name="auto_quality">Auto</string>
<string name="limit_to_runtime">Limit to runtime</string> <string name="limit_to_runtime">Limit to runtime</string>
<string name="open_queue_from_notification">Open queue from notification</string> <string name="open_queue_from_notification">Open queue from notification</string>
<string name="trends">Trends</string>
<!-- Notification channel strings --> <!-- Notification channel strings -->
<string name="download_channel_name">Download Service</string> <string name="download_channel_name">Download Service</string>

View File

@ -12,6 +12,16 @@
android:targetPackage="com.github.libretube" android:targetPackage="com.github.libretube"
android:targetClass="com.github.libretube.ui.activities.MainActivity" /> android:targetClass="com.github.libretube.ui.activities.MainActivity" />
</shortcut> </shortcut>
<shortcut
android:shortcutId="trends"
android:enabled="true"
android:icon="@drawable/ic_trending"
android:shortcutShortLabel="@string/trends">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="com.github.libretube"
android:targetClass="com.github.libretube.ui.activities.MainActivity" />
</shortcut>
<shortcut <shortcut
android:shortcutId="subscriptions" android:shortcutId="subscriptions"
android:enabled="true" android:enabled="true"