LibreTube/app/src/main/java/com/github/libretube/fragments/HomeFragment.kt

87 lines
3.1 KiB
Kotlin
Raw Normal View History

2022-06-08 14:37:47 +05:30
package com.github.libretube.fragments
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
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
import androidx.recyclerview.widget.GridLayoutManager
2022-06-08 14:37:47 +05:30
import com.github.libretube.R
2022-02-01 21:22:06 +05:30
import com.github.libretube.adapters.TrendingAdapter
2022-07-01 14:41:24 +05:30
import com.github.libretube.databinding.FragmentHomeBinding
2022-07-02 21:53:24 +05:30
import com.github.libretube.preferences.PreferenceHelper
2022-06-03 00:40:16 +05:30
import com.github.libretube.util.RetrofitInstance
2022-05-20 03:52:10 +05:30
import retrofit2.HttpException
2022-06-24 20:56:36 +05:30
import java.io.IOException
2021-12-09 18:25:32 +05:30
2022-07-01 14:41:24 +05:30
class HomeFragment : Fragment() {
2022-02-05 00:25:05 +05:30
private val TAG = "HomeFragment"
2022-07-01 14:41:24 +05:30
private lateinit var binding: FragmentHomeBinding
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?
2022-07-01 14:41:24 +05:30
): View {
binding = FragmentHomeBinding.inflate(layoutInflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
2022-06-26 01:51:22 +05:30
val grid = PreferenceHelper.getString(
requireContext(),
2022-05-21 13:32:04 +05:30
"grid",
resources.getInteger(R.integer.grid_items).toString()
)!!
2022-07-01 14:41:24 +05:30
binding.recview.layoutManager = GridLayoutManager(view.context, grid.toInt())
2022-07-02 20:31:24 +05:30
fetchJson()
2022-07-01 14:41:24 +05:30
binding.homeRefresh.isEnabled = true
binding.homeRefresh.setOnRefreshListener {
2022-07-02 20:31:24 +05:30
fetchJson()
2022-03-18 20:43:07 +05:30
}
2021-12-09 18:25:32 +05:30
}
2022-07-02 20:31:24 +05:30
private fun fetchJson() {
fun run() {
2021-12-18 16:34:14 +05:30
lifecycleScope.launchWhenCreated {
val response = try {
2022-06-26 01:51:22 +05:30
val region = PreferenceHelper.getString(requireContext(), "region", "US")
RetrofitInstance.api.getTrending(region!!)
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-07-01 14:41:24 +05:30
binding.homeRefresh.isRefreshing = false
2021-12-18 16:34:14 +05:30
}
runOnUiThread {
2022-07-02 20:31:24 +05:30
binding.progressBar.visibility = View.GONE
binding.recview.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)
}
}