Skip to content

Commit

Permalink
Context care API
Browse files Browse the repository at this point in the history
  • Loading branch information
RawatH committed Sep 29, 2023
1 parent 36c2896 commit 6387017
Show file tree
Hide file tree
Showing 22 changed files with 1,104 additions and 102 deletions.
8 changes: 4 additions & 4 deletions app/src/main/java/org/commcare/dalvik/abha/di/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ object AppModule {
fun provideOkHttpClient(httpLoggingInterceptor: HttpLoggingInterceptor): OkHttpClient {
return OkHttpClient.Builder()
.addInterceptor(httpLoggingInterceptor)
.connectTimeout(2, TimeUnit.MINUTES)
.readTimeout(1, TimeUnit.MINUTES)
.writeTimeout(1, TimeUnit.MINUTES)
// .connectionPool(ConnectionPool(0, 5, TimeUnit.MINUTES))
.connectTimeout(3, TimeUnit.MINUTES)
.readTimeout(2, TimeUnit.MINUTES)
.writeTimeout(2, TimeUnit.MINUTES)


.addInterceptor { chain ->
if (chain.request().url.host.contains("raw.githubusercontent.com")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
package org.commcare.dalvik.abha.model

import org.commcare.dalvik.domain.model.CCAuthModesRequestModel
import org.commcare.dalvik.domain.model.CCLinkModel
import org.commcare.dalvik.domain.model.CCPatientDetails
import org.commcare.dalvik.domain.model.CCRequesterModel

class LinkCareContextModel {
lateinit var patientAbhaId: String
lateinit var hipId: String
lateinit var patient: PatientDetail
lateinit var purpose: String
lateinit var patient: CCPatientDetails

}

data class PatientDetail(var referenceNumber:String,var display:String,var careContexts:List<CareContextDetail>)
fun getAuthModesRequestModel(authMode:String? = null) = try {
CCAuthModesRequestModel(patientAbhaId, purpose, authMode, CCRequesterModel(id = hipId))
} catch (e: Exception) {
null
}

fun getCCLinkRequestModel(accessToken:String):CCLinkModel{
return CCLinkModel(accessToken,hipId,patient)
}

}

data class CareContextDetail(var referenceNumber: String,var display:String, var hiTypes:List<String>)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.WindowManager
import androidx.activity.OnBackPressedCallback
import androidx.activity.viewModels
import androidx.core.os.bundleOf
Expand All @@ -26,6 +25,7 @@ import org.commcare.dalvik.abha.databinding.AbdmActivityBinding
import org.commcare.dalvik.abha.utility.DialogType
import org.commcare.dalvik.abha.utility.DialogUtility
import org.commcare.dalvik.abha.viewmodel.AbdmViewModel
import org.commcare.dalvik.abha.viewmodel.CareContextViewModel
import org.commcare.dalvik.abha.viewmodel.GenerateAbhaUiState
import org.commcare.dalvik.abha.viewmodel.PatientViewModel
import org.commcare.dalvik.data.network.HeaderInterceptor
Expand All @@ -42,6 +42,7 @@ class AbdmActivity : BaseActivity<AbdmActivityBinding>(AbdmActivityBinding::infl
private lateinit var navHostFragment: NavHostFragment
val viewmodel: AbdmViewModel by viewModels()
val patientViewModel: PatientViewModel by viewModels()
val careContextViewModel: CareContextViewModel by viewModels()

private var showMenu = true

Expand Down Expand Up @@ -74,6 +75,7 @@ class AbdmActivity : BaseActivity<AbdmActivityBinding>(AbdmActivityBinding::infl

observeLoader()
observerPatientViewModel()
observerCCViewModel()
observeBlockedOtpRequest()

intent.extras?.getString("lang_code")?.let { langId ->
Expand Down Expand Up @@ -215,8 +217,20 @@ class AbdmActivity : BaseActivity<AbdmActivityBinding>(AbdmActivityBinding::infl
DialogType.Blocking
)
}
}

careContextViewModel.otpRequestBlocked.asFlow().collect { otpBlockedRequest ->
otpBlockedRequest?.let {
DialogUtility.showDialog(
this@AbdmActivity,
resources.getString(R.string.app_blocked, it.getTimeLeftToUnblock()),
{ dispatchResult(getErrorIntent("Blocked ,multiple OTP requested.")) },
DialogType.Blocking
)
}
}


}
}

Expand All @@ -236,6 +250,23 @@ class AbdmActivity : BaseActivity<AbdmActivityBinding>(AbdmActivityBinding::infl
}
}
}

