Skip to content

Commit

Permalink
Merge branch 'master' into launch-and-submission-timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
qiarie authored Oct 11, 2024
2 parents fea4e20 + e702d60 commit 9266784
Show file tree
Hide file tree
Showing 30 changed files with 389 additions and 252 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class AddPatientFragment : Fragment(R.layout.add_patient_fragment) {
addQuestionnaireFragment()
}
observePatientSaveAction()
(activity as MainActivity).setDrawerEnabled(false)

/** Use the provided cancel|submit buttons from the sdc library */
childFragmentManager.setFragmentResultListener(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ class EditPatientFragment : Fragment(R.layout.add_patient_fragment) {
Toast.makeText(requireContext(), R.string.message_patient_updated, Toast.LENGTH_SHORT).show()
NavHostFragment.findNavController(this).navigateUp()
}
(activity as MainActivity).setDrawerEnabled(false)

/** Use the provided cancel|submit buttons from the sdc library */
childFragmentManager.setFragmentResultListener(
Expand Down
16 changes: 3 additions & 13 deletions demo/src/main/java/com/google/android/fhir/demo/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.google.android.fhir.demo

import android.os.Bundle
import android.view.MenuItem
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.cardview.widget.CardView
Expand All @@ -30,10 +29,8 @@ class HomeFragment : Fragment(R.layout.fragment_home) {
super.onViewCreated(view, savedInstanceState)
(requireActivity() as AppCompatActivity).supportActionBar?.apply {
title = resources.getString(R.string.app_name)
setDisplayHomeAsUpEnabled(true)
setDisplayHomeAsUpEnabled(false)
}
setHasOptionsMenu(true)
(activity as MainActivity).setDrawerEnabled(true)
setOnClicks()
}

Expand All @@ -47,15 +44,8 @@ class HomeFragment : Fragment(R.layout.fragment_home) {
requireView().findViewById<CardView>(R.id.item_search).setOnClickListener {
findNavController().navigate(HomeFragmentDirections.actionHomeFragmentToPatientList())
}
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
android.R.id.home -> {
(requireActivity() as MainActivity).openNavigationDrawer()
true
}
else -> false
requireView().findViewById<CardView>(R.id.item_sync).setOnClickListener {
findNavController().navigate(HomeFragmentDirections.actionHomeFragmentToSyncFragment())
}
}
}
55 changes: 0 additions & 55 deletions demo/src/main/java/com/google/android/fhir/demo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,78 +17,23 @@
package com.google.android.fhir.demo

import android.os.Bundle
import android.view.MenuItem
import android.widget.TextView
import androidx.activity.viewModels
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout
import com.google.android.fhir.demo.databinding.ActivityMainBinding

const val MAX_RESOURCE_COUNT = 20

class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var drawerToggle: ActionBarDrawerToggle
private val viewModel: MainActivityViewModel by viewModels()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
initActionBar()
initNavigationDrawer()
observeLastSyncTime()
viewModel.updateLastSyncTimestamp()
}

override fun onBackPressed() {
if (binding.drawer.isDrawerOpen(GravityCompat.START)) {
binding.drawer.closeDrawer(GravityCompat.START)
return
}
super.onBackPressed()
}

fun setDrawerEnabled(enabled: Boolean) {
val lockMode =
if (enabled) DrawerLayout.LOCK_MODE_UNLOCKED else DrawerLayout.LOCK_MODE_LOCKED_CLOSED
binding.drawer.setDrawerLockMode(lockMode)
drawerToggle.isDrawerIndicatorEnabled = enabled
}

fun openNavigationDrawer() {
binding.drawer.openDrawer(GravityCompat.START)
viewModel.updateLastSyncTimestamp()
}

private fun initActionBar() {
val toolbar = binding.toolbar
setSupportActionBar(toolbar)
}

private fun initNavigationDrawer() {
binding.navigationView.setNavigationItemSelectedListener(this::onNavigationItemSelected)
drawerToggle = ActionBarDrawerToggle(this, binding.drawer, R.string.open, R.string.close)
binding.drawer.addDrawerListener(drawerToggle)
drawerToggle.syncState()
}

private fun onNavigationItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.menu_sync -> {
viewModel.triggerOneTimeSync()
binding.drawer.closeDrawer(GravityCompat.START)
return false
}
}
return false
}

private fun observeLastSyncTime() {
viewModel.lastSyncTimestampLiveData.observe(this) {
binding.navigationView.getHeaderView(0).findViewById<TextView>(R.id.last_sync_tv).text = it
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022-2023 Google LLC
* Copyright 2022-2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -82,7 +82,6 @@ class PatientDetailsFragment : Fragment() {
}
}
patientDetailsViewModel.getPatientDetailData()
(activity as MainActivity).setDrawerEnabled(false)
}

