Skip to content

Commit

Permalink
AC-1050: Migrate login package from MVP to MVVM architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
amrsalah3 committed Oct 29, 2022
1 parent 74ac364 commit 09ac22c
Show file tree
Hide file tree
Showing 19 changed files with 1,094 additions and 1,208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,19 @@
package com.openmrs.android_sdk.library.api.repository

import com.openmrs.android_sdk.library.OpenmrsAndroid
import com.openmrs.android_sdk.library.databases.AppDatabaseHelper.createObservableIO
import com.openmrs.android_sdk.library.databases.entities.LocationEntity
import com.openmrs.android_sdk.utilities.ApplicationConstants
import rx.Observable
import javax.inject.Inject
import javax.inject.Singleton
import java.util.concurrent.Callable

/**
* The type Location repository.
*/
class LocationRepository : BaseRepository() {
@Singleton
class LocationRepository @Inject constructor() : BaseRepository() {
/**
* Gets location (only has uuid).
*
Expand All @@ -37,4 +44,20 @@ class LocationRepository : BaseRepository() {
}
return null
}

/**
* Fetches all locations registered in a server.
*
* @param url the URL of the server to fetch the locations from
* @return observable list of LocationEntity
*/
fun getLocations(url: String): Observable<List<LocationEntity>> {
return createObservableIO(Callable {
val locationEndPoint = url + ApplicationConstants.API.REST_ENDPOINT + "location"
restApi.getLocations(locationEndPoint, "Login Location", "full").execute().run {
if (isSuccessful && body() != null) return@Callable body()!!.results
else throw Exception("Error fetching concepts: ${message()}")
}
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.openmrs.android_sdk.library.api.repository

import com.openmrs.android_sdk.library.api.RestApi
import com.openmrs.android_sdk.library.api.RestServiceBuilder
import com.openmrs.android_sdk.library.databases.AppDatabaseHelper.createObservableIO
import com.openmrs.android_sdk.library.models.Session
import rx.Observable
import javax.inject.Inject
import javax.inject.Singleton
import java.util.concurrent.Callable

@Singleton
class LoginRepository @Inject constructor() : BaseRepository() {

/**
* Gets an authenticated session by username and password.
*
* @param username authenticated username
* @param password authenticated password
* @return observable session
*/
fun getSession(username: String, password: String): Observable<Session> {
return createObservableIO(Callable {
restApi = RestServiceBuilder.createService(RestApi::class.java, username, password)
restApi.getSession().execute().run {
if (isSuccessful && body() != null) return@Callable body()!!
else throw Exception("Error fetching session: ${message()}")
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import rx.Observable;

Expand All @@ -37,7 +36,6 @@
import com.openmrs.android_sdk.library.dao.LocationDAO;
import com.openmrs.android_sdk.library.dao.VisitDAO;
import com.openmrs.android_sdk.library.databases.AppDatabaseHelper;
import com.openmrs.android_sdk.library.listeners.retrofitcallbacks.GetVisitTypeCallback;
import com.openmrs.android_sdk.library.models.Encounter;
import com.openmrs.android_sdk.library.models.Encountercreate;
import com.openmrs.android_sdk.library.models.Patient;
Expand Down Expand Up @@ -109,28 +107,16 @@ public Observable<List<Visit>> syncVisitsData(@NonNull final Patient patient) {
}

/**
* This method is used for getting visitType asynchronously .
* This method is used for fetching VisitType asynchronously.
*
* @param callbackListener
* @return Observable VisitType object or null
* @see VisitType
* @see GetVisitTypeCallback
*/
public void getVisitType(final GetVisitTypeCallback callbackListener) {
Call<Results<VisitType>> call = restApi.getVisitType();
call.enqueue(new Callback<Results<VisitType>>() {
@Override
public void onResponse(@NonNull Call<Results<VisitType>> call, @NonNull Response<Results<VisitType>> response) {
if (response.isSuccessful()) {
callbackListener.onGetVisitTypeResponse(response.body().getResults().get(0));
} else {
callbackListener.onErrorResponse(response.message());
}
}

@Override
public void onFailure(@NonNull Call<Results<VisitType>> call, @NonNull Throwable t) {
callbackListener.onErrorResponse(t.getMessage());
}
public Observable<VisitType> getVisitType() {
return AppDatabaseHelper.createObservableIO(() -> {
Response<Results<VisitType>> response = restApi.getVisitType().execute();
if (response.isSuccessful()) return response.body().getResults().get(0);
else return null;
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,29 @@

package com.openmrs.android_sdk.library.dao;

import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.ArrayList;
import java.util.List;

import rx.Observable;

import com.openmrs.android_sdk.library.OpenmrsAndroid;
import com.openmrs.android_sdk.library.databases.AppDatabase;
import com.openmrs.android_sdk.library.databases.AppDatabaseHelper;
import com.openmrs.android_sdk.library.databases.entities.LocationEntity;
import com.openmrs.android_sdk.utilities.StringUtils;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;


/**
* The type Location dao.
*/
@Singleton
public class LocationDAO {

@Inject
public LocationDAO() { }

/**
* The Location room dao.
*/
Expand Down Expand Up @@ -101,4 +108,4 @@ public LocationEntity findLocationByUUID(String uuid) {
}
}

}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ enum class OperationType {
PatientDeleting,
LastViewedPatientsFetching,
ProviderRegistering,
ProviderUpdating
ProviderUpdating,
Login,
LocationsFetching
}

enum class ResultType {
Expand All @@ -44,4 +46,12 @@ enum class ResultType {
ProviderDeletionSuccess,
ProviderDeletionLocalSuccess,
ProviderDeletionError,
LoginOfflineSuccess,
LoginSuccess,
LoginInvalidCredentials,
LoginOfflineUnsupported,
LoginNoInternetConnection,
LocationsFetchingLocalSuccess,
LocationsFetchingSuccess,
LocationsFetchingNoInternetConnection
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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.login

import android.content.Intent
import android.os.Bundle
import android.view.Menu
import dagger.hilt.android.AndroidEntryPoint
import org.openmrs.mobile.R
import org.openmrs.mobile.activities.ACBaseActivity

@AndroidEntryPoint
class LoginActivity : ACBaseActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)

supportActionBar?.run {
elevation = 0f
setTitle(R.string.app_name)
}

// Create fragment
var loginFragment = supportFragmentManager.findFragmentById(R.id.loginContentFrame) as LoginFragment?
if (loginFragment == null) {
loginFragment = LoginFragment.newInstance()
}
if (!loginFragment.isActive) {
addFragmentToActivity(supportFragmentManager, loginFragment, R.id.loginContentFrame)
}
}

override fun onBackPressed() {
val intent = Intent(Intent.ACTION_MAIN)
intent.addCategory(Intent.CATEGORY_HOME)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
startActivity(intent)
}

override fun onCreateOptionsMenu(menu: Menu): Boolean = true
}
Loading

0 comments on commit 09ac22c

Please sign in to comment.