handle counting group members

This commit is contained in:
tibbi 2018-03-19 15:30:57 +01:00
parent 33380cc3a0
commit 707f3ee4a0
4 changed files with 30 additions and 12 deletions

View File

@ -286,7 +286,6 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
invalidateOptionsMenu()
}
})
viewpager.currentItem = config.lastUsedViewPagerPage
main_tabs_holder.onTabSelectionChanged(
tabUnselectedAction = {
@ -409,6 +408,7 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
if (viewpager.adapter == null) {
viewpager.adapter = ViewPagerAdapter(this, it)
viewpager.currentItem = config.lastUsedViewPagerPage
}
if (refreshContactsTab) {

View File

@ -4,11 +4,14 @@ import android.content.Context
import android.support.design.widget.CoordinatorLayout
import android.util.AttributeSet
import com.simplemobiletools.contacts.activities.MainActivity
import com.simplemobiletools.contacts.helpers.ContactsHelper
import com.simplemobiletools.contacts.interfaces.FragmentInterface
import com.simplemobiletools.contacts.models.Contact
class GroupsFragment(context: Context, attributeSet: AttributeSet) : CoordinatorLayout(context, attributeSet), FragmentInterface {
var activity: MainActivity? = null
override fun setupFragment(activity: MainActivity) {
this.activity = activity
}
override fun textColorChanged(color: Int) {
@ -18,5 +21,17 @@ class GroupsFragment(context: Context, attributeSet: AttributeSet) : Coordinator
}
override fun refreshContacts(contacts: ArrayList<Contact>) {
if (activity == null) {
return
}
val storedGroups = ContactsHelper(activity!!).getStoredGroups()
contacts.forEach {
it.groups.forEach {
val group = it
val storedGroup = storedGroups.firstOrNull { it.id == group.id }
storedGroup?.addContact()
}
}
}
}

View File

@ -101,13 +101,6 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
contacts[key]?.notes = notes.valueAt(i)
}
val groups = getContactGroups(getStoredGroups())
size = groups.size()
for (i in 0 until size) {
val key = groups.keyAt(i)
contacts[key]?.groups = groups.valueAt(i)
}
activity.dbHelper.getContacts().forEach {
contacts.put(it.id, it)
}
@ -116,6 +109,15 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
var resultContacts = ArrayList<Contact>(contactsSize)
(0 until contactsSize).mapTo(resultContacts) { contacts.valueAt(it) }
resultContacts = resultContacts.distinctBy { it.contactId } as ArrayList<Contact>
// groups are obtained with contactID, not rawID, so assign them to proper contacts like this
val groups = getContactGroups(getStoredGroups())
size = groups.size()
for (i in 0 until size) {
val key = groups.keyAt(i)
resultContacts.firstOrNull { it.contactId == key }?.groups = groups.valueAt(i)
}
activity.runOnUiThread {
callback(resultContacts)
}
@ -336,12 +338,11 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
val id = cursor.getIntValue(ContactsContract.Data.CONTACT_ID)
val newRowId = cursor.getLongValue(ContactsContract.Data.DATA1)
val groupTitle = storedGroups.firstOrNull { it.id == newRowId }?.title ?: continue
val group = Group(newRowId, groupTitle)
if (groups[id] == null) {
groups.put(id, ArrayList())
}
val groupTitle = storedGroups.firstOrNull { it.id == newRowId }?.title ?: continue
val group = Group(newRowId, groupTitle)
groups[id]!!.add(group)
} while (cursor.moveToNext())
}

View File

@ -1,3 +1,5 @@
package com.simplemobiletools.contacts.models
data class Group(var id: Long, var title: String)
data class Group(var id: Long, var title: String, var contactsCount: Int = 0) {
fun addContact() = contactsCount++
}