migrate fragments to viewbinding

This commit is contained in:
fatih ergin 2023-08-25 01:14:28 +03:00
parent d65d6e884d
commit 168dcc9f71
4 changed files with 150 additions and 97 deletions

View File

@ -11,15 +11,24 @@ import com.simplemobiletools.dialer.R
import com.simplemobiletools.dialer.activities.MainActivity import com.simplemobiletools.dialer.activities.MainActivity
import com.simplemobiletools.dialer.activities.SimpleActivity import com.simplemobiletools.dialer.activities.SimpleActivity
import com.simplemobiletools.dialer.adapters.ContactsAdapter import com.simplemobiletools.dialer.adapters.ContactsAdapter
import com.simplemobiletools.dialer.databinding.FragmentContactsBinding
import com.simplemobiletools.dialer.databinding.FragmentLettersLayoutBinding
import com.simplemobiletools.dialer.extensions.launchCreateNewContactIntent import com.simplemobiletools.dialer.extensions.launchCreateNewContactIntent
import com.simplemobiletools.dialer.extensions.startContactDetailsIntent import com.simplemobiletools.dialer.extensions.startContactDetailsIntent
import com.simplemobiletools.dialer.interfaces.RefreshItemsListener import com.simplemobiletools.dialer.interfaces.RefreshItemsListener
import kotlinx.android.synthetic.main.fragment_letters_layout.view.*
import java.util.Locale import java.util.Locale
class ContactsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet), RefreshItemsListener { class ContactsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment<MyViewPagerFragment.LettersInnerBinding>(context, attributeSet),
RefreshItemsListener {
private lateinit var binding: FragmentLettersLayoutBinding
private var allContacts = ArrayList<Contact>() private var allContacts = ArrayList<Contact>()
override fun onFinishInflate() {
super.onFinishInflate()
binding = FragmentLettersLayoutBinding.bind(FragmentContactsBinding.bind(this).contactsFragment)
innerBinding = LettersInnerBinding(binding)
}
override fun setupFragment() { override fun setupFragment() {
val placeholderResId = if (context.hasPermission(PERMISSION_READ_CONTACTS)) { val placeholderResId = if (context.hasPermission(PERMISSION_READ_CONTACTS)) {
R.string.no_contacts_found R.string.no_contacts_found
@ -27,7 +36,7 @@ class ContactsFragment(context: Context, attributeSet: AttributeSet) : MyViewPag
R.string.could_not_access_contacts R.string.could_not_access_contacts
} }
fragment_placeholder.text = context.getString(placeholderResId) binding.fragmentPlaceholder.text = context.getString(placeholderResId)
val placeholderActionResId = if (context.hasPermission(PERMISSION_READ_CONTACTS)) { val placeholderActionResId = if (context.hasPermission(PERMISSION_READ_CONTACTS)) {
R.string.create_new_contact R.string.create_new_contact
@ -35,7 +44,7 @@ class ContactsFragment(context: Context, attributeSet: AttributeSet) : MyViewPag
R.string.request_access R.string.request_access
} }
fragment_placeholder_2.apply { binding.fragmentPlaceholder2.apply {
text = context.getString(placeholderActionResId) text = context.getString(placeholderActionResId)
underlineText() underlineText()
setOnClickListener { setOnClickListener {
@ -49,15 +58,17 @@ class ContactsFragment(context: Context, attributeSet: AttributeSet) : MyViewPag
} }
override fun setupColors(textColor: Int, primaryColor: Int, properPrimaryColor: Int) { override fun setupColors(textColor: Int, primaryColor: Int, properPrimaryColor: Int) {
(fragment_list?.adapter as? MyRecyclerViewAdapter)?.updateTextColor(textColor) binding.apply {
fragment_placeholder.setTextColor(textColor) (fragmentList?.adapter as? MyRecyclerViewAdapter)?.updateTextColor(textColor)
fragment_placeholder_2.setTextColor(properPrimaryColor) fragmentPlaceholder.setTextColor(textColor)
fragmentPlaceholder2.setTextColor(properPrimaryColor)
letter_fastscroller.textColor = textColor.getColorStateList() letterFastscroller.textColor = textColor.getColorStateList()
letter_fastscroller.pressedTextColor = properPrimaryColor letterFastscroller.pressedTextColor = properPrimaryColor
letter_fastscroller_thumb.setupWithFastScroller(letter_fastscroller) letterFastscrollerThumb.setupWithFastScroller(letterFastscroller)
letter_fastscroller_thumb.textColor = properPrimaryColor.getContrastColor() letterFastscrollerThumb.textColor = properPrimaryColor.getContrastColor()
letter_fastscroller_thumb.thumbColor = properPrimaryColor.getColorStateList() letterFastscrollerThumb.thumbColor = properPrimaryColor.getColorStateList()
}
} }
override fun refreshItems(callback: (() -> Unit)?) { override fun refreshItems(callback: (() -> Unit)?) {
@ -84,39 +95,42 @@ class ContactsFragment(context: Context, attributeSet: AttributeSet) : MyViewPag
private fun gotContacts(contacts: ArrayList<Contact>) { private fun gotContacts(contacts: ArrayList<Contact>) {
setupLetterFastScroller(contacts) setupLetterFastScroller(contacts)
if (contacts.isEmpty()) { if (contacts.isEmpty()) {
fragment_placeholder.beVisible() binding.apply {
fragment_placeholder_2.beVisible() fragmentPlaceholder.beVisible()
fragment_list.beGone() fragmentPlaceholder2.beVisible()
fragmentList.beGone()
}
} else { } else {
fragment_placeholder.beGone() binding.apply {
fragment_placeholder_2.beGone() fragmentPlaceholder.beGone()
fragment_list.beVisible() fragmentPlaceholder2.beGone()
fragmentList.beVisible()
}
val currAdapter = fragment_list.adapter if (binding.fragmentList.adapter == null) {
if (currAdapter == null) {
ContactsAdapter( ContactsAdapter(
activity = activity as SimpleActivity, activity = activity as SimpleActivity,
contacts = contacts, contacts = contacts,
recyclerView = fragment_list, recyclerView = binding.fragmentList,
refreshItemsListener = this refreshItemsListener = this
) { ) {
val contact = it as Contact val contact = it as Contact
activity?.startContactDetailsIntent(contact) activity?.startContactDetailsIntent(contact)
}.apply { }.apply {
fragment_list.adapter = this binding.fragmentList.adapter = this
} }
if (context.areSystemAnimationsEnabled) { if (context.areSystemAnimationsEnabled) {
fragment_list.scheduleLayoutAnimation() binding.fragmentList.scheduleLayoutAnimation()
} }
} else { } else {
(currAdapter as ContactsAdapter).updateItems(contacts) (binding.fragmentList.adapter as ContactsAdapter).updateItems(contacts)
} }
} }
} }
private fun setupLetterFastScroller(contacts: ArrayList<Contact>) { private fun setupLetterFastScroller(contacts: ArrayList<Contact>) {
letter_fastscroller.setupWithRecyclerView(fragment_list, { position -> binding.letterFastscroller.setupWithRecyclerView(binding.fragmentList, { position ->
try { try {
val name = contacts[position].getNameToDisplay() val name = contacts[position].getNameToDisplay()
val character = if (name.isNotEmpty()) name.substring(0, 1) else "" val character = if (name.isNotEmpty()) name.substring(0, 1) else ""
@ -128,8 +142,8 @@ class ContactsFragment(context: Context, attributeSet: AttributeSet) : MyViewPag
} }
override fun onSearchClosed() { override fun onSearchClosed() {
fragment_placeholder.beVisibleIf(allContacts.isEmpty()) binding.fragmentPlaceholder.beVisibleIf(allContacts.isEmpty())
(fragment_list.adapter as? ContactsAdapter)?.updateItems(allContacts) (binding.fragmentList.adapter as? ContactsAdapter)?.updateItems(allContacts)
setupLetterFastScroller(allContacts) setupLetterFastScroller(allContacts)
} }
@ -155,16 +169,16 @@ class ContactsFragment(context: Context, attributeSet: AttributeSet) : MyViewPag
!getProperText(nameToDisplay, shouldNormalize).startsWith(text, true) && !nameToDisplay.contains(text, true) !getProperText(nameToDisplay, shouldNormalize).startsWith(text, true) && !nameToDisplay.contains(text, true)
} }
fragment_placeholder.beVisibleIf(filtered.isEmpty()) binding.fragmentPlaceholder.beVisibleIf(filtered.isEmpty())
(fragment_list.adapter as? ContactsAdapter)?.updateItems(filtered, text) (binding.fragmentList.adapter as? ContactsAdapter)?.updateItems(filtered, text)
setupLetterFastScroller(filtered) setupLetterFastScroller(filtered)
} }
private fun requestReadContactsPermission() { private fun requestReadContactsPermission() {
activity?.handlePermission(PERMISSION_READ_CONTACTS) { activity?.handlePermission(PERMISSION_READ_CONTACTS) {
if (it) { if (it) {
fragment_placeholder.text = context.getString(R.string.no_contacts_found) binding.fragmentPlaceholder.text = context.getString(R.string.no_contacts_found)
fragment_placeholder_2.text = context.getString(R.string.create_new_contact) binding.fragmentPlaceholder2.text = context.getString(R.string.create_new_contact)
ContactsHelper(context).getContacts(showOnlyContactsWithNumbers = true) { contacts -> ContactsHelper(context).getContacts(showOnlyContactsWithNumbers = true) { contacts ->
activity?.runOnUiThread { activity?.runOnUiThread {
gotContacts(contacts) gotContacts(contacts)

View File

@ -14,15 +14,24 @@ import com.simplemobiletools.commons.views.MyLinearLayoutManager
import com.simplemobiletools.dialer.R import com.simplemobiletools.dialer.R
import com.simplemobiletools.dialer.activities.SimpleActivity import com.simplemobiletools.dialer.activities.SimpleActivity
import com.simplemobiletools.dialer.adapters.ContactsAdapter import com.simplemobiletools.dialer.adapters.ContactsAdapter
import com.simplemobiletools.dialer.databinding.FragmentFavoritesBinding
import com.simplemobiletools.dialer.databinding.FragmentLettersLayoutBinding
import com.simplemobiletools.dialer.extensions.config import com.simplemobiletools.dialer.extensions.config
import com.simplemobiletools.dialer.helpers.Converters import com.simplemobiletools.dialer.helpers.Converters
import com.simplemobiletools.dialer.interfaces.RefreshItemsListener import com.simplemobiletools.dialer.interfaces.RefreshItemsListener
import kotlinx.android.synthetic.main.fragment_letters_layout.view.*
import java.util.Locale import java.util.Locale
class FavoritesFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet), RefreshItemsListener { class FavoritesFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment<MyViewPagerFragment.LettersInnerBinding>(context, attributeSet),
RefreshItemsListener {
private lateinit var binding: FragmentLettersLayoutBinding
private var allContacts = ArrayList<Contact>() private var allContacts = ArrayList<Contact>()
override fun onFinishInflate() {
super.onFinishInflate()
binding = FragmentLettersLayoutBinding.bind(FragmentFavoritesBinding.bind(this).favoritesFragment)
innerBinding = LettersInnerBinding(binding)
}
override fun setupFragment() { override fun setupFragment() {
val placeholderResId = if (context.hasPermission(PERMISSION_READ_CONTACTS)) { val placeholderResId = if (context.hasPermission(PERMISSION_READ_CONTACTS)) {
R.string.no_contacts_found R.string.no_contacts_found
@ -30,19 +39,21 @@ class FavoritesFragment(context: Context, attributeSet: AttributeSet) : MyViewPa
R.string.could_not_access_contacts R.string.could_not_access_contacts
} }
fragment_placeholder.text = context.getString(placeholderResId) binding.fragmentPlaceholder.text = context.getString(placeholderResId)
fragment_placeholder_2.beGone() binding.fragmentPlaceholder2.beGone()
} }
override fun setupColors(textColor: Int, primaryColor: Int, properPrimaryColor: Int) { override fun setupColors(textColor: Int, primaryColor: Int, properPrimaryColor: Int) {
fragment_placeholder.setTextColor(textColor) binding.apply {
(fragment_list?.adapter as? MyRecyclerViewAdapter)?.updateTextColor(textColor) fragmentPlaceholder.setTextColor(textColor)
(fragmentList.adapter as? MyRecyclerViewAdapter)?.updateTextColor(textColor)
letter_fastscroller.textColor = textColor.getColorStateList() letterFastscroller.textColor = textColor.getColorStateList()
letter_fastscroller.pressedTextColor = properPrimaryColor letterFastscroller.pressedTextColor = properPrimaryColor
letter_fastscroller_thumb.setupWithFastScroller(letter_fastscroller) letterFastscrollerThumb.setupWithFastScroller(letterFastscroller)
letter_fastscroller_thumb.textColor = properPrimaryColor.getContrastColor() letterFastscrollerThumb.textColor = properPrimaryColor.getContrastColor()
letter_fastscroller_thumb.thumbColor = properPrimaryColor.getColorStateList() letterFastscrollerThumb.thumbColor = properPrimaryColor.getColorStateList()
}
} }
override fun refreshItems(callback: (() -> Unit)?) { override fun refreshItems(callback: (() -> Unit)?) {
@ -76,14 +87,16 @@ class FavoritesFragment(context: Context, attributeSet: AttributeSet) : MyViewPa
private fun gotContacts(contacts: ArrayList<Contact>) { private fun gotContacts(contacts: ArrayList<Contact>) {
setupLetterFastScroller(contacts) setupLetterFastScroller(contacts)
if (contacts.isEmpty()) { binding.apply {
fragment_placeholder.beVisible() if (contacts.isEmpty()) {
fragment_list.beGone() fragmentPlaceholder.beVisible()
} else { fragmentList.beGone()
fragment_placeholder.beGone() } else {
fragment_list.beVisible() fragmentPlaceholder.beGone()
fragmentList.beVisible()
updateListAdapter() updateListAdapter()
}
} }
} }
@ -91,12 +104,12 @@ class FavoritesFragment(context: Context, attributeSet: AttributeSet) : MyViewPa
val viewType = context.config.viewType val viewType = context.config.viewType
setViewType(viewType) setViewType(viewType)
val currAdapter = fragment_list.adapter as ContactsAdapter? val currAdapter = binding.fragmentList.adapter as ContactsAdapter?
if (currAdapter == null) { if (currAdapter == null) {
ContactsAdapter( ContactsAdapter(
activity = activity as SimpleActivity, activity = activity as SimpleActivity,
contacts = allContacts, contacts = allContacts,
recyclerView = fragment_list, recyclerView = binding.fragmentList,
refreshItemsListener = this, refreshItemsListener = this,
viewType = viewType, viewType = viewType,
showDeleteButton = false, showDeleteButton = false,
@ -114,10 +127,10 @@ class FavoritesFragment(context: Context, attributeSet: AttributeSet) : MyViewPa
} }
} }
}.apply { }.apply {
fragment_list.adapter = this binding.fragmentList.adapter = this
onDragEndListener = { onDragEndListener = {
val adapter = fragment_list?.adapter val adapter = binding.fragmentList.adapter
if (adapter is ContactsAdapter) { if (adapter is ContactsAdapter) {
val items = adapter.contacts val items = adapter.contacts
saveCustomOrderToPrefs(items) saveCustomOrderToPrefs(items)
@ -131,7 +144,7 @@ class FavoritesFragment(context: Context, attributeSet: AttributeSet) : MyViewPa
} }
if (context.areSystemAnimationsEnabled) { if (context.areSystemAnimationsEnabled) {
fragment_list.scheduleLayoutAnimation() binding.fragmentList.scheduleLayoutAnimation()
} }
} else { } else {
currAdapter.viewType = viewType currAdapter.viewType = viewType
@ -140,8 +153,8 @@ class FavoritesFragment(context: Context, attributeSet: AttributeSet) : MyViewPa
} }
fun columnCountChanged() { fun columnCountChanged() {
(fragment_list.layoutManager as MyGridLayoutManager).spanCount = context!!.config.contactsGridColumnCount (binding.fragmentList.layoutManager as MyGridLayoutManager).spanCount = context!!.config.contactsGridColumnCount
fragment_list?.adapter?.apply { binding.fragmentList.adapter?.apply {
notifyItemRangeChanged(0, allContacts.size) notifyItemRangeChanged(0, allContacts.size)
} }
} }
@ -169,7 +182,7 @@ class FavoritesFragment(context: Context, attributeSet: AttributeSet) : MyViewPa
} }
private fun setupLetterFastScroller(contacts: List<Contact>) { private fun setupLetterFastScroller(contacts: List<Contact>) {
letter_fastscroller.setupWithRecyclerView(fragment_list, { position -> binding.letterFastscroller.setupWithRecyclerView(binding.fragmentList, { position ->
try { try {
val name = contacts[position].getNameToDisplay() val name = contacts[position].getNameToDisplay()
val character = if (name.isNotEmpty()) name.substring(0, 1) else "" val character = if (name.isNotEmpty()) name.substring(0, 1) else ""
@ -181,8 +194,8 @@ class FavoritesFragment(context: Context, attributeSet: AttributeSet) : MyViewPa
} }
override fun onSearchClosed() { override fun onSearchClosed() {
fragment_placeholder.beVisibleIf(allContacts.isEmpty()) binding.fragmentPlaceholder.beVisibleIf(allContacts.isEmpty())
(fragment_list.adapter as? ContactsAdapter)?.updateItems(allContacts) (binding.fragmentList.adapter as? ContactsAdapter)?.updateItems(allContacts)
setupLetterFastScroller(allContacts) setupLetterFastScroller(allContacts)
} }
@ -193,8 +206,8 @@ class FavoritesFragment(context: Context, attributeSet: AttributeSet) : MyViewPa
it.name.startsWith(text, true) it.name.startsWith(text, true)
}.toMutableList() as ArrayList<Contact> }.toMutableList() as ArrayList<Contact>
fragment_placeholder.beVisibleIf(contacts.isEmpty()) binding.fragmentPlaceholder.beVisibleIf(contacts.isEmpty())
(fragment_list.adapter as? ContactsAdapter)?.updateItems(contacts, text) (binding.fragmentList.adapter as? ContactsAdapter)?.updateItems(contacts, text)
setupLetterFastScroller(contacts) setupLetterFastScroller(contacts)
} }
@ -202,12 +215,12 @@ class FavoritesFragment(context: Context, attributeSet: AttributeSet) : MyViewPa
val spanCount = context.config.contactsGridColumnCount val spanCount = context.config.contactsGridColumnCount
val layoutManager = if (viewType == VIEW_TYPE_GRID) { val layoutManager = if (viewType == VIEW_TYPE_GRID) {
letter_fastscroller.beGone() binding.letterFastscroller.beGone()
MyGridLayoutManager(context, spanCount) MyGridLayoutManager(context, spanCount)
} else { } else {
letter_fastscroller.beVisible() binding.letterFastscroller.beVisible()
MyLinearLayoutManager(context) MyLinearLayoutManager(context)
} }
fragment_list.layoutManager = layoutManager binding.fragmentList.layoutManager = layoutManager
} }
} }

View File

@ -9,18 +9,20 @@ import com.simplemobiletools.commons.extensions.getProperTextColor
import com.simplemobiletools.commons.extensions.getTextSize import com.simplemobiletools.commons.extensions.getTextSize
import com.simplemobiletools.commons.helpers.SORT_BY_FIRST_NAME import com.simplemobiletools.commons.helpers.SORT_BY_FIRST_NAME
import com.simplemobiletools.commons.helpers.SORT_BY_SURNAME import com.simplemobiletools.commons.helpers.SORT_BY_SURNAME
import com.simplemobiletools.commons.views.MyRecyclerView
import com.simplemobiletools.dialer.activities.MainActivity import com.simplemobiletools.dialer.activities.MainActivity
import com.simplemobiletools.dialer.activities.SimpleActivity import com.simplemobiletools.dialer.activities.SimpleActivity
import com.simplemobiletools.dialer.adapters.ContactsAdapter import com.simplemobiletools.dialer.adapters.ContactsAdapter
import com.simplemobiletools.dialer.adapters.RecentCallsAdapter import com.simplemobiletools.dialer.adapters.RecentCallsAdapter
import com.simplemobiletools.dialer.databinding.FragmentLettersLayoutBinding
import com.simplemobiletools.dialer.databinding.FragmentRecentsBinding
import com.simplemobiletools.dialer.extensions.config import com.simplemobiletools.dialer.extensions.config
import com.simplemobiletools.dialer.helpers.Config import com.simplemobiletools.dialer.helpers.Config
import kotlinx.android.synthetic.main.fragment_letters_layout.view.fragment_list
import kotlinx.android.synthetic.main.fragment_recents.view.recents_list
abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) : RelativeLayout(context, attributeSet) { abstract class MyViewPagerFragment<BINDING : MyViewPagerFragment.InnerBinding>(context: Context, attributeSet: AttributeSet) :
RelativeLayout(context, attributeSet) {
protected var activity: SimpleActivity? = null protected var activity: SimpleActivity? = null
protected lateinit var innerBinding: BINDING
private lateinit var config: Config private lateinit var config: Config
fun setupFragment(activity: SimpleActivity) { fun setupFragment(activity: SimpleActivity) {
@ -35,7 +37,7 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
fun startNameWithSurnameChanged(startNameWithSurname: Boolean) { fun startNameWithSurnameChanged(startNameWithSurname: Boolean) {
if (this !is RecentsFragment) { if (this !is RecentsFragment) {
(fragment_list.adapter as? ContactsAdapter)?.apply { (innerBinding.fragmentList?.adapter as? ContactsAdapter)?.apply {
config.sorting = if (startNameWithSurname) SORT_BY_SURNAME else SORT_BY_FIRST_NAME config.sorting = if (startNameWithSurname) SORT_BY_SURNAME else SORT_BY_FIRST_NAME
(this@MyViewPagerFragment.activity!! as MainActivity).refreshFragments() (this@MyViewPagerFragment.activity!! as MainActivity).refreshFragments()
} }
@ -43,18 +45,18 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
} }
fun finishActMode() { fun finishActMode() {
(fragment_list?.adapter as? MyRecyclerViewAdapter)?.finishActMode() (innerBinding.fragmentList?.adapter as? MyRecyclerViewAdapter)?.finishActMode()
(recents_list?.adapter as? MyRecyclerViewAdapter)?.finishActMode() (innerBinding.recentsList?.adapter as? MyRecyclerViewAdapter)?.finishActMode()
} }
fun fontSizeChanged() { fun fontSizeChanged() {
if (this is RecentsFragment) { if (this is RecentsFragment) {
(recents_list?.adapter as? RecentCallsAdapter)?.apply { (innerBinding.recentsList?.adapter as? RecentCallsAdapter)?.apply {
fontSize = activity.getTextSize() fontSize = activity.getTextSize()
notifyDataSetChanged() notifyDataSetChanged()
} }
} else { } else {
(fragment_list?.adapter as? ContactsAdapter)?.apply { (innerBinding.fragmentList?.adapter as? ContactsAdapter)?.apply {
fontSize = activity.getTextSize() fontSize = activity.getTextSize()
notifyDataSetChanged() notifyDataSetChanged()
} }
@ -68,4 +70,19 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
abstract fun onSearchClosed() abstract fun onSearchClosed()
abstract fun onSearchQueryChanged(text: String) abstract fun onSearchQueryChanged(text: String)
interface InnerBinding {
val fragmentList: MyRecyclerView?
val recentsList: MyRecyclerView?
}
class LettersInnerBinding(val binding: FragmentLettersLayoutBinding) : InnerBinding {
override val fragmentList: MyRecyclerView = binding.fragmentList
override val recentsList = null
}
class RecentsInnerBinding(val binding: FragmentRecentsBinding) : InnerBinding {
override val fragmentList = null
override val recentsList = binding.recentsList
}
} }

View File

@ -13,19 +13,25 @@ import com.simplemobiletools.commons.views.MyRecyclerView
import com.simplemobiletools.dialer.R import com.simplemobiletools.dialer.R
import com.simplemobiletools.dialer.activities.SimpleActivity import com.simplemobiletools.dialer.activities.SimpleActivity
import com.simplemobiletools.dialer.adapters.RecentCallsAdapter import com.simplemobiletools.dialer.adapters.RecentCallsAdapter
import com.simplemobiletools.dialer.databinding.FragmentRecentsBinding
import com.simplemobiletools.dialer.extensions.config import com.simplemobiletools.dialer.extensions.config
import com.simplemobiletools.dialer.helpers.MIN_RECENTS_THRESHOLD import com.simplemobiletools.dialer.helpers.MIN_RECENTS_THRESHOLD
import com.simplemobiletools.dialer.helpers.RecentsHelper import com.simplemobiletools.dialer.helpers.RecentsHelper
import com.simplemobiletools.dialer.interfaces.RefreshItemsListener import com.simplemobiletools.dialer.interfaces.RefreshItemsListener
import com.simplemobiletools.dialer.models.RecentCall import com.simplemobiletools.dialer.models.RecentCall
import kotlinx.android.synthetic.main.fragment_recents.view.recents_list
import kotlinx.android.synthetic.main.fragment_recents.view.recents_placeholder
import kotlinx.android.synthetic.main.fragment_recents.view.recents_placeholder_2
class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet), RefreshItemsListener { class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment<MyViewPagerFragment.RecentsInnerBinding>(context, attributeSet),
RefreshItemsListener {
private lateinit var binding: FragmentRecentsBinding
private var allRecentCalls = listOf<RecentCall>() private var allRecentCalls = listOf<RecentCall>()
private var recentsAdapter: RecentCallsAdapter? = null private var recentsAdapter: RecentCallsAdapter? = null
override fun onFinishInflate() {
super.onFinishInflate()
binding = FragmentRecentsBinding.bind(this)
innerBinding = RecentsInnerBinding(binding)
}
override fun setupFragment() { override fun setupFragment() {
val placeholderResId = if (context.hasPermission(PERMISSION_READ_CALL_LOG)) { val placeholderResId = if (context.hasPermission(PERMISSION_READ_CALL_LOG)) {
R.string.no_previous_calls R.string.no_previous_calls
@ -33,8 +39,8 @@ class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
R.string.could_not_access_the_call_history R.string.could_not_access_the_call_history
} }
recents_placeholder.text = context.getString(placeholderResId) binding.recentsPlaceholder.text = context.getString(placeholderResId)
recents_placeholder_2.apply { binding.recentsPlaceholder2.apply {
underlineText() underlineText()
setOnClickListener { setOnClickListener {
requestCallLogPermission() requestCallLogPermission()
@ -43,8 +49,8 @@ class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
} }
override fun setupColors(textColor: Int, primaryColor: Int, properPrimaryColor: Int) { override fun setupColors(textColor: Int, primaryColor: Int, properPrimaryColor: Int) {
recents_placeholder.setTextColor(textColor) binding.recentsPlaceholder.setTextColor(textColor)
recents_placeholder_2.setTextColor(properPrimaryColor) binding.recentsPlaceholder.setTextColor(properPrimaryColor)
recentsAdapter?.apply { recentsAdapter?.apply {
initDrawables() initDrawables()
@ -73,17 +79,20 @@ class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
private fun gotRecents(recents: List<RecentCall>) { private fun gotRecents(recents: List<RecentCall>) {
if (recents.isEmpty()) { if (recents.isEmpty()) {
recents_placeholder.beVisible() binding.apply {
recents_placeholder_2.beGoneIf(context.hasPermission(PERMISSION_READ_CALL_LOG)) recentsPlaceholder.beVisible()
recents_list.beGone() recentsPlaceholder2.beGoneIf(context.hasPermission(PERMISSION_READ_CALL_LOG))
recentsList.beGone()
}
} else { } else {
recents_placeholder.beGone() binding.apply {
recents_placeholder_2.beGone() recentsPlaceholder.beGone()
recents_list.beVisible() recentsPlaceholder2.beGone()
recentsList.beVisible()
}
val currAdapter = recents_list.adapter if (binding.recentsList.adapter == null) {
if (currAdapter == null) { recentsAdapter = RecentCallsAdapter(activity as SimpleActivity, recents.toMutableList(), binding.recentsList, this, true) {
recentsAdapter = RecentCallsAdapter(activity as SimpleActivity, recents.toMutableList(), recents_list, this, true) {
val recentCall = it as RecentCall val recentCall = it as RecentCall
if (context.config.showCallConfirmation) { if (context.config.showCallConfirmation) {
CallConfirmationDialog(activity as SimpleActivity, recentCall.name) { CallConfirmationDialog(activity as SimpleActivity, recentCall.name) {
@ -94,13 +103,13 @@ class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
} }
} }
recents_list.adapter = recentsAdapter binding.recentsList.adapter = recentsAdapter
if (context.areSystemAnimationsEnabled) { if (context.areSystemAnimationsEnabled) {
recents_list.scheduleLayoutAnimation() binding.recentsList.scheduleLayoutAnimation()
} }
recents_list.endlessScrollListener = object : MyRecyclerView.EndlessScrollListener { binding.recentsList.endlessScrollListener = object : MyRecyclerView.EndlessScrollListener {
override fun updateTop() {} override fun updateTop() {}
override fun updateBottom() { override fun updateBottom() {
@ -136,8 +145,8 @@ class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
private fun requestCallLogPermission() { private fun requestCallLogPermission() {
activity?.handlePermission(PERMISSION_READ_CALL_LOG) { activity?.handlePermission(PERMISSION_READ_CALL_LOG) {
if (it) { if (it) {
recents_placeholder.text = context.getString(R.string.no_previous_calls) binding.recentsPlaceholder.text = context.getString(R.string.no_previous_calls)
recents_placeholder_2.beGone() binding.recentsPlaceholder2.beGone()
val groupSubsequentCalls = context?.config?.groupSubsequentCalls ?: false val groupSubsequentCalls = context?.config?.groupSubsequentCalls ?: false
RecentsHelper(context).getRecentCalls(groupSubsequentCalls) { recents -> RecentsHelper(context).getRecentCalls(groupSubsequentCalls) { recents ->
@ -150,7 +159,7 @@ class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
} }
override fun onSearchClosed() { override fun onSearchClosed() {
recents_placeholder.beVisibleIf(allRecentCalls.isEmpty()) binding.recentsPlaceholder.beVisibleIf(allRecentCalls.isEmpty())
recentsAdapter?.updateItems(allRecentCalls) recentsAdapter?.updateItems(allRecentCalls)
} }
@ -161,7 +170,7 @@ class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
it.name.startsWith(text, true) it.name.startsWith(text, true)
}.toMutableList() as ArrayList<RecentCall> }.toMutableList() as ArrayList<RecentCall>
recents_placeholder.beVisibleIf(recentCalls.isEmpty()) binding.recentsPlaceholder.beVisibleIf(recentCalls.isEmpty())
recentsAdapter?.updateItems(recentCalls, text) recentsAdapter?.updateItems(recentCalls, text)
} }
} }