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

View File

@ -4,11 +4,14 @@ import android.content.Context
import android.support.design.widget.CoordinatorLayout import android.support.design.widget.CoordinatorLayout
import android.util.AttributeSet import android.util.AttributeSet
import com.simplemobiletools.contacts.activities.MainActivity import com.simplemobiletools.contacts.activities.MainActivity
import com.simplemobiletools.contacts.helpers.ContactsHelper
import com.simplemobiletools.contacts.interfaces.FragmentInterface import com.simplemobiletools.contacts.interfaces.FragmentInterface
import com.simplemobiletools.contacts.models.Contact import com.simplemobiletools.contacts.models.Contact
class GroupsFragment(context: Context, attributeSet: AttributeSet) : CoordinatorLayout(context, attributeSet), FragmentInterface { class GroupsFragment(context: Context, attributeSet: AttributeSet) : CoordinatorLayout(context, attributeSet), FragmentInterface {
var activity: MainActivity? = null
override fun setupFragment(activity: MainActivity) { override fun setupFragment(activity: MainActivity) {
this.activity = activity
} }
override fun textColorChanged(color: Int) { override fun textColorChanged(color: Int) {
@ -18,5 +21,17 @@ class GroupsFragment(context: Context, attributeSet: AttributeSet) : Coordinator
} }
override fun refreshContacts(contacts: ArrayList<Contact>) { 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) 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 { activity.dbHelper.getContacts().forEach {
contacts.put(it.id, it) contacts.put(it.id, it)
} }
@ -116,6 +109,15 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
var resultContacts = ArrayList<Contact>(contactsSize) var resultContacts = ArrayList<Contact>(contactsSize)
(0 until contactsSize).mapTo(resultContacts) { contacts.valueAt(it) } (0 until contactsSize).mapTo(resultContacts) { contacts.valueAt(it) }
resultContacts = resultContacts.distinctBy { it.contactId } as ArrayList<Contact> 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 { activity.runOnUiThread {
callback(resultContacts) callback(resultContacts)
} }
@ -336,12 +338,11 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
val id = cursor.getIntValue(ContactsContract.Data.CONTACT_ID) val id = cursor.getIntValue(ContactsContract.Data.CONTACT_ID)
val newRowId = cursor.getLongValue(ContactsContract.Data.DATA1) 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) { if (groups[id] == null) {
groups.put(id, ArrayList()) groups.put(id, ArrayList())
} }
val groupTitle = storedGroups.firstOrNull { it.id == newRowId }?.title ?: continue
val group = Group(newRowId, groupTitle)
groups[id]!!.add(group) groups[id]!!.add(group)
} while (cursor.moveToNext()) } while (cursor.moveToNext())
} }

View File

@ -1,3 +1,5 @@
package com.simplemobiletools.contacts.models 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++
}