Skip to content

Commit

Permalink
Simplify codelab code (#2060)
Browse files Browse the repository at this point in the history
* Initial version

* make app work

* add update function

* Run spotless apply

* Remove unnecessary header in FHIR Engine codelab README.md

* Simplify engine codelab code

* Update codelabs/engine/app/src/main/java/com/google/android/fhir/codelabs/engine/DownloadWorkManagerImpl.kt

Co-authored-by: Omar Ismail <[email protected]>

---------

Co-authored-by: omarismail <[email protected]>
Co-authored-by: Omar Ismail <[email protected]>
  • Loading branch information
3 people authored Jul 1, 2023
1 parent b038f27 commit 2251940
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 289 deletions.
3 changes: 0 additions & 3 deletions codelabs/engine/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Codelab"
android:networkSecurityConfig="@xml/network_security_config"
android:name=".FhirApplication"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2022 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.codelabs.engine

import com.google.android.fhir.sync.DownloadWorkManager
import com.google.android.fhir.sync.Request
import java.util.LinkedList
import org.hl7.fhir.r4.model.Bundle
import org.hl7.fhir.r4.model.Resource
import org.hl7.fhir.r4.model.ResourceType

class DownloadWorkManagerImpl : DownloadWorkManager {
private val urls = LinkedList(listOf("Patient"))

override suspend fun getNextRequest(): Request? {
val url = urls.poll() ?: return null
return Request.of(url)
}

override suspend fun getSummaryRequestUrls() = mapOf<ResourceType, String>()

override suspend fun processResponse(response: Resource): Collection<Resource> {
var bundleCollection: Collection<Resource> = mutableListOf()
if (response is Bundle && response.type == Bundle.BundleType.SEARCHSET) {
bundleCollection = response.entry.map { it.resource }
}
return bundleCollection
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import com.google.android.fhir.sync.FhirSyncWorker
class FhirSyncWorker(appContext: Context, workerParams: WorkerParameters) :
FhirSyncWorker(appContext, workerParams) {

override fun getDownloadWorkManager() = PatientDownloadWorkManagerImpl()
override fun getDownloadWorkManager() = DownloadWorkManagerImpl()

override fun getConflictResolver() = AcceptLocalConflictResolver

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,15 @@ import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import com.google.android.fhir.codelabs.engine.databinding.PatientListItemViewBinding
import org.hl7.fhir.r4.model.Patient

/** UI Controller helper class to monitor Patient viewmodel and display list of patients. */
class PatientItemRecyclerViewAdapter :
ListAdapter<MainActivityViewModel.PatientItem, PatientItemViewHolder>(PatientItemDiffCallback()) {
ListAdapter<Patient, PatientItemViewHolder>(PatientItemDiffCallback()) {

class PatientItemDiffCallback : DiffUtil.ItemCallback<MainActivityViewModel.PatientItem>() {
override fun areItemsTheSame(
oldItem: MainActivityViewModel.PatientItem,
newItem: MainActivityViewModel.PatientItem
): Boolean = oldItem.resourceId == newItem.resourceId

override fun areContentsTheSame(
oldItem: MainActivityViewModel.PatientItem,
newItem: MainActivityViewModel.PatientItem
): Boolean = oldItem.id == newItem.id && oldItem.risk == newItem.risk
class PatientItemDiffCallback : DiffUtil.ItemCallback<Patient>() {
override fun areItemsTheSame(oldItem: Patient, newItem: Patient) = oldItem.id == newItem.id
override fun areContentsTheSame(oldItem: Patient, newItem: Patient) =
oldItem.equalsDeep(newItem)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PatientItemViewHolder {
Expand All @@ -46,6 +40,6 @@ class PatientItemRecyclerViewAdapter :

override fun onBindViewHolder(holder: PatientItemViewHolder, position: Int) {
val item = currentList[position]
holder.bindTo(item)
holder.bind(item)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,19 @@ package com.google.android.fhir.codelabs.engine
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.google.android.fhir.codelabs.engine.databinding.PatientListItemViewBinding
import org.hl7.fhir.r4.model.Patient

class PatientItemViewHolder(binding: PatientListItemViewBinding) :
RecyclerView.ViewHolder(binding.root) {
private val nameView: TextView = binding.name
private val idView: TextView = binding.id

fun bindTo(
patientItem: MainActivityViewModel.PatientItem,
) {
this.nameView.text = patientItem.name
this.idView.text = "Id: #---${getTruncatedId(patientItem)}"
}
private val nameTextView: TextView = binding.name
private val genderTextView: TextView = binding.gender
private val cityTextView = binding.city

/** The new ui just shows shortened id with just last 3 characters. */
private fun getTruncatedId(patientItem: MainActivityViewModel.PatientItem): String {
return patientItem.resourceId.takeLast(3)
fun bind(patientItem: Patient) {
nameTextView.text =
patientItem.name.first().let { it.given.joinToString(separator = " ") + " " + it.family }
genderTextView.text = patientItem.gender.display
cityTextView.text = patientItem.address.singleOrNull()?.city
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package com.google.android.fhir.codelabs.engine

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.Menu
Expand All @@ -29,55 +27,45 @@ import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.core.view.MenuHost
import androidx.core.view.MenuProvider
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.DividerItemDecoration
import com.google.android.fhir.codelabs.engine.databinding.FragmentPatientListBinding
import com.google.android.fhir.codelabs.engine.databinding.FragmentPatientListViewBinding
import com.google.android.fhir.sync.SyncJobStatus
import kotlinx.coroutines.launch

class PatientListFragment : Fragment() {
private lateinit var searchView: SearchView
private var _binding: FragmentPatientListBinding? = null
private var _binding: FragmentPatientListViewBinding? = null
private val binding
get() = _binding!!

private val viewModel: MainActivityViewModel by activityViewModels()
private val viewModel: PatientListViewModel by viewModels()

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View {
_binding = FragmentPatientListBinding.inflate(inflater, container, false)
_binding = FragmentPatientListViewBinding.inflate(inflater, container, false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
(requireActivity() as AppCompatActivity).supportActionBar?.apply {
setDisplayHomeAsUpEnabled(true)
}
val adapter = PatientItemRecyclerViewAdapter()
binding.patientList.apply {
this.adapter = adapter
addItemDecoration(
DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL).apply {
setDrawable(ColorDrawable(Color.LTGRAY))
}
)
}

initSearchView()
initMenu()

viewModel.liveSearchedPatients.observe(viewLifecycleOwner) { adapter.submitList(it) }
PatientItemRecyclerViewAdapter().apply {
binding.patientList.adapter = this
viewModel.liveSearchedPatients.observe(viewLifecycleOwner) { submitList(it) }
}

viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
Expand Down
Loading

0 comments on commit 2251940

Please sign in to comment.