Skip to content

Commit

Permalink
periodic sync separatee screen.
Browse files Browse the repository at this point in the history
  • Loading branch information
Santosh Pingle committed Oct 14, 2024
1 parent c28ac63 commit fc01f54
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ class HomeFragment : Fragment(R.layout.fragment_home) {
findNavController().navigate(HomeFragmentDirections.actionHomeFragmentToPatientList())
}
requireView().findViewById<CardView>(R.id.item_sync).setOnClickListener {
findNavController().navigate(HomeFragmentDirections.actionHomeFragmentToSyncFragment())
findNavController().navigate(HomeFragmentDirections.actionHomeFragmentToManualSyncFragment())
}
requireView().findViewById<CardView>(R.id.item_periodic_sync).setOnClickListener {
findNavController()
.navigate(HomeFragmentDirections.actionHomeFragmentToPeriodicSyncFragment())
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* 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
import com.google.android.fhir.sync.LastSyncJobStatus
import com.google.android.fhir.sync.PeriodicSyncJobStatus

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

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View {
return inflater.inflate(R.layout.periodic_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 {
launchAndRepeatStarted(
{ syncFragmentViewModel.pollPeriodicSyncJobStatus.collect(::periodicSyncJobStatus) },
)
}
}

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

private fun periodicSyncJobStatus(periodicSyncJobStatus: PeriodicSyncJobStatus) {
// Handle last sync job status and update UI
periodicSyncJobStatus.lastSyncJobStatus?.let { lastStatus ->
val lastSyncStatusValue = when (lastStatus) {
is LastSyncJobStatus.Succeeded -> getString(R.string.last_sync_status, LastSyncJobStatus.Succeeded::class.java.simpleName)
is LastSyncJobStatus.Failed -> getString(R.string.last_sync_status, LastSyncJobStatus.Failed::class.java.simpleName)
else -> null
}

lastSyncStatusValue?.let { statusText ->
requireView().findViewById<TextView>(R.id.last_sync_status).text = statusText
requireView().findViewById<TextView>(R.id.last_sync_time).text = getString(
R.string.last_sync_timestamp,
syncFragmentViewModel.formatSyncTimestamp(lastStatus.timestamp)
)
}
}

// Set current sync status
val currentSyncStatusTextView = requireView().findViewById<TextView>(R.id.current_sync_status)
currentSyncStatusTextView.text = getString(
R.string.current_status,
periodicSyncJobStatus.currentSyncJobStatus::class.java.simpleName
)

// Update progress indicator visibility based on current sync status
val syncIndicator = requireView().findViewById<ProgressBar>(R.id.sync_indicator)
syncIndicator.visibility = if (periodicSyncJobStatus.currentSyncJobStatus is CurrentSyncJobStatus.Running) {
View.VISIBLE
} else {
View.GONE
}

// Control the visibility of the current sync status text view
currentSyncStatusTextView.visibility = when (periodicSyncJobStatus.currentSyncJobStatus) {
is CurrentSyncJobStatus.Failed, is CurrentSyncJobStatus.Succeeded -> View.GONE
else -> View.VISIBLE
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,17 @@ class SyncFragmentViewModel(application: Application) : AndroidViewModel(applica

/** Emits last sync time. */
fun updateLastSyncTimestamp(lastSync: OffsetDateTime? = null) {
_lastSyncTimestampLiveData.value = formatSyncTimestamp(lastSync)
}

fun formatSyncTimestamp(lastSync: OffsetDateTime? = null): String {
val formatter =
DateTimeFormatter.ofPattern(
if (DateFormat.is24HourFormat(getApplication())) formatString24 else formatString12,
)
_lastSyncTimestampLiveData.value =
lastSync?.let { it.toLocalDateTime()?.format(formatter) ?: "" }
?: Sync.getLastSyncTimestamp(getApplication())?.toLocalDateTime()?.format(formatter) ?: ""

return lastSync?.let { it.toLocalDateTime()?.format(formatter) ?: "" }
?: Sync.getLastSyncTimestamp(getApplication())?.toLocalDateTime()?.format(formatter) ?: ""
}

companion object {
Expand Down
44 changes: 44 additions & 0 deletions demo/src/main/res/layout/fragment_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,48 @@
</LinearLayout>

</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/item_periodic_sync"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/search_patient_cardview_horizontal_margin"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:layout_marginTop="@dimen/search_patient_cardview_margin_top"
android:layout_marginVertical="@dimen/search_patient_cardview_vertical_margin"
app:cardBackgroundColor="@color/white"
app:cardCornerRadius="@dimen/cardView_radius_corner"
android:visibility="visible"
>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/new_patient_padding"
android:orientation="horizontal"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_home_sync"
android:contentDescription="Task Image"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?textAppearanceTitleLarge"
android:textSize="@dimen/dashboard_text_fontSize"
android:textColor="@color/dashboard_cardview_textcolor"
android:ellipsize="end"
android:maxLines="1"
android:layout_marginHorizontal="@dimen/new_patient_horizontal_margin"
android:layout_gravity="center"
android:text="Periodic Sync"
/>
</LinearLayout>

</androidx.cardview.widget.CardView>
</LinearLayout>
13 changes: 12 additions & 1 deletion demo/src/main/res/navigation/reference_nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@
app:destination="@id/patient_list"
/>
<action
android:id="@+id/action_home_fragment_to_sync_fragment"
android:id="@+id/action_home_fragment_to_manual_sync_fragment"
app:destination="@id/manualSyncFragment"
/>
<action
android:id="@+id/action_home_fragment_to_periodic_sync_fragment"
app:destination="@id/periodicSyncFragment"
/>
</fragment>

<fragment
Expand Down Expand Up @@ -91,4 +95,11 @@
tools:layout="@layout/sync"
/>

<fragment
android:id="@+id/periodicSyncFragment"
android:name="com.google.android.fhir.demo.PeriodicSyncFragment"
android:label="fragment_periodic_sync"
tools:layout="@layout/periodic_sync"
/>

</navigation>
3 changes: 2 additions & 1 deletion demo/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,6 @@
<string name="sync">Sync</string>
<string name="last_sync_timestamp">Last sync: %1$s</string>
<string name="current_status">Sync status: %1$s</string>
<string name="manual_sync">Sync</string>
<string name="last_sync_status">Last sync status: %1$s</string>
<string name="periodic_sync">Periodic Sync</string>
</resources>

0 comments on commit fc01f54

Please sign in to comment.