Skip to content

Commit

Permalink
Implement field input template for symptom, HomeVisit.detail, HomeVis…
Browse files Browse the repository at this point in the history
…it.result
  • Loading branch information
piruin authored and porntipa committed Dec 27, 2018
1 parent dff3de2 commit ea3dce8
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 12 deletions.
4 changes: 2 additions & 2 deletions ffc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ dependencies {
implementation 'com.fatboyindustrial.gson-jodatime-serialisers:gson-jodatime-serialisers:1.6.0'
implementation 'com.github.bumptech.glide:glide:4.7.1'
implementation "com.github.chrisbanes:PhotoView:2.1.3"
implementation 'com.github.ffc-nectec:entities:1.0.0-alpha13'
implementation 'com.github.ffc-nectec:entities:61ebf124c8'
implementation 'com.github.ffc-nectec.genogram:genogram-android:1.0.0-alpha4'
implementation 'com.github.piruin:AsymmetricGridView:7faa393d0e'
implementation 'com.github.piruin.geok:geok-gson:1.0.0-alpha-2'
implementation 'com.github.piruin.geok:geok-gson:1.0.0'
implementation 'com.github.piruin:retrofit-dsl:1.2'
implementation 'com.github.santalu:emptyview:1.3.6'
implementation 'com.github.sembozdemir:PermissionsKt:1.0.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,24 @@ package ffc.app.health.service.community

import android.arch.lifecycle.MutableLiveData
import android.os.Bundle
import android.support.design.widget.TextInputEditText
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import ffc.android.check
import ffc.android.observe
import ffc.android.onLongClick
import ffc.android.viewModel
import ffc.app.R
import ffc.app.familyFolderActivity
import ffc.app.health.service.HealthCareServivceForm
import ffc.app.util.SimpleViewModel
import ffc.app.util.datetime.th_TH
import ffc.app.util.datetime.toCalendar
import ffc.app.util.datetime.toLocalDate
import ffc.app.util.setInto
import ffc.entity.Template
import ffc.entity.healthcare.CommunityService
import ffc.entity.healthcare.HealthCareService
import ffc.entity.healthcare.HomeVisit
Expand All @@ -42,13 +46,15 @@ import kotlinx.android.synthetic.main.hs_homevisit_from_fragment.planField
import kotlinx.android.synthetic.main.hs_homevisit_from_fragment.resultField
import kotlinx.android.synthetic.main.hs_homevisit_from_fragment.syntomField
import me.piruin.spinney.Spinney
import me.piruin.spinney.SpinneyAdapter
import me.piruin.spinney.SpinneyDialog
import org.jetbrains.anko.support.v4.find
import org.jetbrains.anko.support.v4.toast

internal class HomeVisitFormFragment : Fragment(), HealthCareServivceForm<HealthCareService> {

val communityServicesField by lazy { find<Spinney<CommunityService.ServiceType>>(R.id.communityServiceField) }
val serviceTypeViewModel by lazy { viewModel<ServicesTypeViewModel>() }
val viewModel by lazy { viewModel<ServicesTypeViewModel>() }

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.hs_homevisit_from_fragment, container, false)
Expand All @@ -63,30 +69,62 @@ internal class HomeVisitFormFragment : Fragment(), HealthCareServivceForm<Health
appointField.setUndefinedAsDefault()
}

