lets start rewriting the Select Contact screen to fragments

This commit is contained in:
tibbi 2019-12-06 20:21:20 +01:00
parent 9d2254e891
commit 9aac2414fd
3 changed files with 64 additions and 135 deletions

View file

@ -1,24 +1,27 @@
package com.simplemobiletools.contacts.pro.activities
import android.app.Activity
import android.content.Intent
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.provider.ContactsContract
import android.view.Menu
import androidx.viewpager.widget.ViewPager
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.PERMISSION_GET_ACCOUNTS
import com.simplemobiletools.commons.helpers.PERMISSION_READ_CONTACTS
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_CONTACTS
import com.simplemobiletools.contacts.pro.R
import com.simplemobiletools.contacts.pro.adapters.ContactsAdapter
import com.simplemobiletools.contacts.pro.extensions.config
import com.simplemobiletools.contacts.pro.extensions.getContactPublicUri
import com.simplemobiletools.contacts.pro.helpers.*
import com.simplemobiletools.contacts.pro.models.Contact
import com.simplemobiletools.contacts.pro.helpers.CONTACTS_TAB_MASK
import com.simplemobiletools.contacts.pro.helpers.FAVORITES_TAB_MASK
import kotlinx.android.synthetic.main.activity_insert_edit_contact.*
class InsertOrEditContactActivity : SimpleActivity() {
private val START_INSERT_ACTIVITY = 1
private val START_EDIT_ACTIVITY = 2
private val contactsFavoritesList = arrayListOf(CONTACTS_TAB_MASK,
FAVORITES_TAB_MASK
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_insert_edit_contact)
@ -27,12 +30,18 @@ class InsertOrEditContactActivity : SimpleActivity() {
return
}
setupViews()
setupTabColors()
// we do not really care about the permission request result. Even if it was denied, load private contacts
handlePermission(PERMISSION_READ_CONTACTS) {
// we do not really care about the permission request result. Even if it was denied, load private contacts
ContactsHelper(this).getContacts {
gotContacts(it)
if (it) {
handlePermission(PERMISSION_WRITE_CONTACTS) {
handlePermission(PERMISSION_GET_ACCOUNTS) {
initFragments()
}
}
} else {
initFragments()
}
}
}
@ -42,77 +51,46 @@ class InsertOrEditContactActivity : SimpleActivity() {
return super.onCreateOptionsMenu(menu)
}
private fun setupViews() {
updateTextColors(insert_edit_contact_holder)
new_contact_tmb.setImageDrawable(resources.getColoredDrawableWithColor(R.drawable.ic_new_contact_vector, config.textColor))
new_contact_holder.setOnClickListener {
val name = intent.getStringExtra(KEY_NAME) ?: ""
val phoneNumber = getPhoneNumberFromIntent(intent) ?: ""
val email = getEmailFromIntent(intent) ?: ""
private fun initFragments() {
viewpager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
override fun onPageScrollStateChanged(state: Int) {}
Intent().apply {
action = Intent.ACTION_INSERT
data = ContactsContract.Contacts.CONTENT_URI
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
if (phoneNumber.isNotEmpty()) {
putExtra(KEY_PHONE, phoneNumber)
override fun onPageSelected(position: Int) {
insert_or_edit_tabs_holder.getTabAt(position)?.select()
invalidateOptionsMenu()
}
})
insert_or_edit_tabs_holder.onTabSelectionChanged(
tabUnselectedAction = {
it.icon?.applyColorFilter(config.textColor)
},
tabSelectedAction = {
viewpager.currentItem = it.position
it.icon?.applyColorFilter(getAdjustedPrimaryColor())
}
)
if (name.isNotEmpty()) {
putExtra(KEY_NAME, name)
}
if (email.isNotEmpty()) {
putExtra(KEY_EMAIL, email)
}
if (resolveActivity(packageManager) != null) {
startActivityForResult(this, START_INSERT_ACTIVITY)
} else {
toast(R.string.no_app_found)
}
insert_or_edit_tabs_holder.removeAllTabs()
var skippedTabs = 0
contactsFavoritesList.forEachIndexed { index, value ->
if (config.showTabs and value == 0) {
skippedTabs++
} else {
val tab = insert_or_edit_tabs_holder.newTab().setIcon(getTabIcon(index))
insert_or_edit_tabs_holder.addTab(tab, index - skippedTabs, index == 0)
}
}
select_contact_label.setTextColor(getAdjustedPrimaryColor())
insert_or_edit_tabs_holder.beVisibleIf(skippedTabs == 0)
}
private fun gotContacts(contacts: ArrayList<Contact>) {
ContactsAdapter(this, contacts, null, LOCATION_INSERT_OR_EDIT, null, select_contact_list, select_contact_fastscroller) {
val contact = it as Contact
val phoneNumber = getPhoneNumberFromIntent(intent) ?: ""
val email = getEmailFromIntent(intent) ?: ""
Intent(applicationContext, EditContactActivity::class.java).apply {
data = getContactPublicUri(contact)
action = ADD_NEW_CONTACT_NUMBER
if (phoneNumber.isNotEmpty()) {
putExtra(KEY_PHONE, phoneNumber)
}
if (email.isNotEmpty()) {
putExtra(KEY_EMAIL, email)
}
putExtra(IS_PRIVATE, contact.isPrivate())
startActivityForResult(this, START_EDIT_ACTIVITY)
}
}.apply {
select_contact_list.adapter = this
}
select_contact_fastscroller.setScrollToY(0)
select_contact_fastscroller.setViews(select_contact_list) {
val item = (select_contact_list.adapter as ContactsAdapter).contactItems.getOrNull(it)
select_contact_fastscroller.updateBubbleText(item?.getBubbleText() ?: "")
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
super.onActivityResult(requestCode, resultCode, resultData)
if (resultCode == Activity.RESULT_OK) {
finish()
private fun setupTabColors() {
insert_or_edit_tabs_holder.apply {
background = ColorDrawable(config.backgroundColor)
setSelectedTabIndicatorColor(getAdjustedPrimaryColor())
}
}
}

View file

@ -396,7 +396,8 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
if (config.showTabs and value == 0) {
skippedTabs++
} else {
main_tabs_holder.addTab(main_tabs_holder.newTab().setIcon(getTabIcon(index)), index - skippedTabs, config.lastUsedViewPagerPage == index - skippedTabs)
val tab = main_tabs_holder.newTab().setIcon(getTabIcon(index))
main_tabs_holder.addTab(tab, index - skippedTabs, config.lastUsedViewPagerPage == index - skippedTabs)
}
}

View file

@ -5,70 +5,20 @@
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/new_contact_holder"
<com.google.android.material.tabs.TabLayout
android:id="@+id/insert_or_edit_tabs_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:paddingTop="@dimen/medium_margin"
android:paddingEnd="@dimen/activity_margin"
android:paddingBottom="@dimen/medium_margin">
android:layout_height="48dp"
app:elevation="6dp"
app:tabIndicatorColor="@android:color/white"
app:tabIndicatorHeight="2dp"
app:tabMinWidth="150dp"
app:tabSelectedTextColor="@android:color/white" />
<ImageView
android:id="@+id/new_contact_tmb"
android:layout_width="@dimen/normal_icon_size"
android:layout_height="@dimen/normal_icon_size"
android:layout_centerVertical="true"
android:padding="@dimen/medium_margin"
android:src="@drawable/ic_new_contact_vector" />
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/new_contact_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/new_contact_tmb"
android:ellipsize="end"
android:gravity="center_vertical"
android:maxLines="1"
android:paddingStart="@dimen/small_margin"
android:text="@string/create_new_contact"
android:textSize="@dimen/big_text_size" />
</RelativeLayout>
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/select_contact_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/new_contact_holder"
android:layout_marginStart="@dimen/bigger_margin"
android:layout_marginTop="@dimen/small_margin"
android:layout_marginBottom="@dimen/activity_margin"
android:text="@string/add_to_existing_contact"
android:textAllCaps="true"
android:textSize="@dimen/smaller_text_size" />
<com.simplemobiletools.commons.views.MyRecyclerView
android:id="@+id/select_contact_list"
<com.simplemobiletools.commons.views.MyViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/select_contact_label"
android:clipToPadding="false"
android:scrollbars="none"
app:layoutManager="com.simplemobiletools.commons.views.MyLinearLayoutManager" />
android:layout_below="@+id/insert_or_edit_tabs_holder" />
<com.simplemobiletools.commons.views.FastScroller
android:id="@+id/select_contact_fastscroller"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="@+id/select_contact_label"
android:layout_alignTop="@+id/select_contact_list"
android:layout_alignBottom="@+id/select_contact_list"
android:layout_alignParentEnd="true"
android:paddingStart="@dimen/normal_margin">
<include layout="@layout/fastscroller_handle_vertical" />
</com.simplemobiletools.commons.views.FastScroller>
</RelativeLayout>