Files
sms-remote/android/src/main/kotlin/xyz/magicalbits/smsremote/device/DeviceFragment.kt
T

89 lines
3.4 KiB
Kotlin

package xyz.magicalbits.smsremote.device
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.rememberNestedScrollInteropConnection
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.navigation.NavController
import xyz.magicalbits.smsremote.MainViewModel
import xyz.magicalbits.smsremote.R
import xyz.magicalbits.smsremote.components.JetchatAppBar
import xyz.magicalbits.smsremote.theme.JetchatTheme
import kotlin.getValue
import androidx.navigation.findNavController
class DeviceFragment : Fragment() {
private val viewModel: DeviceViewModel by viewModels()
private val activityViewModel: MainViewModel by activityViewModels()
override fun onAttach(context: Context) {
super.onAttach(context)
// Consider using safe args plugin
val deviceId = arguments?.getString("deviceId")
val name = arguments?.getString("name")
val type = arguments?.getString("type")
viewModel.setDeviceData(deviceId, name, type)
}
@OptIn(ExperimentalComposeUiApi::class, ExperimentalMaterial3Api::class)
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val rootView: View = inflater.inflate(R.layout.fragment_profile, container, false)
rootView.findViewById<ComposeView>(R.id.toolbar_compose_view).apply {
setContent {
JetchatTheme {
JetchatAppBar(
// Reset the minimum bounds that are passed to the root of a compose tree
modifier = Modifier.wrapContentSize(),
onNavIconPressed = { activityViewModel.openDrawer() },
title = { },
)
}
}
}
rootView.findViewById<ComposeView>(R.id.profile_compose_view).apply {
setContent {
val deviceData by viewModel.deviceData.observeAsState()
val nestedScrollInteropConnection = rememberNestedScrollInteropConnection()
JetchatTheme {
if (deviceData == null) {
println("calling device error")
DeviceError()
} else {
val navController: NavController = rootView.findNavController()
DeviceScreen(
deviceData = deviceData!!,
nestedScrollInteropConnection = nestedScrollInteropConnection,
onPhoneNumberClicked = {
val args = Bundle(1)
args.putString("phoneNumber", it)
navController.navigate(R.id.action_device_to_conversation, args)
},
)
}
}
}
}
return rootView
}
}