private fun onAddScreenerClick() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class PatientListFragment : Fragment() {
private val binding
get() = _binding!!

private val mainActivityViewModel: MainActivityViewModel by activityViewModels()
private val mainActivityViewModel: SyncFragmentViewModel by activityViewModels()

override fun onCreateView(
inflater: LayoutInflater,
Expand Down Expand Up @@ -153,7 +153,6 @@ class PatientListFragment : Fragment() {
addPatient.setColorFilter(Color.WHITE)
}
setHasOptionsMenu(true)
(activity as MainActivity).setDrawerEnabled(false)
launchAndRepeatStarted(
{ mainActivityViewModel.pollState.collect(::currentSyncJobStatus) },
{ mainActivityViewModel.pollPeriodicSyncJobStatus.collect(::periodicSyncJobStatus) },
Expand Down
104 changes: 104 additions & 0 deletions demo/src/main/java/com/google/android/fhir/demo/SyncFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright 2024 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.ProgressBar
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.NavHostFragment
import com.google.android.fhir.demo.extensions.launchAndRepeatStarted
import com.google.android.fhir.sync.CurrentSyncJobStatus

class SyncFragment : Fragment() {
private val syncFragmentViewModel: SyncFragmentViewModel by viewModels()

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View {
return inflater.inflate(R.layout.sync, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setUpActionBar()
setHasOptionsMenu(true)
view.findViewById<Button>(R.id.sync_now_button).setOnClickListener {
syncFragmentViewModel.triggerOneTimeSync()
}
observeLastSyncTime()
launchAndRepeatStarted(
{ syncFragmentViewModel.pollState.collect(::currentSyncJobStatus) },
)
}

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.sync)
setDisplayHomeAsUpEnabled(true)
}
}

private fun currentSyncJobStatus(currentSyncJobStatus: CurrentSyncJobStatus) {
requireView().findViewById<TextView>(R.id.current_status).text =
getString(R.string.current_status, currentSyncJobStatus::class.java.simpleName)

// Update progress indicator visibility and handle status-specific actions
val syncIndicator = requireView().findViewById<ProgressBar>(R.id.sync_indicator)
when (currentSyncJobStatus) {
is CurrentSyncJobStatus.Running -> {
syncIndicator.visibility = View.VISIBLE
}
is CurrentSyncJobStatus.Succeeded -> {
syncIndicator.visibility = View.GONE
syncFragmentViewModel.updateLastSyncTimestamp(currentSyncJobStatus.timestamp)
}
is CurrentSyncJobStatus.Failed,
is CurrentSyncJobStatus.Enqueued,
is CurrentSyncJobStatus.Cancelled,
is CurrentSyncJobStatus.Blocked, -> {
syncIndicator.visibility = View.GONE
}
}
}

private fun observeLastSyncTime() {
syncFragmentViewModel.lastSyncTimestampLiveData.observe(viewLifecycleOwner) {
requireView().findViewById<TextView>(R.id.lastSyncTime).text =
getString(R.string.last_sync_timestamp, it)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import kotlinx.coroutines.launch

/** View model for [MainActivity]. */
@OptIn(ExperimentalCoroutinesApi::class)
class MainActivityViewModel(application: Application) : AndroidViewModel(application) {
class SyncFragmentViewModel(application: Application) : AndroidViewModel(application) {
private val _lastSyncTimestampLiveData = MutableLiveData<String>()
val lastSyncTimestampLiveData: LiveData<String>
get() = _lastSyncTimestampLiveData
Expand Down
15 changes: 15 additions & 0 deletions demo/src/main/res/drawable/ic_home_sync.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:width="36dp"
android:height="36dp"
android:autoMirrored="true"
android:viewportHeight="960"
android:viewportWidth="960"
>

<path
android:fillColor="#4285F4"
android:pathData="M160,800v-80h110l-16,-14q-49,-49 -71.5,-106.5T160,482q0,-111 66.5,-197.5T400,170v84q-72,26 -116,88.5T240,482q0,45 17,87.5t53,78.5l10,10v-98h80v240L160,800ZM560,790v-84q72,-26 116,-88.5T720,478q0,-45 -17,-87.5T650,312l-10,-10v98h-80v-240h240v80L690,240l16,14q49,49 71.5,106.5T800,478q0,111 -66.5,197.5T560,790Z"
/>

</vector>
59 changes: 19 additions & 40 deletions demo/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,59 +14,38 @@
See the License for the specific language governing permissions and
limitations under the License.
-->
<androidx.drawerlayout.widget.DrawerLayout
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
>

<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
<!-- AppBar with Toolbar -->
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:layout_height="wrap_content"
>

<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorSurface"
/>

</com.google.android.material.appbar.AppBarLayout>

<androidx.fragment.app.FragmentContainerView
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/reference_nav_graph"
app:defaultNavHost="true"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorSurface"
/>
</com.google.android.material.appbar.AppBarLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
<!-- Fragment Container -->
<androidx.fragment.app.FragmentContainerView
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:clipToPadding="false"
app:menu="@menu/drawer_menu"
app:headerLayout="@layout/drawer_header_layout"
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/reference_nav_graph"
app:defaultNavHost="true"
/>
</androidx.drawerlayout.widget.DrawerLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Loading

0 comments on commit 9266784

Please sign in to comment.