mirror of
https://github.com/SimpleMobileTools/Simple-Contacts.git
synced 2025-02-23 22:57:50 +01:00
adding a ContactsAdapter
This commit is contained in:
parent
0b7e652052
commit
8678a7d109
@ -32,5 +32,5 @@ android {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation 'com.simplemobiletools:commons:3.2.5'
|
implementation 'com.simplemobiletools:commons:3.2.6'
|
||||||
}
|
}
|
||||||
|
@ -10,18 +10,23 @@ import com.simplemobiletools.commons.extensions.toast
|
|||||||
import com.simplemobiletools.commons.helpers.LICENSE_KOTLIN
|
import com.simplemobiletools.commons.helpers.LICENSE_KOTLIN
|
||||||
import com.simplemobiletools.commons.helpers.LICENSE_MULTISELECT
|
import com.simplemobiletools.commons.helpers.LICENSE_MULTISELECT
|
||||||
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_CONTACTS
|
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_CONTACTS
|
||||||
|
import com.simplemobiletools.commons.interfaces.RefreshRecyclerViewListener
|
||||||
import com.simplemobiletools.contacts.BuildConfig
|
import com.simplemobiletools.contacts.BuildConfig
|
||||||
import com.simplemobiletools.contacts.R
|
import com.simplemobiletools.contacts.R
|
||||||
|
import com.simplemobiletools.contacts.adapters.ContactsAdapter
|
||||||
import com.simplemobiletools.contacts.extensions.config
|
import com.simplemobiletools.contacts.extensions.config
|
||||||
import com.simplemobiletools.contacts.helpers.ContactsHelper
|
import com.simplemobiletools.contacts.helpers.ContactsHelper
|
||||||
|
import com.simplemobiletools.contacts.models.Contact
|
||||||
|
import kotlinx.android.synthetic.main.activity_main.*
|
||||||
|
|
||||||
class MainActivity : SimpleActivity() {
|
class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
|
||||||
private var storedUseEnglish = false
|
private var storedUseEnglish = false
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContentView(R.layout.activity_main)
|
setContentView(R.layout.activity_main)
|
||||||
appLaunched()
|
appLaunched()
|
||||||
|
contacts_fab.setOnClickListener { addNewContact() }
|
||||||
|
|
||||||
handlePermission(PERMISSION_WRITE_CONTACTS) {
|
handlePermission(PERMISSION_WRITE_CONTACTS) {
|
||||||
if (it) {
|
if (it) {
|
||||||
@ -70,7 +75,44 @@ class MainActivity : SimpleActivity() {
|
|||||||
|
|
||||||
private fun initContacts() {
|
private fun initContacts() {
|
||||||
ContactsHelper(this).getContacts {
|
ContactsHelper(this).getContacts {
|
||||||
|
Contact.sorting = config.sorting
|
||||||
|
it.sort()
|
||||||
|
runOnUiThread {
|
||||||
|
setupContacts(it)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun setupContacts(contacts: ArrayList<Contact>) {
|
||||||
|
val currAdapter = contacts_list.adapter
|
||||||
|
if (currAdapter == null) {
|
||||||
|
ContactsAdapter(this, contacts, this, contacts_list) {
|
||||||
|
itemClicked(it as Contact)
|
||||||
|
}.apply {
|
||||||
|
setupDragListener(true)
|
||||||
|
addVerticalDividers(true)
|
||||||
|
contacts_list.adapter = this
|
||||||
|
}
|
||||||
|
|
||||||
|
contacts_fastscroller.allowBubbleDisplay = config.showInfoBubble
|
||||||
|
contacts_fastscroller.setViews(contacts_list) {
|
||||||
|
val item = contacts.getOrNull(it)
|
||||||
|
contacts_fastscroller.updateBubbleText(item?.getBubbleText() ?: "")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
(currAdapter as ContactsAdapter).updateItems(contacts)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun itemClicked(contact: Contact) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun addNewContact() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun refreshItems() {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,7 @@ class SettingsActivity : SimpleActivity() {
|
|||||||
|
|
||||||
setupCustomizeColors()
|
setupCustomizeColors()
|
||||||
setupUseEnglish()
|
setupUseEnglish()
|
||||||
|
setupShowInfoBubble()
|
||||||
updateTextColors(settings_holder)
|
updateTextColors(settings_holder)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,4 +39,12 @@ class SettingsActivity : SimpleActivity() {
|
|||||||
useEnglishToggled()
|
useEnglishToggled()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun setupShowInfoBubble() {
|
||||||
|
settings_show_info_bubble.isChecked = config.showInfoBubble
|
||||||
|
settings_show_info_bubble_holder.setOnClickListener {
|
||||||
|
settings_show_info_bubble.toggle()
|
||||||
|
config.showInfoBubble = settings_show_info_bubble.isChecked
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,94 @@
|
|||||||
|
package com.simplemobiletools.contacts.adapters
|
||||||
|
|
||||||
|
import android.graphics.drawable.Drawable
|
||||||
|
import android.view.Menu
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import com.bumptech.glide.Glide
|
||||||
|
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
|
||||||
|
import com.simplemobiletools.commons.dialogs.ConfirmationDialog
|
||||||
|
import com.simplemobiletools.commons.extensions.getColoredDrawableWithColor
|
||||||
|
import com.simplemobiletools.commons.extensions.isActivityDestroyed
|
||||||
|
import com.simplemobiletools.commons.interfaces.RefreshRecyclerViewListener
|
||||||
|
import com.simplemobiletools.commons.views.MyRecyclerView
|
||||||
|
import com.simplemobiletools.contacts.R
|
||||||
|
import com.simplemobiletools.contacts.activities.SimpleActivity
|
||||||
|
import com.simplemobiletools.contacts.models.Contact
|
||||||
|
import kotlinx.android.synthetic.main.item_contact.view.*
|
||||||
|
|
||||||
|
class ContactsAdapter(activity: SimpleActivity, var contactItems: MutableList<Contact>, val listener: RefreshRecyclerViewListener?,
|
||||||
|
recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, itemClick) {
|
||||||
|
|
||||||
|
lateinit private var contactDrawable: Drawable
|
||||||
|
|
||||||
|
init {
|
||||||
|
initDrawables()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getActionMenuId() = R.menu.cab
|
||||||
|
|
||||||
|
override fun prepareActionMode(menu: Menu) {}
|
||||||
|
|
||||||
|
override fun prepareItemSelection(view: View) {}
|
||||||
|
|
||||||
|
override fun markItemSelection(select: Boolean, view: View?) {
|
||||||
|
view?.contact_frame?.isSelected = select
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun actionItemPressed(id: Int) {
|
||||||
|
when (id) {
|
||||||
|
R.id.cab_select_all -> selectAll()
|
||||||
|
R.id.cab_delete -> askConfirmDelete()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getSelectableItemCount() = contactItems.size
|
||||||
|
|
||||||
|
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int) = createViewHolder(R.layout.item_contact, parent)
|
||||||
|
|
||||||
|
override fun onBindViewHolder(holder: MyRecyclerViewAdapter.ViewHolder, position: Int) {
|
||||||
|
val contact = contactItems[position]
|
||||||
|
val view = holder.bindView(contact, true) { itemView, layoutPosition ->
|
||||||
|
setupView(itemView, contact)
|
||||||
|
}
|
||||||
|
bindViewHolder(holder, position, view)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getItemCount() = contactItems.size
|
||||||
|
|
||||||
|
fun initDrawables() {
|
||||||
|
contactDrawable = activity.resources.getColoredDrawableWithColor(R.drawable.ic_person, textColor)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updateItems(newItems: MutableList<Contact>) {
|
||||||
|
contactItems = newItems
|
||||||
|
notifyDataSetChanged()
|
||||||
|
finishActMode()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun askConfirmDelete() {
|
||||||
|
ConfirmationDialog(activity) {
|
||||||
|
deleteContacts()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun deleteContacts() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onViewRecycled(holder: ViewHolder?) {
|
||||||
|
super.onViewRecycled(holder)
|
||||||
|
if (!activity.isActivityDestroyed()) {
|
||||||
|
Glide.with(activity).clear(holder?.itemView?.contact_tmb)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun setupView(view: View, contact: Contact) {
|
||||||
|
view.apply {
|
||||||
|
contact_name.text = contact.name
|
||||||
|
contact_name.setTextColor(textColor)
|
||||||
|
contact_number.text = contact.number
|
||||||
|
contact_number.setTextColor(textColor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -2,10 +2,14 @@ package com.simplemobiletools.contacts.helpers
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import com.simplemobiletools.commons.helpers.BaseConfig
|
import com.simplemobiletools.commons.helpers.BaseConfig
|
||||||
|
import com.simplemobiletools.commons.helpers.SORT_BY_NAME
|
||||||
|
|
||||||
class Config(context: Context) : BaseConfig(context) {
|
class Config(context: Context) : BaseConfig(context) {
|
||||||
companion object {
|
companion object {
|
||||||
fun newInstance(context: Context) = Config(context)
|
fun newInstance(context: Context) = Config(context)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var sorting: Int
|
||||||
|
get() = prefs.getInt(SORTING, SORT_BY_NAME)
|
||||||
|
set(sorting) = prefs.edit().putInt(SORTING, sorting).apply()
|
||||||
}
|
}
|
||||||
|
@ -1 +1,3 @@
|
|||||||
package com.simplemobiletools.contacts.helpers
|
package com.simplemobiletools.contacts.helpers
|
||||||
|
|
||||||
|
val SORTING = "sorting"
|
||||||
|
@ -1,3 +1,28 @@
|
|||||||
package com.simplemobiletools.contacts.models
|
package com.simplemobiletools.contacts.models
|
||||||
|
|
||||||
data class Contact(val id: Int, var name: String, var number: String, var photoUri: String)
|
import com.simplemobiletools.commons.helpers.SORT_BY_NUMBER
|
||||||
|
import com.simplemobiletools.commons.helpers.SORT_DESCENDING
|
||||||
|
|
||||||
|
data class Contact(val id: Int, var name: String, var number: String, var photoUri: String) : Comparable<Contact> {
|
||||||
|
companion object {
|
||||||
|
var sorting: Int = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun compareTo(other: Contact): Int {
|
||||||
|
var result = when {
|
||||||
|
(sorting and SORT_BY_NUMBER != 0) -> number.toLowerCase().compareTo(other.number.toLowerCase())
|
||||||
|
else -> name.toLowerCase().compareTo(other.name.toLowerCase())
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sorting and SORT_DESCENDING != 0) {
|
||||||
|
result *= -1
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getBubbleText() = when {
|
||||||
|
sorting and SORT_BY_NUMBER != 0 -> number
|
||||||
|
else -> name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,8 +1,44 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<RelativeLayout
|
<android.support.design.widget.CoordinatorLayout
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
android:id="@+id/main_holder"
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
android:id="@+id/contacts_holder"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent">
|
android:layout_height="match_parent">
|
||||||
|
|
||||||
</RelativeLayout>
|
<RelativeLayout
|
||||||
|
android:id="@+id/contacts_wrapper"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content">
|
||||||
|
|
||||||
|
<com.simplemobiletools.commons.views.MyRecyclerView
|
||||||
|
android:id="@+id/contacts_list"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:clipToPadding="false"
|
||||||
|
android:scrollbars="none"
|
||||||
|
app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>
|
||||||
|
|
||||||
|
<com.simplemobiletools.commons.views.FastScroller
|
||||||
|
android:id="@+id/contacts_fastscroller"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:layout_alignParentEnd="true"
|
||||||
|
android:layout_alignParentRight="true"
|
||||||
|
android:paddingLeft="@dimen/normal_margin"
|
||||||
|
android:paddingStart="@dimen/normal_margin">
|
||||||
|
|
||||||
|
<include layout="@layout/fastscroller_handle_vertical"/>
|
||||||
|
|
||||||
|
</com.simplemobiletools.commons.views.FastScroller>
|
||||||
|
</RelativeLayout>
|
||||||
|
|
||||||
|
<com.simplemobiletools.commons.views.MyFloatingActionButton
|
||||||
|
android:id="@+id/contacts_fab"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_gravity="bottom|end"
|
||||||
|
android:layout_margin="@dimen/activity_margin"
|
||||||
|
android:src="@drawable/ic_plus"/>
|
||||||
|
|
||||||
|
</android.support.design.widget.CoordinatorLayout>
|
||||||
|
@ -49,5 +49,25 @@
|
|||||||
android:text="@string/use_english_language"/>
|
android:text="@string/use_english_language"/>
|
||||||
|
|
||||||
</RelativeLayout>
|
</RelativeLayout>
|
||||||
|
|
||||||
|
<RelativeLayout
|
||||||
|
android:id="@+id/settings_show_info_bubble_holder"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="@dimen/medium_margin"
|
||||||
|
android:background="?attr/selectableItemBackground"
|
||||||
|
android:padding="@dimen/activity_margin">
|
||||||
|
|
||||||
|
<com.simplemobiletools.commons.views.MySwitchCompat
|
||||||
|
android:id="@+id/settings_show_info_bubble"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:background="@null"
|
||||||
|
android:clickable="false"
|
||||||
|
android:paddingLeft="@dimen/medium_margin"
|
||||||
|
android:paddingStart="@dimen/medium_margin"
|
||||||
|
android:text="@string/show_info_bubble"/>
|
||||||
|
|
||||||
|
</RelativeLayout>
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
</ScrollView>
|
</ScrollView>
|
||||||
|
14
app/src/main/res/menu/cab.xml
Normal file
14
app/src/main/res/menu/cab.xml
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||||
|
<item
|
||||||
|
android:id="@+id/cab_select_all"
|
||||||
|
android:icon="@drawable/ic_select_all"
|
||||||
|
android:title="@string/select_all"
|
||||||
|
app:showAsAction="ifRoom"/>
|
||||||
|
<item
|
||||||
|
android:id="@+id/cab_delete"
|
||||||
|
android:icon="@drawable/ic_delete"
|
||||||
|
android:title="@string/delete"
|
||||||
|
app:showAsAction="ifRoom"/>
|
||||||
|
</menu>
|
Loading…
x
Reference in New Issue
Block a user