mirror of
https://invent.kde.org/network/kdeconnect-android.git
synced 2025-12-12 20:35:58 +01:00
created setActionBarText called from onViewCreated
This commit is contained in:
committed by
Albert Vaca Cintora
parent
0e877bdb49
commit
f2f2d81b2f
@@ -32,6 +32,8 @@ class SystemVolumeFragment : BaseFragment<SystemVolumeFragmentBinding>(),
|
||||
private var tracking = false
|
||||
private val trackingConsumer = Consumer { aBoolean: Boolean -> tracking = aBoolean }
|
||||
|
||||
override fun getActionBarTitle() = getString(R.string.open_mpris_controls)
|
||||
|
||||
override fun onInflateBinding(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
|
||||
@@ -13,7 +13,6 @@ import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.FrameLayout
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.net.toUri
|
||||
import androidx.fragment.app.Fragment
|
||||
@@ -41,6 +40,8 @@ class AboutFragment : BaseFragment<FragmentAboutBinding>() {
|
||||
}
|
||||
}
|
||||
|
||||
override fun getActionBarTitle() = getString(R.string.about)
|
||||
|
||||
private lateinit var aboutData: AboutData
|
||||
private var tapCount = 0
|
||||
private var firstTapMillis: Long? = null
|
||||
@@ -60,7 +61,6 @@ class AboutFragment : BaseFragment<FragmentAboutBinding>() {
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
(activity as? AppCompatActivity)?.supportActionBar?.setTitle(R.string.about)
|
||||
binding.scrollView.setupBottomPadding()
|
||||
updateData()
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import android.widget.Toast
|
||||
import androidx.activity.OnBackPressedCallback
|
||||
import androidx.annotation.StringRes
|
||||
import androidx.annotation.UiThread
|
||||
import androidx.appcompat.app.ActionBar
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
@@ -82,16 +83,15 @@ class DeviceFragment : BaseFragment<ActivityDeviceBinding>() {
|
||||
return frag
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
override fun getActionBarTitle() = null
|
||||
|
||||
val deviceId: String by lazy {
|
||||
arguments?.getString(ARG_DEVICE_ID)
|
||||
?: throw RuntimeException("You must instantiate a new DeviceFragment using DeviceFragment.newInstance()")
|
||||
}
|
||||
|
||||
private var device: Device? = null
|
||||
|
||||
private val mActivity: MainActivity? by lazy { activity as MainActivity? }
|
||||
private val device by lazy { KdeConnect.getInstance().getDevice(deviceId) }
|
||||
|
||||
/**
|
||||
* Not-yet-paired ViewBinding.
|
||||
@@ -117,7 +117,7 @@ class DeviceFragment : BaseFragment<ActivityDeviceBinding>() {
|
||||
val callback = object : OnBackPressedCallback(true) {
|
||||
override fun handleOnBackPressed() {
|
||||
// Handle back button, so we go to the list of devices in case we came from there
|
||||
mActivity?.onDeviceSelected(null)
|
||||
(mActivity as? MainActivity)?.onDeviceSelected(null)
|
||||
}
|
||||
}
|
||||
requireActivity().onBackPressedDispatcher.addCallback(getViewLifecycleOwner(), callback)
|
||||
@@ -135,7 +135,7 @@ class DeviceFragment : BaseFragment<ActivityDeviceBinding>() {
|
||||
for (p in plugins) {
|
||||
if (p.displayInContextMenu()) {
|
||||
menu.add(p.actionName).setOnMenuItemClickListener {
|
||||
p.startMainActivity(mActivity!!)
|
||||
mActivity?.let { p.startMainActivity(it) }
|
||||
true
|
||||
}
|
||||
}
|
||||
@@ -180,7 +180,7 @@ class DeviceFragment : BaseFragment<ActivityDeviceBinding>() {
|
||||
removePluginsChangedListener(pluginsChangedListener)
|
||||
unpair()
|
||||
}
|
||||
mActivity?.onDeviceSelected(null)
|
||||
(mActivity as? MainActivity)?.onDeviceSelected(null)
|
||||
true
|
||||
}
|
||||
}
|
||||
@@ -220,16 +220,15 @@ class DeviceFragment : BaseFragment<ActivityDeviceBinding>() {
|
||||
removePairingCallback(pairingCallback)
|
||||
cancelPairing()
|
||||
}
|
||||
mActivity?.onDeviceSelected(null)
|
||||
(mActivity as? MainActivity)?.onDeviceSelected(null)
|
||||
}
|
||||
device = KdeConnect.getInstance().getDevice(deviceId)
|
||||
device?.apply {
|
||||
mActivity?.supportActionBar?.title = name
|
||||
addPairingCallback(pairingCallback)
|
||||
addPluginsChangedListener(pluginsChangedListener)
|
||||
} ?: run { // device is null
|
||||
Log.e(TAG, "Trying to display a device fragment but the device is not present")
|
||||
mActivity?.onDeviceSelected(null)
|
||||
(mActivity as? MainActivity)?.onDeviceSelected(null)
|
||||
}
|
||||
mActivity?.addMenuProvider(menuProvider, viewLifecycleOwner)
|
||||
refreshUI()
|
||||
@@ -257,7 +256,6 @@ class DeviceFragment : BaseFragment<ActivityDeviceBinding>() {
|
||||
removePluginsChangedListener(pluginsChangedListener)
|
||||
removePairingCallback(pairingCallback)
|
||||
}
|
||||
device = null
|
||||
super.onDestroyView()
|
||||
}
|
||||
|
||||
@@ -390,11 +388,6 @@ class DeviceFragment : BaseFragment<ActivityDeviceBinding>() {
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDetach() {
|
||||
super.onDetach()
|
||||
mActivity?.supportActionBar?.subtitle = null
|
||||
}
|
||||
|
||||
@Composable
|
||||
@Preview
|
||||
fun PreviewCompose() {
|
||||
@@ -409,7 +402,7 @@ class DeviceFragment : BaseFragment<ActivityDeviceBinding>() {
|
||||
Card(
|
||||
shape = MaterialTheme.shapes.medium,
|
||||
modifier = modifier.semantics { role = Role.Button },
|
||||
onClick = { plugin.startMainActivity(mActivity!!) }
|
||||
onClick = { mActivity?.let { plugin.startMainActivity(it) } }
|
||||
) {
|
||||
Column(
|
||||
verticalArrangement = Arrangement.spacedBy(10.dp),
|
||||
|
||||
@@ -62,8 +62,6 @@ class PairingFragment : BaseFragment<DevicesListBinding>() {
|
||||
|
||||
private var listRefreshCalledThisFrame = false
|
||||
|
||||
private val mainActivity by lazy { activity as MainActivity }
|
||||
|
||||
private val menuProvider = object : MenuProvider {
|
||||
|
||||
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
|
||||
@@ -77,11 +75,11 @@ class PairingFragment : BaseFragment<DevicesListBinding>() {
|
||||
true
|
||||
}
|
||||
R.id.menu_custom_device_list -> {
|
||||
startActivity(Intent(mainActivity, CustomDevicesActivity::class.java))
|
||||
startActivity(Intent(mActivity, CustomDevicesActivity::class.java))
|
||||
true
|
||||
}
|
||||
R.id.menu_trusted_networks -> {
|
||||
startActivity(Intent(mainActivity, TrustedNetworksActivity::class.java))
|
||||
startActivity(Intent(mActivity, TrustedNetworksActivity::class.java))
|
||||
true
|
||||
}
|
||||
else -> false
|
||||
@@ -89,6 +87,8 @@ class PairingFragment : BaseFragment<DevicesListBinding>() {
|
||||
}
|
||||
}
|
||||
|
||||
override fun getActionBarTitle() = getString(R.string.pairing_title)
|
||||
|
||||
override fun onInflateBinding(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
@@ -110,8 +110,7 @@ class PairingFragment : BaseFragment<DevicesListBinding>() {
|
||||
binding.devicesList.itemsCanFocus = true
|
||||
binding.devicesList.setupBottomPadding()
|
||||
|
||||
mainActivity.supportActionBar?.setTitle(R.string.pairing_title)
|
||||
mainActivity.addMenuProvider(menuProvider, viewLifecycleOwner, Lifecycle.State.RESUMED)
|
||||
mActivity?.addMenuProvider(menuProvider, viewLifecycleOwner, Lifecycle.State.RESUMED)
|
||||
|
||||
notTrustedText.setOnClickListener(null)
|
||||
notTrustedText.setOnLongClickListener(null)
|
||||
@@ -260,7 +259,7 @@ class PairingFragment : BaseFragment<DevicesListBinding>() {
|
||||
val v = binding.devicesList.getChildAt(0)
|
||||
val top = if ((v == null)) 0 else (v.top - binding.devicesList.paddingTop)
|
||||
|
||||
binding.devicesList.adapter = ListAdapter(mainActivity, items)
|
||||
binding.devicesList.adapter = ListAdapter(requireContext(), items)
|
||||
|
||||
//Restore scroll
|
||||
binding.devicesList.setSelectionFromTop(index, top)
|
||||
@@ -310,9 +309,9 @@ class PairingFragment : BaseFragment<DevicesListBinding>() {
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
KdeConnect.getInstance().addDeviceListChangedCallback("PairingFragment") {
|
||||
mainActivity.runOnUiThread { this.updateDeviceList() }
|
||||
mActivity?.runOnUiThread { this.updateDeviceList() }
|
||||
}
|
||||
ForceRefreshConnections(mainActivity) // force a network re-discover
|
||||
ForceRefreshConnections(requireContext()) // force a network re-discover
|
||||
updateDeviceList()
|
||||
}
|
||||
|
||||
@@ -322,14 +321,14 @@ class PairingFragment : BaseFragment<DevicesListBinding>() {
|
||||
}
|
||||
|
||||
fun deviceClicked(device: Device) {
|
||||
mainActivity.onDeviceSelected(device.deviceId, !device.isPaired || !device.isReachable)
|
||||
(mActivity as? MainActivity)?.onDeviceSelected(device.deviceId, !device.isPaired || !device.isReachable)
|
||||
}
|
||||
|
||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||
when (requestCode) {
|
||||
RESULT_PAIRING_SUCCESFUL -> if (resultCode == 1) {
|
||||
val deviceId = data?.getStringExtra("deviceId")
|
||||
mainActivity.onDeviceSelected(deviceId)
|
||||
(mActivity as? MainActivity)?.onDeviceSelected(deviceId)
|
||||
}
|
||||
|
||||
else -> super.onActivityResult(requestCode, resultCode, data)
|
||||
|
||||
@@ -49,7 +49,8 @@ import java.io.InputStreamReader
|
||||
class SettingsFragment : PreferenceFragmentCompat() {
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
||||
if (activity != null) {
|
||||
(activity as MainActivity).supportActionBar?.setTitle(R.string.settings)
|
||||
(activity as? MainActivity)?.supportActionBar?.setTitle(R.string.settings)
|
||||
(activity as? MainActivity)?.supportActionBar?.subtitle = null
|
||||
}
|
||||
return super.onCreateView(inflater, container, savedInstanceState)
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.viewbinding.ViewBinding
|
||||
|
||||
@@ -18,6 +19,11 @@ abstract class BaseFragment<VB: ViewBinding> : Fragment() {
|
||||
|
||||
protected val binding get() = _binding!!
|
||||
|
||||
protected val mActivity
|
||||
get() = activity as? AppCompatActivity?
|
||||
|
||||
protected abstract fun getActionBarTitle(): String?
|
||||
|
||||
abstract fun onInflateBinding(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): VB
|
||||
|
||||
override fun onCreateView(
|
||||
@@ -29,8 +35,18 @@ abstract class BaseFragment<VB: ViewBinding> : Fragment() {
|
||||
return binding.root
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
setActionBarText()
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
_binding = null
|
||||
}
|
||||
|
||||
private fun setActionBarText(){
|
||||
mActivity?.supportActionBar?.title = getActionBarTitle()
|
||||
mActivity?.supportActionBar?.subtitle = null
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user