observe(serviceTypeViewModel.content) {
observe(viewModel.content) {
if (!it.isNullOrEmpty()) {
communityServicesField.setSearchableItem(it)
serviceTypeViewModel.bindItem.value?.let { communityServicesField.selectedItem = it }
viewModel.bindItem.value?.let { communityServicesField.selectedItem = it }
}
}
observe(serviceTypeViewModel.exception) { toast(it?.message ?: "What happend") }
observe(serviceTypeViewModel.bindItem) {
if (!serviceTypeViewModel.content.value.isNullOrEmpty()) {
observe(viewModel.exception) { toast(it?.message ?: "What happend") }
observe(viewModel.bindItem) {
if (!viewModel.content.value.isNullOrEmpty()) {
communityServicesField.selectedItem = it
}
}
observe(viewModel.templates) { templates ->
if (!templates.isNullOrEmpty()) {
syntomField.setTemplateAdapter(
templates.filter { it.field == "HealthCareService.syntom" }
)
detailField.setTemplateAdapter(
templates.filter { it.field == "HomeVisit.detail" }
)
resultField.setTemplateAdapter(
templates.filter { it.field == "HomeVisit.result" }
)
}
}

communityServiceTypes(context!!).all {
onFound { serviceTypeViewModel.content.value = it }
onNotFound { serviceTypeViewModel.content.value = listOf() }
onFail { serviceTypeViewModel.exception.value = it }
onFound { viewModel.content.value = it }
onNotFound { viewModel.content.value = listOf() }
onFail { viewModel.exception.value = it }
}

templatesOf(familyFolderActivity.org!!).all {
onFound { viewModel.templates.value = it }
onNotFound { viewModel.templates.value = listOf() }
onFail { viewModel.exception.value = it }
}
}

private fun TextInputEditText.setTemplateAdapter(template: List<Template>) {
onLongClick { view ->
val dialog = SpinneyDialog(view.context)
dialog.setAdapter(SpinneyAdapter(view.context, template.map { it.value }))
dialog.setOnItemSelectedListener { item, _ ->
view.setText(item as String)
true
}
dialog.show()
true
}
}

override fun bind(service: HealthCareService) {
service.communityServices.firstOrNull { it is HomeVisit }?.let {
it as HomeVisit
serviceTypeViewModel.bindItem.value = it.serviceType
viewModel.bindItem.value = it.serviceType
it.detail.setInto(detailField)
it.result.setInto(resultField)
it.plan.setInto(planField)
Expand Down Expand Up @@ -122,5 +160,7 @@ internal class HomeVisitFormFragment : Fragment(), HealthCareServivceForm<Health

class ServicesTypeViewModel : SimpleViewModel<List<CommunityService.ServiceType>>() {
val bindItem = MutableLiveData<CommunityService.ServiceType>()

val templates = MutableLiveData<List<Template>>()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package ffc.app.health.service.community

import ffc.api.ApiErrorException
import ffc.api.FfcCentral
import ffc.app.mockRepository
import ffc.app.util.RepoCallback
import ffc.entity.Organization
import ffc.entity.Template
import retrofit2.Call
import retrofit2.dsl.enqueue
import retrofit2.http.GET
import retrofit2.http.Path

interface TemplateApi {

@GET("org/{orgId}/template")
fun getAll(@Path("orgId") orgId: String): Call<List<Template>>
}

interface Templates {

fun all(callbackDsl: RepoCallback<List<Template>>.() -> Unit)
}

internal fun templatesOf(org: Organization): Templates = if (mockRepository) DummyTemplate() else ApiTemplates(org.id)

private class ApiTemplates(val orgId: String) : Templates {

private val api by lazy { FfcCentral().service<TemplateApi>() }

override fun all(callbackDsl: RepoCallback<List<Template>>.() -> Unit) {
val callback = RepoCallback<List<Template>>().apply(callbackDsl)

api.getAll(orgId).enqueue {
onSuccess {
callback.onFound!!.invoke(body()!!)
}

onError {
if (code() == 404)
callback.onNotFound?.invoke()
else
callback.onFail!!.invoke(ApiErrorException(this))
}
onFailure { callback.onFail!!.invoke(it) }
}
}
}

private class DummyTemplate() : Templates {

override fun all(callbackDsl: RepoCallback<List<Template>>.() -> Unit) {
val callback = RepoCallback<List<Template>>().apply(callbackDsl)
callback.onFound!!.invoke(listOf(
Template("Hello", "HealthCareService.syntom"),
Template("World", "HealthCareService.syntom"),
Template("FFC", "HealthCareService.syntom")
))
}
}

0 comments on commit ea3dce8

Please sign in to comment.