Fix crashes in SearchResultFragment

This commit is contained in:
Bnyro 2023-03-28 18:49:08 +02:00
parent b3c6a74968
commit 648949e360
5 changed files with 46 additions and 19 deletions

View File

@ -122,9 +122,6 @@ dependencies {
/* Room */ /* Room */
kapt libs.room.compiler kapt libs.room.compiler
implementation libs.room implementation libs.room
/* View binding */
implementation libs.viewBindingPropertyDelegate
} }
static def getUnixTime() { static def getUnixTime() {

View File

@ -7,7 +7,6 @@ import android.content.Intent
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.os.Handler import android.os.Handler
import android.os.Looper import android.os.Looper
import androidx.appcompat.app.AppCompatActivity
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
import androidx.core.os.postDelayed import androidx.core.os.postDelayed
import androidx.fragment.app.commitNow import androidx.fragment.app.commitNow
@ -31,7 +30,7 @@ object NavigationHelper {
) { ) {
if (channelId == null) return if (channelId == null) return
val activity = unwrap(context) val activity = unwrapActivity(context)
val bundle = bundleOf(IntentData.channelId to channelId) val bundle = bundleOf(IntentData.channelId to channelId)
activity.navController.navigate(R.id.channelFragment, bundle) activity.navController.navigate(R.id.channelFragment, bundle)
try { try {
@ -45,7 +44,7 @@ object NavigationHelper {
} }
} }
private fun unwrap(context: Context): MainActivity { private fun unwrapActivity(context: Context): MainActivity {
var correctContext: Context? = context var correctContext: Context? = context
while (correctContext !is MainActivity && correctContext is ContextWrapper) { while (correctContext !is MainActivity && correctContext is ContextWrapper) {
correctContext = correctContext.baseContext correctContext = correctContext.baseContext
@ -92,7 +91,7 @@ object NavigationHelper {
IntentData.timeStamp to timeStamp IntentData.timeStamp to timeStamp
) )
val activity = context as AppCompatActivity val activity = unwrapActivity(context)
activity.supportFragmentManager.commitNow { activity.supportFragmentManager.commitNow {
replace<PlayerFragment>(R.id.container, args = bundle) replace<PlayerFragment>(R.id.container, args = bundle)
} }
@ -105,7 +104,7 @@ object NavigationHelper {
) { ) {
if (playlistId == null) return if (playlistId == null) return
val activity = unwrap(context) val activity = unwrapActivity(context)
val bundle = bundleOf( val bundle = bundleOf(
IntentData.playlistId to playlistId, IntentData.playlistId to playlistId,
IntentData.playlistType to playlistType IntentData.playlistType to playlistType
@ -117,7 +116,7 @@ object NavigationHelper {
* Start the audio player fragment * Start the audio player fragment
*/ */
fun startAudioPlayer(context: Context) { fun startAudioPlayer(context: Context) {
val activity = unwrap(context) val activity = unwrapActivity(context)
activity.supportFragmentManager.commitNow { activity.supportFragmentManager.commitNow {
replace<AudioPlayerFragment>(R.id.container) replace<AudioPlayerFragment>(R.id.container)
} }

View File

@ -2,13 +2,13 @@ package com.github.libretube.ui.fragments
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import by.kirich1409.viewbindingdelegate.viewBinding
import com.github.libretube.R
import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.RetrofitInstance
import com.github.libretube.databinding.FragmentSearchBinding import com.github.libretube.databinding.FragmentSearchBinding
import com.github.libretube.db.DatabaseHolder.Database import com.github.libretube.db.DatabaseHolder.Database
@ -21,8 +21,9 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
class SearchFragment : Fragment(R.layout.fragment_search) { class SearchFragment : Fragment() {
private val binding by viewBinding(FragmentSearchBinding::bind) private var _binding: FragmentSearchBinding? = null
private val binding get() = _binding!!
private val viewModel: SearchViewModel by activityViewModels() private val viewModel: SearchViewModel by activityViewModels()
@ -33,6 +34,15 @@ class SearchFragment : Fragment(R.layout.fragment_search) {
query = arguments?.getString("query") query = arguments?.getString("query")
} }
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentSearchBinding.inflate(layoutInflater)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
@ -93,4 +103,10 @@ class SearchFragment : Fragment(R.layout.fragment_search) {
} }
} }
} }
override fun onDestroy() {
super.onDestroy()
_binding = null
}
} }

View File

@ -2,12 +2,13 @@ package com.github.libretube.ui.fragments
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import by.kirich1409.viewbindingdelegate.viewBinding
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.RetrofitInstance
import com.github.libretube.constants.PreferenceKeys import com.github.libretube.constants.PreferenceKeys
@ -24,8 +25,9 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import retrofit2.HttpException import retrofit2.HttpException
class SearchResultFragment : Fragment(R.layout.fragment_search_result) { class SearchResultFragment : Fragment() {
private val binding by viewBinding(FragmentSearchResultBinding::bind) private var _binding: FragmentSearchResultBinding? = null
private val binding get() = _binding!!
private var nextPage: String? = null private var nextPage: String? = null
private var query: String = "" private var query: String = ""
@ -38,6 +40,15 @@ class SearchResultFragment : Fragment(R.layout.fragment_search_result) {
query = arguments?.getString("query").toString() query = arguments?.getString("query").toString()
} }
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentSearchResultBinding.inflate(layoutInflater)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
@ -132,4 +143,10 @@ class SearchResultFragment : Fragment(R.layout.fragment_search_result) {
} }
} }
} }
override fun onDestroy() {
super.onDestroy()
_binding = null
}
} }

View File

@ -20,7 +20,6 @@ room = "2.5.1"
kotlinxSerialization = "1.5.0" kotlinxSerialization = "1.5.0"
kotlinxDatetime = "0.4.0" kotlinxDatetime = "0.4.0"
kotlinxRetrofit = "0.8.0" kotlinxRetrofit = "0.8.0"
viewBindingDelegate = "1.5.8"
[libraries] [libraries]
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" } androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
@ -52,4 +51,3 @@ room-compiler = { group = "androidx.room", name = "room-compiler", version.ref =
kotlinx-serialization = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-json", version.ref = "kotlinxSerialization" } kotlinx-serialization = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-json", version.ref = "kotlinxSerialization" }
kotlinx-datetime = { group = "org.jetbrains.kotlinx", name = "kotlinx-datetime", version.ref = "kotlinxDatetime" } kotlinx-datetime = { group = "org.jetbrains.kotlinx", name = "kotlinx-datetime", version.ref = "kotlinxDatetime" }
kotlinx-serialization-retrofit = { group = "com.jakewharton.retrofit", name = "retrofit2-kotlinx-serialization-converter", version.ref = "kotlinxRetrofit" } kotlinx-serialization-retrofit = { group = "com.jakewharton.retrofit", name = "retrofit2-kotlinx-serialization-converter", version.ref = "kotlinxRetrofit" }
viewBindingPropertyDelegate = { group = "com.github.kirich1409", name = "viewbindingpropertydelegate-noreflection", version.ref = "viewBindingDelegate" }