LibreTube/app/src/main/java/com/github/libretube/Home.kt

101 lines
3.7 KiB
Kotlin
Raw Normal View History

2022-02-01 21:22:06 +05:30
package com.github.libretube
2021-12-09 18:25:32 +05:30
import android.os.Bundle
2021-12-18 16:34:14 +05:30
import android.util.Log
2021-12-09 18:25:32 +05:30
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ProgressBar
2022-03-18 20:43:07 +05:30
import android.widget.Toast
2022-05-20 03:52:10 +05:30
import androidx.fragment.app.Fragment
2021-12-18 16:34:14 +05:30
import androidx.lifecycle.lifecycleScope
2022-02-08 19:57:13 +05:30
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
2022-03-18 20:43:07 +05:30
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
2022-02-01 21:22:06 +05:30
import com.github.libretube.adapters.TrendingAdapter
2022-06-03 00:40:16 +05:30
import com.github.libretube.util.RetrofitInstance
import java.io.IOException
2022-05-20 03:52:10 +05:30
import retrofit2.HttpException
2021-12-09 18:25:32 +05:30
class Home : Fragment() {
2022-02-05 21:20:16 +05:30
2022-02-05 00:25:05 +05:30
private val TAG = "HomeFragment"
2022-03-18 20:43:07 +05:30
private var refreshLayout: SwipeRefreshLayout? = null
2021-12-09 18:25:32 +05:30
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
}
}
override fun onCreateView(
2022-05-20 03:52:10 +05:30
inflater: LayoutInflater,
container: ViewGroup?,
2021-12-09 18:25:32 +05:30
savedInstanceState: Bundle?
): View? {
2021-12-09 18:25:32 +05:30
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
2022-05-20 03:52:10 +05:30
val recyclerView = view.findViewById<RecyclerView>(R.id.recview)
2022-03-29 22:32:34 +05:30
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
2022-05-21 13:32:04 +05:30
val grid = sharedPreferences.getString(
"grid",
resources.getInteger(R.integer.grid_items).toString()
)!!
2022-03-29 22:32:34 +05:30
recyclerView.layoutManager = GridLayoutManager(view.context, grid.toInt())
val progressbar = view.findViewById<ProgressBar>(R.id.progressBar)
2022-05-20 03:52:10 +05:30
fetchJson(progressbar, recyclerView)
2022-03-18 20:43:07 +05:30
refreshLayout = view.findViewById(R.id.home_refresh)
refreshLayout?.isEnabled = true
refreshLayout?.setOnRefreshListener {
2022-05-20 03:52:10 +05:30
Log.d(TAG, "hmm")
fetchJson(progressbar, recyclerView)
2022-03-18 20:43:07 +05:30
}
2021-12-09 18:25:32 +05:30
}
2022-05-20 03:52:10 +05:30
private fun fetchJson(progressBar: ProgressBar, recyclerView: RecyclerView) {
fun run() {
2021-12-18 16:34:14 +05:30
lifecycleScope.launchWhenCreated {
val response = try {
2022-05-21 13:32:04 +05:30
val sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(requireContext())
2022-02-08 19:57:13 +05:30
RetrofitInstance.api.getTrending(sharedPreferences.getString("region", "US")!!)
2022-05-20 03:52:10 +05:30
} catch (e: IOException) {
2021-12-18 16:34:14 +05:30
println(e)
Log.e(TAG, "IOException, you might not have internet connection")
2022-05-20 03:52:10 +05:30
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
2021-12-18 16:34:14 +05:30
return@launchWhenCreated
} catch (e: HttpException) {
Log.e(TAG, "HttpException, unexpected response")
2022-05-20 03:52:10 +05:30
Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show()
2021-12-18 16:34:14 +05:30
return@launchWhenCreated
2022-05-20 03:52:10 +05:30
} finally {
2022-03-18 20:43:07 +05:30
refreshLayout?.isRefreshing = false
2021-12-18 16:34:14 +05:30
}
runOnUiThread {
progressBar.visibility = View.GONE
recyclerView.adapter = TrendingAdapter(response, childFragmentManager)
2021-12-18 16:34:14 +05:30
}
}
}
2022-05-20 03:52:10 +05:30
run()
}
2022-05-21 13:32:04 +05:30
2022-01-20 17:28:59 +05:30
private fun Fragment?.runOnUiThread(action: () -> Unit) {
this ?: return
if (!isAdded) return // Fragment not attached to an Activity
activity?.runOnUiThread(action)
}
2022-02-05 21:10:29 +05:30
override fun onDestroyView() {
2022-05-20 03:52:10 +05:30
view?.findViewById<RecyclerView>(R.id.recview)?.adapter = null
2022-03-18 20:43:07 +05:30
refreshLayout = null
2022-05-20 03:52:10 +05:30
Log.e(TAG, "destroyview")
2022-02-05 21:10:29 +05:30
super.onDestroyView()
}
}