Add school quick actions (#570)

This commit is contained in:
Dominik Korsa
2019-10-27 00:36:39 +02:00
committed by Rafał Borcz
parent 125a010f03
commit a71a183160
9 changed files with 147 additions and 39 deletions

View File

@ -10,6 +10,8 @@ import io.github.wulkanowy.ui.base.BaseFragment
import io.github.wulkanowy.ui.modules.main.MainView
import io.github.wulkanowy.ui.modules.schoolandteachers.SchoolAndTeachersChildView
import io.github.wulkanowy.ui.modules.schoolandteachers.SchoolAndTeachersFragment
import io.github.wulkanowy.utils.dialPhone
import io.github.wulkanowy.utils.openMapLocation
import kotlinx.android.synthetic.main.fragment_school.*
import javax.inject.Inject
@ -35,12 +37,17 @@ class SchoolFragment : BaseFragment(), SchoolView, MainView.TitledView, SchoolAn
override fun initView() {
schoolSwipe.setOnRefreshListener { presenter.onSwipeRefresh() }
schoolAddressButton.setOnClickListener { presenter.onAddressSelected() }
schoolTelephoneButton.setOnClickListener { presenter.onTelephoneSelected() }
}
override fun updateData(data: School) {
schoolName.text = data.name
schoolAddress.text = data.address.ifBlank { "-" }
schoolAddressButton.visibility = if (data.address.isNotBlank()) View.VISIBLE else View.GONE
schoolTelephone.text = data.contact.ifBlank { "-" }
schoolTelephoneButton.visibility = if (data.contact.isNotBlank()) View.VISIBLE else View.GONE
schoolHeadmaster.text = data.headmaster
schoolPedagogue.text = data.pedagogue
}
@ -77,4 +84,12 @@ class SchoolFragment : BaseFragment(), SchoolView, MainView.TitledView, SchoolAn
presenter.onDetachView()
super.onDestroyView()
}
override fun openMapsLocation(location: String) {
context?.openMapLocation(location)
}
override fun dialPhone(phone: String) {
context?.dialPhone(phone)
}
}

View File

@ -19,6 +19,10 @@ class SchoolPresenter @Inject constructor(
private val analytics: FirebaseAnalyticsHelper
) : BasePresenter<SchoolView>(errorHandler, studentRepository, schedulers) {
private var address: String? = null
private var contact: String? = null
override fun onAttachView(view: SchoolView) {
super.onAttachView(view)
view.initView()
@ -34,6 +38,14 @@ class SchoolPresenter @Inject constructor(
loadData(forceRefresh)
}
fun onAddressSelected() {
address?.let{ view?.openMapsLocation(it) }
}
fun onTelephoneSelected() {
contact?.let { view?.dialPhone(it) }
}
private fun loadData(forceRefresh: Boolean = false) {
Timber.i("Loading school info started")
disposable.add(studentRepository.getCurrentStudent()
@ -51,6 +63,8 @@ class SchoolPresenter @Inject constructor(
}.subscribe({
Timber.i("Loading teachers result: Success")
view?.run {
address = it.address.ifBlank { null }
contact = it.contact.ifBlank { null }
updateData(it)
showContent(true)
showEmpty(false)

View File

@ -19,4 +19,8 @@ interface SchoolView : BaseView, SchoolAndTeachersChildView {
fun showContent(show: Boolean)
fun hideRefresh()
fun openMapsLocation(location: String)
fun dialPhone(phone: String)
}

View File

@ -9,7 +9,6 @@ import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import io.github.wulkanowy.R
@ColorInt
fun Context.getThemeAttrColor(@AttrRes colorAttr: Int): Int {
@ -41,4 +40,18 @@ fun Context.openEmail(chooserTitle: String, email: String, subject: String?, bod
startActivity(Intent.createChooser(emailIntent, chooserTitle))
}
fun Context.openMapLocation(location: String) {
val intentUri = Uri.parse("geo:0,0?q=${Uri.encode(location)}")
val intent = Intent(Intent.ACTION_VIEW, intentUri)
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
}
}
fun Context.dialPhone(phone: String) {
val intentUri = Uri.parse("tel:$phone")
val intent = Intent(Intent.ACTION_DIAL, intentUri)
startActivity(intent)
}
fun Context.dpToPx(dp: Float) = dp * resources.displayMetrics.densityDpi / DENSITY_DEFAULT