Fix crash

This commit is contained in:
世界 2023-09-06 19:44:38 +08:00
parent 03f9dc4883
commit 49a9fb3817
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
5 changed files with 23 additions and 20 deletions

View file

@ -192,5 +192,9 @@ object DefaultNetworkListener {
}
}
private fun unregister() = Application.connectivity.unregisterNetworkCallback(Callback)
private fun unregister() {
runCatching {
Application.connectivity.unregisterNetworkCallback(Callback)
}
}
}

View file

@ -7,7 +7,7 @@ class ProxyService : Service(), PlatformInterfaceWrapper {
private val service = BoxService(this, this)
override fun onStartCommand(intent: Intent, flags: Int, startId: Int) =
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int) =
service.onStartCommand(intent, flags, startId)
override fun onBind(intent: Intent) = service.onBind(intent)

View file

@ -29,18 +29,14 @@ import kotlinx.coroutines.withContext
class ConfigurationFragment : Fragment() {
private var _adapter: Adapter? = null
private var adapter: Adapter
get() = _adapter as Adapter
set(value) {
_adapter = value
}
private var adapter: Adapter? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View {
val binding = FragmentConfigurationBinding.inflate(inflater, container, false)
adapter = Adapter(lifecycleScope, binding)
val adapter = Adapter(lifecycleScope, binding)
this.adapter = adapter
binding.profileList.also {
it.layoutManager = LinearLayoutManager(requireContext())
it.adapter = adapter
@ -69,17 +65,17 @@ class ConfigurationFragment : Fragment() {
override fun onResume() {
super.onResume()
_adapter?.reload()
adapter?.reload()
}
override fun onDestroyView() {
super.onDestroyView()
ProfileManager.unregisterCallback(this::updateProfiles)
_adapter = null
adapter = null
}
private fun updateProfiles() {
_adapter?.reload()
adapter?.reload()
}
class Adapter(
@ -94,8 +90,10 @@ class ConfigurationFragment : Fragment() {
internal fun reload() {
if (isMoving) return
scope.launch(Dispatchers.IO) {
items = ProfileManager.list().toMutableList()
val newItems = ProfileManager.list().toMutableList()
withContext(Dispatchers.Main) {
items = newItems
notifyDataSetChanged()
if (items.isEmpty()) {
parent.statusText.isVisible = true
parent.profileList.isVisible = false
@ -103,7 +101,6 @@ class ConfigurationFragment : Fragment() {
parent.statusText.isVisible = false
parent.profileList.isVisible = true
}
notifyDataSetChanged()
}
}
}

View file

@ -31,13 +31,12 @@ import kotlinx.coroutines.withContext
class SettingsFragment : Fragment() {
private var _binding: FragmentSettingsBinding? = null
private val binding get() = _binding!!
private var binding: FragmentSettingsBinding? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View {
_binding = FragmentSettingsBinding.inflate(inflater, container, false)
val binding = FragmentSettingsBinding.inflate(inflater, container, false)
this.binding = binding
onCreate()
return binding.root
}
@ -52,6 +51,7 @@ class SettingsFragment : Fragment() {
private fun onCreate() {
val activity = activity as MainActivity? ?: return
val binding = binding ?: return
binding.versionText.text = Libbox.version()
binding.clearButton.setOnClickListener {
lifecycleScope.launch(Dispatchers.IO) {
@ -119,6 +119,7 @@ class SettingsFragment : Fragment() {
private suspend fun reloadSettings() {
val activity = activity ?: return
val binding = binding ?: return
val dataSize = Libbox.formatBytes(
(activity.getExternalFilesDir(null) ?: activity.filesDir)
.walkTopDown().filter { it.isFile }.map { it.length() }.sum()
@ -145,7 +146,7 @@ class SettingsFragment : Fragment() {
override fun onDestroyView() {
super.onDestroyView()
_binding = null
binding = null
}
}

View file

@ -36,7 +36,8 @@
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/dashboardPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="match_parent"
android:saveEnabled="false" />
</LinearLayout>