diff --git a/openmrs-android-sdk/src/main/java/com/openmrs/android_sdk/library/api/repository/ProviderRepository.java b/openmrs-android-sdk/src/main/java/com/openmrs/android_sdk/library/api/repository/ProviderRepository.java index 4ce49edfb..58588203f 100644 --- a/openmrs-android-sdk/src/main/java/com/openmrs/android_sdk/library/api/repository/ProviderRepository.java +++ b/openmrs-android-sdk/src/main/java/com/openmrs/android_sdk/library/api/repository/ProviderRepository.java @@ -96,8 +96,7 @@ public Observable> getProviders() { return createObservableIO(() -> { // If not online, fetch providers locally if (!NetworkUtils.isOnline()) { - ToastUtil.notify(context.getString(R.string.offline_provider_fetch)); - logger.e("offline providers fetched couldn't sync with the database device offline"); + logger.e("Offline providers fetched, couldn't sync with the database while offline"); return providerRoomDao.getProviderList().blockingGet(); } providerRoomDao.deleteAll(); diff --git a/openmrs-client/src/main/AndroidManifest.xml b/openmrs-client/src/main/AndroidManifest.xml index 41a0c8715..7ebec87e2 100644 --- a/openmrs-client/src/main/AndroidManifest.xml +++ b/openmrs-client/src/main/AndroidManifest.xml @@ -251,6 +251,7 @@ android:configChanges="keyboardHidden|screenSize" android:label="@string/provider_manager" android:launchMode="singleTop" + android:parentActivityName=".activities.dashboard.DashboardActivity" android:theme="@style/AppThemeOrig" /> (com.google.android.material.R.id.snackbar_text) + textView.setTextColor(Color.WHITE) + mSnackbar.show() + } +} diff --git a/openmrs-client/src/main/java/org/openmrs/mobile/activities/providermanagerdashboard/ProviderManagerDashboardContract.java b/openmrs-client/src/main/java/org/openmrs/mobile/activities/providermanagerdashboard/ProviderManagerDashboardContract.java deleted file mode 100644 index f091f3488..000000000 --- a/openmrs-client/src/main/java/org/openmrs/mobile/activities/providermanagerdashboard/ProviderManagerDashboardContract.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * The contents of this file are subject to the OpenMRS Public License - * Version 1.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://license.openmrs.org - * - * Software distributed under the License is distributed on an "AS IS" - * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the - * License for the specific language governing rights and limitations - * under the License. - * - * Copyright (C) OpenMRS, LLC. All Rights Reserved. - */ - -package org.openmrs.mobile.activities.providermanagerdashboard; - -import androidx.fragment.app.Fragment; - -import com.openmrs.android_sdk.library.models.Provider; - -import org.openmrs.mobile.activities.BasePresenterContract; -import org.openmrs.mobile.activities.BaseView; - -import java.util.List; - -public interface ProviderManagerDashboardContract { - interface View extends BaseView { - void refreshUI(); - - void updateAdapter(List providerList); - - void updateVisibility(boolean visibility, String text); - } - - interface Presenter extends BasePresenterContract { - void getProviders(Fragment fragment); - - void updateViews(List providerList); - - void deleteProvider(String providerUuid); - - void addProvider(Provider provider); - - void updateProvider(Provider provider); - } -} \ No newline at end of file diff --git a/openmrs-client/src/main/java/org/openmrs/mobile/activities/providermanagerdashboard/ProviderManagerDashboardFragment.java b/openmrs-client/src/main/java/org/openmrs/mobile/activities/providermanagerdashboard/ProviderManagerDashboardFragment.java deleted file mode 100644 index c6c7eee4d..000000000 --- a/openmrs-client/src/main/java/org/openmrs/mobile/activities/providermanagerdashboard/ProviderManagerDashboardFragment.java +++ /dev/null @@ -1,173 +0,0 @@ -/* - * The contents of this file are subject to the OpenMRS Public License - * Version 1.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://license.openmrs.org - * - * Software distributed under the License is distributed on an "AS IS" - * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the - * License for the specific language governing rights and limitations - * under the License. - * - * Copyright (C) OpenMRS, LLC. All Rights Reserved. - */ - -package org.openmrs.mobile.activities.providermanagerdashboard; - -import android.content.Intent; -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ProgressBar; -import android.widget.TextView; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.recyclerview.widget.RecyclerView; -import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; - -import com.openmrs.android_sdk.library.models.Provider; -import com.google.android.material.floatingactionbutton.FloatingActionButton; - -import org.openmrs.mobile.activities.ACBaseFragment; -import org.openmrs.mobile.activities.providermanagerdashboard.addprovider.AddProviderActivity; -import org.openmrs.mobile.databinding.FragmentProviderManagementBinding; -import com.openmrs.android_sdk.utilities.ApplicationConstants; - -import java.util.ArrayList; -import java.util.List; - -import static android.app.Activity.RESULT_OK; -import static com.openmrs.android_sdk.utilities.ApplicationConstants.RequestCodes.ADD_PROVIDER_REQ_CODE; -import static com.openmrs.android_sdk.utilities.ApplicationConstants.RequestCodes.EDIT_PROVIDER_REQ_CODE; - -public class ProviderManagerDashboardFragment extends ACBaseFragment - implements ProviderManagerDashboardContract.View { - - private FragmentProviderManagementBinding binding; - private TextView emptyList; - private RecyclerView providerManagementRecyclerView; - private ProviderManagerDashboardRecyclerViewAdapter providersAdapter; - private SwipeRefreshLayout refreshList; - private ProgressBar progressBar; - private List providerList; - public FloatingActionButton addProviderFab; - - /** - * @return New instance of ProviderManagerDashboardFragment - */ - public static ProviderManagerDashboardFragment newInstance() { - return new ProviderManagerDashboardFragment(); - } - - @Nullable - @Override - public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - binding = FragmentProviderManagementBinding.inflate(inflater, container, false); - View root = binding.getRoot(); - providerList = new ArrayList<>(); - - providersAdapter = new ProviderManagerDashboardRecyclerViewAdapter(this, mPresenter, providerList); - - providerManagementRecyclerView = binding.providerManagementRecyclerView; - providerManagementRecyclerView.setHasFixedSize(true); - providerManagementRecyclerView.setAdapter(providersAdapter); - LinearLayoutManager linearLayoutManager = new LinearLayoutManager(root.getContext()); - providerManagementRecyclerView.setLayoutManager(linearLayoutManager); - - emptyList = binding.emptyProviderManagementList; - progressBar = binding.providerManagementInitialProgressBar; - addProviderFab = binding.providerManagementFragAddFAB; - refreshList = binding.swipeLayout; - - refreshUI(); - - addProviderFab.setOnClickListener(view -> { - Intent intent = new Intent(getActivity(), AddProviderActivity.class); - ArrayList providerArrayList = new ArrayList<>(providerList); - intent.putExtra(ApplicationConstants.BundleKeys.EXISTING_PROVIDERS_BUNDLE, providerArrayList); - startActivityForResult(intent, ADD_PROVIDER_REQ_CODE); - }); - - refreshList.setOnRefreshListener(() -> { - refreshUI(); - refreshList.setRefreshing(false); - }); - return root; - } - - @Override - public void refreshUI() { - providerManagementRecyclerView.setVisibility(View.GONE); - progressBar.setVisibility(View.VISIBLE); - mPresenter.getProviders(this); - } - - @Override - public void updateAdapter(List providerList) { - this.providerList = providerList; - providersAdapter.setItems(providerList); - providersAdapter.notifyDataSetChanged(); - } - - @Override - public void updateVisibility(boolean visibility, String text) { - progressBar.setVisibility(View.GONE); - if (visibility) { - providerManagementRecyclerView.setVisibility(View.VISIBLE); - emptyList.setVisibility(View.GONE); - } else { - providerManagementRecyclerView.setVisibility(View.GONE); - emptyList.setVisibility(View.VISIBLE); - emptyList.setText(text); - } - } - - public void filterProviders(String searchString) { - if (searchString.isEmpty()) { - providersAdapter.setItems(providerList); - providersAdapter.notifyDataSetChanged(); - return; - } - - List filteredProviders = new ArrayList<>(providerList); - for (Provider provider : providerList) { - if (!provider.getDisplay().toLowerCase().contains(searchString.toLowerCase())) { - filteredProviders.remove(provider); - } - } - - providersAdapter.setItems(filteredProviders); - providersAdapter.notifyDataSetChanged(); - } - - @Override - public void onActivityResult(int requestCode, int resultCode, Intent data) { - if (requestCode == ADD_PROVIDER_REQ_CODE) { - if (resultCode == RESULT_OK) { - Provider provider = (Provider) data.getSerializableExtra(ApplicationConstants.BundleKeys.PROVIDER_ID_BUNDLE); - mPresenter.addProvider(provider); - } - } else if (requestCode == EDIT_PROVIDER_REQ_CODE) { - if (resultCode == RESULT_OK) { - Provider provider = (Provider) data.getSerializableExtra(ApplicationConstants.BundleKeys.PROVIDER_ID_BUNDLE); - mPresenter.updateProvider(provider); - } - } - providersAdapter.notifyDataSetChanged(); - } - - @Override - public void onResume() { - super.onResume(); - refreshUI(); - } - - @Override - public void onDestroyView() { - super.onDestroyView(); - binding = null; - } -} \ No newline at end of file diff --git a/openmrs-client/src/main/java/org/openmrs/mobile/activities/providermanagerdashboard/ProviderManagerDashboardFragment.kt b/openmrs-client/src/main/java/org/openmrs/mobile/activities/providermanagerdashboard/ProviderManagerDashboardFragment.kt new file mode 100644 index 000000000..7d28a46f1 --- /dev/null +++ b/openmrs-client/src/main/java/org/openmrs/mobile/activities/providermanagerdashboard/ProviderManagerDashboardFragment.kt @@ -0,0 +1,146 @@ +/* + * The contents of this file are subject to the OpenMRS Public License + * Version 1.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://license.openmrs.org + * + * Software distributed under the License is distributed on an "AS IS" + * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the + * License for the specific language governing rights and limitations + * under the License. + * + * Copyright (C) OpenMRS, LLC. All Rights Reserved. + */ +package org.openmrs.mobile.activities.providermanagerdashboard + +import android.content.Intent +import android.os.Bundle +import android.view.LayoutInflater +import android.view.Menu +import android.view.MenuInflater +import android.view.View +import android.view.ViewGroup +import androidx.appcompat.widget.SearchView +import androidx.fragment.app.viewModels +import androidx.lifecycle.Observer +import androidx.recyclerview.widget.LinearLayoutManager +import com.openmrs.android_sdk.library.models.Provider +import com.openmrs.android_sdk.library.models.Result +import com.openmrs.android_sdk.utilities.ToastUtil +import dagger.hilt.android.AndroidEntryPoint +import org.openmrs.mobile.R +import org.openmrs.mobile.activities.BaseFragment +import org.openmrs.mobile.activities.providermanagerdashboard.addprovider.AddProviderActivity +import org.openmrs.mobile.databinding.FragmentProviderManagementBinding +import org.openmrs.mobile.utilities.makeGone +import org.openmrs.mobile.utilities.makeVisible + +@AndroidEntryPoint +class ProviderManagerDashboardFragment : BaseFragment() { + private var _binding: FragmentProviderManagementBinding? = null + private val binding: FragmentProviderManagementBinding get() = _binding!! + + private val viewModel: ProviderManagerDashboardViewModel by viewModels() + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { + _binding = FragmentProviderManagementBinding.inflate(inflater, container, false) + setHasOptionsMenu(true) + + setupAdapter() + setupObserver() + setupListeners() + fetchProviders() + + return binding.root + } + + private fun setupAdapter() = with(binding.providerManagementRecyclerView) { + setHasFixedSize(true) + layoutManager = LinearLayoutManager(requireContext()) + adapter = ProviderManagerDashboardRecyclerViewAdapter(this@ProviderManagerDashboardFragment, emptyList()) + } + + private fun setupListeners() = with(binding) { + // Swipe to refresh + swipeLayout.setOnRefreshListener { + fetchProviders() + swipeLayout.isRefreshing = false + } + + // Add provider floating action button + providerManagementFragAddFAB.setOnClickListener { + startActivity(Intent(activity, AddProviderActivity::class.java)) + } + } + + private fun setupObserver() { + viewModel.result.observe(viewLifecycleOwner, Observer { result -> + when (result) { + is Result.Loading -> showLoading() + is Result.Success -> showProvidersList(result.data) + is Result.Error -> showError() + else -> throw IllegalStateException() + } + }) + } + + private fun fetchProviders(query: String? = null) { + viewModel.fetchProviders(query) + } + + private fun showLoading() = with(binding) { + providerManagementRecyclerView.makeGone() + emptyProviderManagementList.makeGone() + providerManagementInitialProgressBar.makeVisible() + } + + private fun showProvidersList(providers: List) = with(binding) { + providerManagementInitialProgressBar.makeGone() + if (providers.isEmpty()) { + emptyProviderManagementList.text = getString(R.string.providers_fetching_no_results) + emptyProviderManagementList.makeVisible() + providerManagementRecyclerView.makeGone() + } else { + (providerManagementRecyclerView.adapter as ProviderManagerDashboardRecyclerViewAdapter) + .updateList(providers) + providerManagementRecyclerView.makeVisible() + emptyProviderManagementList.makeGone() + } + } + + private fun showError() = with(binding) { + providerManagementInitialProgressBar.makeGone() + emptyProviderManagementList.makeGone() + providerManagementRecyclerView.makeGone() + ToastUtil.error(getString(R.string.providers_fetching_error)) + } + + override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { + super.onCreateOptionsMenu(menu, inflater) + + inflater.inflate(R.menu.provider_manager_menu, menu) + val searchView = menu.findItem(R.id.actionSearchLocal).actionView as SearchView + + // Search function + searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener { + override fun onQueryTextSubmit(query: String): Boolean { + searchView.clearFocus() + return true + } + + override fun onQueryTextChange(query: String): Boolean { + fetchProviders(query) + return true + } + }) + } + + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } + + companion object { + fun newInstance(): ProviderManagerDashboardFragment = ProviderManagerDashboardFragment() + } +} diff --git a/openmrs-client/src/main/java/org/openmrs/mobile/activities/providermanagerdashboard/ProviderManagerDashboardPresenter.java b/openmrs-client/src/main/java/org/openmrs/mobile/activities/providermanagerdashboard/ProviderManagerDashboardPresenter.java deleted file mode 100644 index 6fe4e4f95..000000000 --- a/openmrs-client/src/main/java/org/openmrs/mobile/activities/providermanagerdashboard/ProviderManagerDashboardPresenter.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * The contents of this file are subject to the OpenMRS Public License - * Version 1.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://license.openmrs.org - * - * Software distributed under the License is distributed on an "AS IS" - * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the - * License for the specific language governing rights and limitations - * under the License. - * - * Copyright (C) OpenMRS, LLC. All Rights Reserved. - */ - -package org.openmrs.mobile.activities.providermanagerdashboard; - -import java.util.List; - -import rx.android.schedulers.AndroidSchedulers; - -import androidx.fragment.app.Fragment; - -import com.openmrs.android_sdk.library.api.RestApi; -import com.openmrs.android_sdk.library.api.RestServiceBuilder; -import com.openmrs.android_sdk.library.api.repository.ProviderRepository; -import com.openmrs.android_sdk.library.listeners.retrofitcallbacks.DefaultResponseCallback; -import com.openmrs.android_sdk.library.models.Provider; -import com.openmrs.android_sdk.utilities.ToastUtil; - -import org.jetbrains.annotations.NotNull; -import org.openmrs.mobile.activities.BasePresenter; - -public class ProviderManagerDashboardPresenter extends BasePresenter implements ProviderManagerDashboardContract.Presenter, DefaultResponseCallback { - private RestApi restApi; - private ProviderRepository providerRepository; - @NotNull - private final ProviderManagerDashboardContract.View providerManagerView; - - ProviderManagerDashboardPresenter(@NotNull ProviderManagerDashboardContract.View providerManagerView) { - this.providerManagerView = providerManagerView; - this.providerManagerView.setPresenter(this); - restApi = RestServiceBuilder.createService(RestApi.class); - providerRepository = new ProviderRepository(); - } - - public ProviderManagerDashboardPresenter(@NotNull ProviderManagerDashboardContract.View providerManagerView, @NotNull RestApi restApi, ProviderRepository providerRepository) { - this.providerManagerView = providerManagerView; - this.restApi = restApi; - this.providerManagerView.setPresenter(this); - this.providerRepository = providerRepository; - } - - @Override - public void getProviders(Fragment fragment) { - providerRepository.getProviders().observeOn(AndroidSchedulers.mainThread()).subscribe(this::updateViews); - } - - @Override - public void updateViews(List providerList) { - if (providerList != null && providerList.size() != 0) { - providerManagerView.updateAdapter(providerList); - providerManagerView.updateVisibility(true, null); - } else { - providerManagerView.updateVisibility(false, "No Data to display."); - } - } - - @Override - public void deleteProvider(String providerUuid) { - providerRepository.deleteProviders(providerUuid, this); - } - - @Override - public void addProvider(Provider provider) { - providerRepository.addProvider(provider, this); - } - - @Override - public void updateProvider(Provider provider) { - providerRepository.updateProvider(provider, this); - } - - @Override - public void subscribe() { - - } - - @Override - public void onErrorResponse(String errorMessage) { - ToastUtil.error(errorMessage); - } - - @Override - public void onResponse() { - providerManagerView.refreshUI(); - } -} diff --git a/openmrs-client/src/main/java/org/openmrs/mobile/activities/providermanagerdashboard/ProviderManagerDashboardRecyclerViewAdapter.java b/openmrs-client/src/main/java/org/openmrs/mobile/activities/providermanagerdashboard/ProviderManagerDashboardRecyclerViewAdapter.java index 9a7304b87..03d387c91 100644 --- a/openmrs-client/src/main/java/org/openmrs/mobile/activities/providermanagerdashboard/ProviderManagerDashboardRecyclerViewAdapter.java +++ b/openmrs-client/src/main/java/org/openmrs/mobile/activities/providermanagerdashboard/ProviderManagerDashboardRecyclerViewAdapter.java @@ -14,6 +14,8 @@ package org.openmrs.mobile.activities.providermanagerdashboard; +import java.util.List; + import android.content.Intent; import android.view.LayoutInflater; import android.view.View; @@ -25,20 +27,18 @@ import androidx.recyclerview.widget.RecyclerView; import com.openmrs.android_sdk.library.models.Provider; +import com.openmrs.android_sdk.utilities.ApplicationConstants; import org.jetbrains.annotations.NotNull; import org.openmrs.mobile.R; import org.openmrs.mobile.activities.providerdashboard.ProviderDashboardActivity; -import com.openmrs.android_sdk.utilities.ApplicationConstants; - -import java.util.List; public class ProviderManagerDashboardRecyclerViewAdapter extends RecyclerView.Adapter { private Fragment fragment; private List mItems; - public ProviderManagerDashboardRecyclerViewAdapter(Fragment fragment, ProviderManagerDashboardContract.Presenter presenter, List items) { + public ProviderManagerDashboardRecyclerViewAdapter(Fragment fragment, List items) { this.fragment = fragment; this.mItems = items; } @@ -86,7 +86,8 @@ public ProviderViewHolder(View itemView) { } } - public void setItems(List mItems) { + public void updateList(List mItems) { this.mItems = mItems; + notifyDataSetChanged(); } -} \ No newline at end of file +} diff --git a/openmrs-client/src/main/java/org/openmrs/mobile/activities/providermanagerdashboard/ProviderManagerDashboardViewModel.kt b/openmrs-client/src/main/java/org/openmrs/mobile/activities/providermanagerdashboard/ProviderManagerDashboardViewModel.kt new file mode 100644 index 000000000..02c020ef7 --- /dev/null +++ b/openmrs-client/src/main/java/org/openmrs/mobile/activities/providermanagerdashboard/ProviderManagerDashboardViewModel.kt @@ -0,0 +1,30 @@ +package org.openmrs.mobile.activities.providermanagerdashboard + +import com.openmrs.android_sdk.library.api.repository.ProviderRepository +import com.openmrs.android_sdk.library.models.Provider +import dagger.hilt.android.lifecycle.HiltViewModel +import org.openmrs.mobile.activities.BaseViewModel +import org.openmrs.mobile.utilities.FilterUtil +import rx.android.schedulers.AndroidSchedulers +import javax.inject.Inject + +@HiltViewModel +class ProviderManagerDashboardViewModel @Inject constructor( + private val providerRepository: ProviderRepository +) : BaseViewModel>() { + + fun fetchProviders(query: String? = null) { + setLoading() + addSubscription(providerRepository.getProviders() + .map { providers -> + if (query.isNullOrEmpty()) return@map providers + else return@map FilterUtil.getProvidersFilteredByQuery(providers, query) + } + .observeOn(AndroidSchedulers.mainThread()) + .subscribe( + { providers -> setContent(providers) }, + { throwable -> setError(throwable) } + ) + ) + } +} diff --git a/openmrs-client/src/main/java/org/openmrs/mobile/utilities/FilterUtil.kt b/openmrs-client/src/main/java/org/openmrs/mobile/utilities/FilterUtil.kt index 8e4b45b92..a98c3c87a 100644 --- a/openmrs-client/src/main/java/org/openmrs/mobile/utilities/FilterUtil.kt +++ b/openmrs-client/src/main/java/org/openmrs/mobile/utilities/FilterUtil.kt @@ -15,6 +15,7 @@ package org.openmrs.mobile.utilities import com.openmrs.android_sdk.library.models.Patient +import com.openmrs.android_sdk.library.models.Provider import com.openmrs.android_sdk.library.models.Visit object FilterUtil { @@ -39,6 +40,16 @@ object FilterUtil { return filteredList } + @JvmStatic + fun getProvidersFilteredByQuery(providerList: List, query: String): List { + val filteredList = ArrayList() + for (provider in providerList) { + val searchableWords = getProviderSearchableWords(provider) + if (doesAnySearchableWordFitQuery(searchableWords, query)) filteredList.add(provider) + } + return filteredList + } + @JvmStatic fun getPatientsWithActiveVisitsFilteredByQuery(visitList: List?, query: String?): List { val filteredList: MutableList = ArrayList() @@ -70,6 +81,20 @@ object FilterUtil { return searchableWords } + @JvmStatic + private fun getProviderSearchableWords(provider: Provider): List { + val providerIdentifier = provider.identifier + val fullName = provider.person?.name?.nameString + val display = provider.person?.display + + val searchableWords = ArrayList().apply { + add(providerIdentifier) + add(fullName) + add(display) + } + return searchableWords + } + @JvmStatic private fun getVisitSearchableWords(visit: Visit?): List { val visitPlace = visit?.location?.display @@ -99,4 +124,4 @@ object FilterUtil { } return false } -} \ No newline at end of file +} diff --git a/openmrs-client/src/main/res/layout/fragment_provider_management.xml b/openmrs-client/src/main/res/layout/fragment_provider_management.xml index c417800c4..a59a32d54 100644 --- a/openmrs-client/src/main/res/layout/fragment_provider_management.xml +++ b/openmrs-client/src/main/res/layout/fragment_provider_management.xml @@ -49,8 +49,7 @@ android:id="@+id/emptyProviderManagementList" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_centerInParent="true" - android:visibility="gone" /> + android:layout_gravity="center" /> - \ No newline at end of file + diff --git a/openmrs-client/src/main/res/menu/provider_manager_menu.xml b/openmrs-client/src/main/res/menu/provider_manager_menu.xml index faca34223..57d6294b4 100644 --- a/openmrs-client/src/main/res/menu/provider_manager_menu.xml +++ b/openmrs-client/src/main/res/menu/provider_manager_menu.xml @@ -3,15 +3,11 @@ xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context="org.openmrs.mobile.activities.providermanagerdashboard.ProviderManagerDashboardActivity" > - - - - - \ No newline at end of file + diff --git a/openmrs-client/src/main/res/values-hi/strings.xml b/openmrs-client/src/main/res/values-hi/strings.xml index ac6eee0c7..5cff89325 100644 --- a/openmrs-client/src/main/res/values-hi/strings.xml +++ b/openmrs-client/src/main/res/values-hi/strings.xml @@ -339,6 +339,9 @@ पुनः प्रयास करें सर्जन + कोई प्रदाता नहीं मिला + प्रदाताओं को लाने में त्रुटि + प्रदाता - रोगी संबंध diff --git a/openmrs-client/src/main/res/values/strings.xml b/openmrs-client/src/main/res/values/strings.xml index 49b523483..71a196332 100644 --- a/openmrs-client/src/main/res/values/strings.xml +++ b/openmrs-client/src/main/res/values/strings.xml @@ -343,7 +343,9 @@ Surgeon - offline providers fetched couldnt sync with the database device offline + Error fetching providers + No providers found. + offline providers fetched couldn\'t sync with the database device offline provider will be synced to the server when device gets connected to network updated provider will be synced to the server when device gets connected to network Provider will be removed from the server when you\'re back online diff --git a/openmrs-client/src/test/java/org/openmrs/mobile/test/presenters/ProviderManagerDashboardPresenterTest.java b/openmrs-client/src/test/java/org/openmrs/mobile/test/presenters/ProviderManagerDashboardPresenterTest.java deleted file mode 100644 index 8193c69d6..000000000 --- a/openmrs-client/src/test/java/org/openmrs/mobile/test/presenters/ProviderManagerDashboardPresenterTest.java +++ /dev/null @@ -1,148 +0,0 @@ -/* - * The contents of this file are subject to the OpenMRS Public License - * Version 1.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://license.openmrs.org - * - * Software distributed under the License is distributed on an "AS IS" - * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the - * License for the specific language governing rights and limitations - * under the License. - * - * Copyright (C) OpenMRS, LLC. All Rights Reserved. - */ - -package org.openmrs.mobile.test.presenters; - -import static org.mockito.Mockito.RETURNS_MOCKS; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import androidx.arch.core.executor.testing.InstantTaskExecutorRule; -import androidx.fragment.app.Fragment; -import androidx.lifecycle.MutableLiveData; -import androidx.lifecycle.Observer; - -import com.openmrs.android_sdk.library.OpenMRSLogger; -import com.openmrs.android_sdk.library.OpenmrsAndroid; -import com.openmrs.android_sdk.library.api.RestApi; -import com.openmrs.android_sdk.library.api.repository.ProviderRepository; -import com.openmrs.android_sdk.library.dao.ProviderRoomDAO; -import com.openmrs.android_sdk.library.models.Provider; -import com.openmrs.android_sdk.utilities.NetworkUtils; -import com.openmrs.android_sdk.utilities.ToastUtil; - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.openmrs.mobile.activities.providermanagerdashboard.ProviderManagerDashboardContract; -import org.openmrs.mobile.activities.providermanagerdashboard.ProviderManagerDashboardPresenter; -import org.openmrs.mobile.application.OpenMRS; -import org.openmrs.mobile.test.ACUnitTestBase; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; - -@PrepareForTest({NetworkUtils.class, ToastUtil.class, OpenMRS.class, OpenMRSLogger.class,OpenmrsAndroid.class}) -public class ProviderManagerDashboardPresenterTest extends ACUnitTestBase { - @Rule - public InstantTaskExecutorRule taskExecutorRule = new InstantTaskExecutorRule(); - @Mock - private RestApi restApi; - @Mock - private ProviderManagerDashboardContract.View providerManagerView; - @Mock - private Observer> observer; - @Mock - private OpenMRSLogger openMRSLogger; - @Mock - private OpenMRS openMRS; - MutableLiveData> providerLiveData = Mockito.mock(MutableLiveData.class); - private ProviderManagerDashboardPresenter providerManagerDashboardPresenter; - private ProviderRepository providerRepository; - private Fragment fragment = new Fragment(); - List providerList; - Provider providerOne = createProvider(1l, "doctor"); - Provider providerTwo = createProvider(2l, "nurse"); - - @Before - public void setUp() { - providerList = Arrays.asList(providerOne, providerTwo); - providerLiveData.postValue(providerList); - - this.providerRepository = new ProviderRepository(restApi, openMRSLogger); - ProviderRoomDAO providerRoomDao = Mockito.mock(ProviderRoomDAO.class, RETURNS_MOCKS); - ProviderRoomDAO spyProviderRoomDao = spy(providerRoomDao); - - Single listSingle = Mockito.mock(Single.class); - doNothing().when(spyProviderRoomDao).updateProviderByUuid(Mockito.anyString(), Mockito.anyLong(), Mockito.any(), Mockito.anyString(), Mockito.anyString()); - when(spyProviderRoomDao.getProviderList()).thenReturn(listSingle); - when(listSingle.blockingGet()).thenReturn(providerList); - - this.providerRepository.setProviderRoomDao(spyProviderRoomDao); - - this.providerManagerDashboardPresenter = new ProviderManagerDashboardPresenter(providerManagerView, restApi, providerRepository); - mockStaticMethods(); - } - - @Test - public void shouldGetProviders_AllOK() { - - Provider providerOne = createProvider(1l, "doctor"); - Provider providerTwo = createProvider(2l, "nurse"); - providerList = Arrays.asList(providerOne, providerTwo); - providerLiveData.postValue(providerList); - - when(NetworkUtils.isOnline()).thenReturn(true); - when(restApi.getProviderList()).thenReturn(mockSuccessCall(providerList)); - - providerManagerDashboardPresenter.updateViews(providerList); - //providerRepository.getProviders().observeForever(observer); - - verify(restApi).getProviderList(); - verify(providerManagerView).updateAdapter(providerList); - verify(providerManagerView).updateVisibility(true, null); - } - - @Test - public void shouldGetProviders_Error() { - Mockito.lenient().when(NetworkUtils.isOnline()).thenReturn(true); - Mockito.lenient().when(restApi.getProviderList()).thenReturn(mockErrorCall(401)); - - providerManagerDashboardPresenter.getProviders(fragment); - verify(restApi).getProviderList(); - } - - @Test - public void shouldGetProviders_EmptyList() { - List providerList = new ArrayList<>(); - providerLiveData.postValue(providerList); - - Mockito.lenient().when(NetworkUtils.isOnline()).thenReturn(true); - when(restApi.getProviderList()).thenReturn(mockSuccessCall(providerList)); - providerManagerDashboardPresenter.getProviders(fragment); - providerManagerDashboardPresenter.updateViews(providerList); - verify(restApi).getProviderList(); - verify(providerManagerView).updateVisibility(false, "No Data to display."); - } - - private void mockStaticMethods() { - PowerMockito.mockStatic(NetworkUtils.class); - PowerMockito.mockStatic(OpenMRS.class); - PowerMockito.mockStatic(OpenMRSLogger.class); - PowerMockito.mockStatic(OpenmrsAndroid.class); - Mockito.lenient().when(OpenMRS.getInstance()).thenReturn(openMRS); - PowerMockito.when(OpenmrsAndroid.getOpenMRSLogger()).thenReturn(openMRSLogger); - PowerMockito.mockStatic(ToastUtil.class); - } - - public abstract class Single extends io.reactivex.Single { - } -} diff --git a/openmrs-client/src/test/java/org/openmrs/mobile/test/viewmodels/ProviderManagerDashboardViewModelTest.kt b/openmrs-client/src/test/java/org/openmrs/mobile/test/viewmodels/ProviderManagerDashboardViewModelTest.kt new file mode 100644 index 000000000..d44bbee23 --- /dev/null +++ b/openmrs-client/src/test/java/org/openmrs/mobile/test/viewmodels/ProviderManagerDashboardViewModelTest.kt @@ -0,0 +1,77 @@ +package org.openmrs.mobile.test.viewmodels + +import androidx.arch.core.executor.testing.InstantTaskExecutorRule +import com.openmrs.android_sdk.library.api.repository.ProviderRepository +import com.openmrs.android_sdk.library.models.Provider +import com.openmrs.android_sdk.library.models.Result +import org.junit.Assert.assertEquals +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.jupiter.api.Assertions.assertIterableEquals +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 +import org.mockito.Mock +import org.mockito.Mockito.`when` +import org.openmrs.mobile.activities.providermanagerdashboard.ProviderManagerDashboardViewModel +import org.openmrs.mobile.test.ACUnitTestBaseRx +import rx.Observable + +@RunWith(JUnit4::class) +class ProviderManagerDashboardViewModelTest : ACUnitTestBaseRx() { + + @get:Rule + val instantTaskExecutorRule = InstantTaskExecutorRule() + + @Mock + lateinit var providerRepository: ProviderRepository + + lateinit var viewModel: ProviderManagerDashboardViewModel + + @Before + override fun setUp() { + super.setUp() + viewModel = ProviderManagerDashboardViewModel(providerRepository) + } + + @Test + fun `fetch providers should succeed`() { + val providers = listOf( + createProvider(1L, "doctor"), + createProvider(2L, "nurse") + ) + `when`(providerRepository.getProviders()).thenReturn(Observable.just(providers)) + + viewModel.fetchProviders() + val result = viewModel.result.value as Result.Success> + + assertIterableEquals(providers, result.data) + } + + @Test + fun `fetch providers should return error`() { + val errorMsg = "Error message!" + val throwable = Throwable(errorMsg) + `when`(providerRepository.getProviders()).thenReturn(Observable.error(throwable)) + + viewModel.fetchProviders() + val result = (viewModel.result.value as Result.Error).throwable.message + + assertEquals(errorMsg, result) + } + + @Test + fun `fetch providers by query name should succeed`() { + val providers = listOf( + createProvider(1L, "Doctor"), + createProvider(2L, "Nurse") + ) + val filteredPatients = listOf(providers[0]) + `when`(providerRepository.getProviders()).thenReturn(Observable.just(providers)) + + viewModel.fetchProviders("doctor") + + val result = viewModel.result.value as Result.Success> + assertIterableEquals(filteredPatients, result.data) + } +}