diff --git a/demo/src/main/java/com/google/android/fhir/demo/CrudOperationFragment.kt b/demo/src/main/java/com/google/android/fhir/demo/CrudOperationFragment.kt new file mode 100644 index 0000000000..2694044bc8 --- /dev/null +++ b/demo/src/main/java/com/google/android/fhir/demo/CrudOperationFragment.kt @@ -0,0 +1,338 @@ +/* + * Copyright 2024-2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.android.fhir.demo + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.MenuItem +import android.view.View +import android.view.ViewGroup +import android.widget.Button +import android.widget.CheckBox +import android.widget.EditText +import android.widget.RadioGroup +import android.widget.Toast +import androidx.appcompat.app.AppCompatActivity +import androidx.fragment.app.Fragment +import androidx.fragment.app.viewModels +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.lifecycleScope +import androidx.lifecycle.repeatOnLifecycle +import androidx.navigation.fragment.NavHostFragment +import com.google.android.fhir.demo.helpers.PatientCreationHelper +import com.google.android.material.tabs.TabLayout +import kotlinx.coroutines.launch +import org.hl7.fhir.r4.model.Enumerations + +class CrudOperationFragment : Fragment() { + private val crudOperationViewModel: CrudOperationViewModel by viewModels() + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle?, + ): View { + return inflater.inflate(R.layout.fragment_crud_layout, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + setUpActionBar() + setHasOptionsMenu(true) + setupUiOnScreenLaunch() + viewLifecycleOwner.lifecycleScope.launch { + viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { + crudOperationViewModel.patientUiState.collect { patientUiState -> + patientUiState?.let { + when (it.operationType) { + OperationType.CREATE -> { + Toast.makeText(requireContext(), "Patient is saved", Toast.LENGTH_SHORT).show() + } + OperationType.READ -> displayPatientDetails(it) + OperationType.UPDATE -> { + Toast.makeText(requireContext(), "Patient is updated", Toast.LENGTH_SHORT).show() + } + OperationType.DELETE -> { + // Reset the page as the patient has been deleted. + clearUiFieldValues() + configureFieldsForOperation(OperationType.DELETE) + Toast.makeText(requireContext(), "Patient is deleted", Toast.LENGTH_SHORT).show() + } + } + } + } + } + } + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean { + return when (item.itemId) { + android.R.id.home -> { + NavHostFragment.findNavController(this).navigateUp() + true + } + else -> false + } + } + + private fun setUpActionBar() { + (requireActivity() as AppCompatActivity).supportActionBar?.apply { + title = requireContext().getString(R.string.crud_operations) + setDisplayHomeAsUpEnabled(true) + } + } + + private fun setupUiOnScreenLaunch() { + setupTabLayoutChangeListener() + selectTab(TAB_CREATE) + setupUiForCrudOperation(OperationType.CREATE) + + requireView().findViewById