Skip to content

Commit

Permalink
ContactsViewModel to Android ViewModel, allows us to ditch global sco…
Browse files Browse the repository at this point in the history
…pe in the ImageContact BindingAdapter
  • Loading branch information
growse committed Feb 9, 2022
1 parent 3f99fdb commit f0d576a
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import android.util.Base64
import android.widget.ImageView
import androidx.databinding.BindingAdapter
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.owntracks.android.model.FusedContact
import org.owntracks.android.support.widgets.TextDrawable
import timber.log.Timber
Expand All @@ -18,11 +21,11 @@ class ContactImageBindingAdapter @Inject constructor(
@ApplicationContext context: Context,
private val memoryCache: ContactBitmapAndNameMemoryCache
) {
@BindingAdapter(value = ["contact"])
fun ImageView.displayFaceInViewAsync(c: FusedContact?) {
c?.also { contact ->
GlobalScope.launch(Dispatchers.Main) {
setImageBitmap(getBitmapFromCache(contact))
@BindingAdapter(value = ["contact", "coroutineScope"])
fun ImageView.displayFaceInViewAsync(contact: FusedContact?, scope: CoroutineScope) {
contact?.also {
scope.launch(Dispatchers.Main) {
setImageBitmap(getBitmapFromCache(it))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ protected final void bindAndAttachContentView(@LayoutRes int layoutResId, @Nulla
binding.setLifecycleOwner(this);

//noinspection unchecked
viewModel.attachView(savedInstanceState, (MvvmView) this);
if (MvvmView.class.isAssignableFrom(this.getClass())) {
viewModel.attachView(savedInstanceState, (MvvmView) this);
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,38 @@ package org.owntracks.android.ui.contacts
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import dagger.hilt.android.AndroidEntryPoint
import org.owntracks.android.R
import org.owntracks.android.databinding.UiContactsBinding
import org.owntracks.android.model.FusedContact
import org.owntracks.android.ui.base.BaseActivity
import org.owntracks.android.ui.base.BaseAdapter
import org.owntracks.android.ui.base.viewmodel.NoOpViewModel
import org.owntracks.android.ui.map.MapActivity

@AndroidEntryPoint
class ContactsActivity : BaseActivity<UiContactsBinding?, ContactsMvvm.ViewModel<*>?>(),
ContactsMvvm.View, BaseAdapter.ClickListener<FusedContact?> {

class ContactsActivity : BaseActivity<UiContactsBinding?, NoOpViewModel>(),
BaseAdapter.ClickListener<FusedContact?> {
private val vm: ContactsViewModel by viewModels()
private lateinit var contactsAdapter: ContactsAdapter

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
contactsAdapter = ContactsAdapter(this)
setHasEventBus(false)
contactsAdapter = ContactsAdapter(this, vm.coroutineScope)
bindAndAttachContentView(R.layout.ui_contacts, savedInstanceState)
setHasEventBus(false)
setSupportToolbar(binding!!.appbar.toolbar)
setDrawer(binding!!.appbar.toolbar)
binding!!.vm!!.contacts.observe({ this.lifecycle }, { contacts: Map<String, FusedContact> ->
vm.contacts.observe({ this.lifecycle }, { contacts: Map<String, FusedContact> ->
contactsAdapter.setContactList(contacts.values)
binding!!.vm!!.refreshGeocodes()
vm.refreshGeocodes()
})
binding!!.recyclerView.layoutManager = LinearLayoutManager(this)
binding!!.recyclerView.adapter = contactsAdapter
binding?.recyclerView?.run {
layoutManager = LinearLayoutManager(this@ContactsActivity)
adapter = contactsAdapter
}
}

override fun onClick(fusedContact: FusedContact, view: View, longClick: Boolean) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.CoroutineScope
import org.owntracks.android.BR
import org.owntracks.android.R
import org.owntracks.android.model.FusedContact
import org.owntracks.android.ui.base.BaseAdapter
import java.util.*

internal class ContactsAdapter(private val clickListener: BaseAdapter.ClickListener<FusedContact?>) :
RecyclerView.Adapter<FusedContactViewHolder>() {
internal class ContactsAdapter(
private val clickListener: BaseAdapter.ClickListener<FusedContact?>,
private val coroutineScope: CoroutineScope
) :
RecyclerView.Adapter<ContactsAdapter.FusedContactViewHolder>() {
private lateinit var contactList: List<FusedContact>
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FusedContactViewHolder {
val binding = DataBindingUtil.inflate<ViewDataBinding>(
Expand All @@ -21,7 +24,7 @@ internal class ContactsAdapter(private val clickListener: BaseAdapter.ClickListe
parent,
false
)
return FusedContactViewHolder(binding)
return FusedContactViewHolder(binding, coroutineScope)
}

override fun onBindViewHolder(holder: FusedContactViewHolder, position: Int) {
Expand All @@ -36,23 +39,30 @@ internal class ContactsAdapter(private val clickListener: BaseAdapter.ClickListe
contactList = ArrayList(contacts)
notifyDataSetChanged()
}
}

internal class FusedContactViewHolder(private val binding: ViewDataBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(fusedContact: FusedContact?, clickListener: BaseAdapter.ClickListener<FusedContact?>) {
fusedContact?.run {
binding.setVariable(BR.contact, this)
binding.root.setOnClickListener {
clickListener.onClick(
this,
binding.root,
false
)
}
class FusedContactViewHolder(
private val binding: ViewDataBinding,
private val coroutineScope: CoroutineScope
) :
RecyclerView.ViewHolder(binding.root) {
fun bind(
fusedContact: FusedContact?,
clickListener: BaseAdapter.ClickListener<FusedContact?>
) {
fusedContact?.run {
binding.setVariable(BR.contact, this)
binding.setVariable(BR.coroutineScope, coroutineScope)
binding.root.setOnClickListener {
clickListener.onClick(
this,
binding.root,
false
)
}

}
binding.executePendingBindings()
}

binding.executePendingBindings()
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
package org.owntracks.android.ui.contacts

import android.os.Bundle
import androidx.lifecycle.LiveData
import dagger.hilt.android.scopes.ActivityScoped
import kotlinx.coroutines.MainScope
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import org.owntracks.android.data.repos.ContactsRepo
import org.owntracks.android.geocoding.GeocoderProvider
import org.owntracks.android.model.FusedContact
import org.owntracks.android.ui.base.viewmodel.BaseViewModel
import timber.log.Timber
import javax.inject.Inject

@ActivityScoped
@HiltViewModel
class ContactsViewModel @Inject constructor(
private val contactsRepo: ContactsRepo, private val geocoderProvider: GeocoderProvider
) :
BaseViewModel<ContactsMvvm.View?>(), ContactsMvvm.ViewModel<ContactsMvvm.View?> {
private val mainScope = MainScope()
override fun attachView(savedInstanceState: Bundle?, view: ContactsMvvm.View?) {
super.attachView(savedInstanceState, view!!)
}
) : ViewModel() {

fun refreshGeocodes() {
Timber.i("Refreshing contacts geocodes")
mainScope.launch {
viewModelScope.launch {
contactsRepo.all.value?.run {
map { it.value.messageLocation }
.filterNotNull()
Expand All @@ -34,6 +29,8 @@ class ContactsViewModel @Inject constructor(
}
}

override val contacts: LiveData<out Map<String, FusedContact>>
val contacts: LiveData<out Map<String, FusedContact>>
get() = contactsRepo.all
val coroutineScope: CoroutineScope
get() = viewModelScope
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import org.owntracks.android.data.repos.ContactsRepo
import org.owntracks.android.data.repos.LocationRepo
Expand Down Expand Up @@ -69,6 +70,9 @@ class MapViewModel @Inject constructor(
val myLocationEnabled: LiveData<Boolean>
get() = mutableMyLocationEnabled

val scope: CoroutineScope
get() = viewModelScope

val allContacts = contactsRepo.all
val locationIdlingResource = SimpleIdlingResource("locationIdlingResource", false)

Expand Down
3 changes: 2 additions & 1 deletion project/app/src/main/res/layout/ui_map.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
<include
android:id="@+id/contactPeek"
layout="@layout/ui_row_contact"
app:contact="@{vm.currentContact}" />
app:contact="@{vm.currentContact}"
app:coroutineScope="@{vm.scope}" />

<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
Expand Down
4 changes: 4 additions & 0 deletions project/app/src/main/res/layout/ui_row_contact.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
name="contact"
type="org.owntracks.android.model.FusedContact" />

<variable
name="coroutineScope"
type="kotlinx.coroutines.CoroutineScope" />
</data>


Expand All @@ -30,6 +33,7 @@
android:layout_marginTop="16dp"
android:contentDescription="@string/contact_image"
app:contact="@{contact}"
app:coroutineScope="@{coroutineScope}"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

Expand Down

0 comments on commit f0d576a

Please sign in to comment.