private fun observerCCViewModel(){
lifecycleScope.launch(Dispatchers.Main) {
repeatOnLifecycle(Lifecycle.State.STARTED) {
careContextViewModel.uiState.collect {
when (it) {
is GenerateAbhaUiState.Loading -> {
Timber.d("LOADER VISIBILITY ${it.isLoading}")
binding.loader.visibility =
if (it.isLoading) View.VISIBLE else View.GONE
}
else -> false
}
}
}
}
}
private fun observeLoader() {
lifecycleScope.launch(Dispatchers.Main) {
repeatOnLifecycle(Lifecycle.State.STARTED) {
Expand Down Expand Up @@ -299,7 +330,7 @@ class AbdmActivity : BaseActivity<AbdmActivityBinding>(AbdmActivityBinding::infl
}

ACTION_CARE_CONTEXT_LINK ->{

R.navigation.link_care_context
}
else -> {
-1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class TestActivity : AppCompatActivity() {
val REQ_CODE_B = 101
val REQ_CODE_C = 102
val REQ_CODE_D = 103
val REQ_CODE_E = 104

val action = "org.commcare.dalvik.abha.abdm.app"

Expand Down Expand Up @@ -57,14 +58,15 @@ class TestActivity : AppCompatActivity() {

}

val lang = "hi"
val lang = "en"
private val token = "0c0a3fbbacc0922192a1b4e63be5d6f511790a31"

private fun startIntentA() {
val intent = Intent(action).apply {
putExtras(
bundleOf(
"abha_id" to "",
"mobile_number" to "9560833229",
"abha_id" to "66543673407500",
"lang_code" to lang,
"abdm_api_token" to token,
"action" to ACTION_CREATE_ABHA
Expand Down Expand Up @@ -123,41 +125,40 @@ class TestActivity : AppCompatActivity() {

private fun linkCareContext(){
val intent = Intent(action).apply {
val patientJson = JSONObject().apply {
put("referenceNumber","Test_001")
put("display","Ajeet Test_001")


val careContext= JSONObject().apply {
put("referenceNumber","CC_505")
put("display","Visit for fever 505")
val hiTypes= JSONArray().apply {
put("Prescription")
}
put("hiTypes",hiTypes)
}


val careContextArr= JSONArray().apply {
put(careContext)
}
put("careContexts",careContextArr)

}
putExtras(
bundleOf(
"abhaId" to "ajeet2040@sbx",
"abhaId" to "91766261606756@sbx",
"purpose" to "LINK",
"requester" to JSONObject().apply {
put("type","HIP")
put("hipId","6004")
},
"patientDetail" to JSONObject().apply {
put("referenceNumber","Test_001")
put("display","Ajeet Test_001")


val careContext= JSONObject().apply {
put("referenceNumber","CC_505")
put("display","Visit for fever 505")
val hiTypes= JSONArray().apply {
put("Prescription")
}
put("hiTypes",hiTypes)
}


val careContextArr= JSONArray().apply {
put(careContext)
}
put("careContexts",careContext)

},
"hipId" to "6004",
"patientDetail" to patientJson.toString(),
"lang_code" to lang,
"action" to ACTION_CARE_CONTEXT_LINK
"action" to ACTION_CARE_CONTEXT_LINK,
"abdm_api_token" to token,
)
)
}
startActivityForResult(intent, REQ_CODE_D)
startActivityForResult(intent, REQ_CODE_E)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,152 @@ package org.commcare.dalvik.abha.ui.main.fragment

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.fragment.findNavController
import com.google.android.material.textfield.MaterialAutoCompleteTextView
import com.google.gson.Gson
import kotlinx.coroutines.launch
import org.commcare.dalvik.abha.R
import org.commcare.dalvik.abha.databinding.CCAuthModeBinding
import org.commcare.dalvik.abha.model.LinkCareContextModel
import org.commcare.dalvik.abha.ui.main.activity.AbdmActivity
import org.commcare.dalvik.abha.viewmodel.CareContextViewModel
import org.commcare.dalvik.abha.viewmodel.GenerateAbhaUiState
import org.commcare.dalvik.domain.model.CCPatientDetails
import timber.log.Timber

class CCFetchAuthModeFragment : BaseFragment<CCAuthModeBinding>(CCAuthModeBinding::inflate) {
class CCFetchAuthModeFragment : BaseFragment<CCAuthModeBinding>(CCAuthModeBinding::inflate),
AdapterView.OnItemClickListener {

val viewModel: CareContextViewModel by activityViewModels()

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.authModesList.clear()
observeUiState()
populateIntentData()
fetchAuthMethods()
}

private fun populateIntentData() {
arguments?.let {
val linkCareContextModel = LinkCareContextModel()
it.getString("abhaId")?.let {
linkCareContextModel.patientAbhaId = it
}

it.getString("purpose")?.let {
linkCareContextModel.purpose = it
}

it.getString("hipId")?.let {
linkCareContextModel.hipId = it
}

it.getString("patientDetail")?.let {
linkCareContextModel.patient = Gson().fromJson(
it,
CCPatientDetails::class.java
)
}

viewModel.init(linkCareContextModel)
binding.clickHandler = this
binding.reqModel = linkCareContextModel
}
}

private fun fetchAuthMethods() {
if (hasNetworkConnectivity()) {
viewModel.fetchCareContextAuthModes()
}
}


private fun setAuthModeAdapter() {
val adapter =
ArrayAdapter(
requireContext(),
R.layout.dropdown_item,
viewModel.authModesList
)
(binding.ccAuthMode as? MaterialAutoCompleteTextView)?.apply {

setAdapter(adapter)
setOnItemClickListener(this@CCFetchAuthModeFragment)
if (viewModel.authModesList.size == 1) {
setText(adapter.getItem(0).toString(), false)
}
}
}

private fun observeUiState() {
viewLifecycleOwner.lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.uiState.collect {
when (it) {

is GenerateAbhaUiState.Success -> {
viewModel.uiState.emit(GenerateAbhaUiState.Loading(false))

try {
it.data.getAsJsonArray("modes")?.forEach { modeName ->
if ("DEMOGRAPHICS" != modeName.asString) {
viewModel.authModesList.add(modeName.asString)
}
}
viewModel.authModesList.sort()

setAuthModeAdapter()

} catch (e: Exception) {
(activity as AbdmActivity).showMessageAndDispatchResult("No authentication methods received.")
Timber.d("Auth methods not received")
}

}

is GenerateAbhaUiState.AbdmError -> {
viewModel.uiState.emit(GenerateAbhaUiState.Loading(false))
(activity as AbdmActivity).showBlockerDialog(it.data.message)

}

is GenerateAbhaUiState.Error -> {
(activity as AbdmActivity).showBlockerDialog(it.data.get("message").asString)
viewModel.uiState.emit(GenerateAbhaUiState.Loading(false))
}

else -> {
//exhaustive block
}

}
}
}
}
}

override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
parent?.getItemAtPosition(position).toString().let {
viewModel.selectedAuthMethod = it
binding.ccGenerateAuthOtp.isEnabled = true
}
}

override fun onClick(view: View?) {
super.onClick(view)
when (view?.id) {
R.id.ccGenerateAuthOtp -> {
findNavController().navigate(
R.id.action_CCFetchAuthModeFragment_to_verifyCCLinkOtpFragment
)
}
}
}
}
Loading

0 comments on commit 6387017

Please sign in to comment.