Contacts can now be set as shortcuts

This commit is contained in:
ottoptj 2024-11-12 23:23:23 +02:00
commit 53f4d792e9
3 changed files with 60 additions and 16 deletions

View file

@ -15,10 +15,13 @@ import eu.ottop.yamlauncher.utils.UIUtils
class ContactsAdapter( class ContactsAdapter(
private val activity: MainActivity, private val activity: MainActivity,
private var contacts: MutableList<Pair<String, Int>>, private var contacts: MutableList<Pair<String, Int>>,
private val contactClickListener: OnContactClickListener private val contactClickListener: OnContactClickListener,
private val contactShortcutListener: OnContactShortcutListener,
) : ) :
RecyclerView.Adapter<ContactsAdapter.AppViewHolder>() { RecyclerView.Adapter<ContactsAdapter.AppViewHolder>() {
var shortcutTextView: TextView? = null
private val uiUtils = UIUtils(activity) private val uiUtils = UIUtils(activity)
private val sharedPreferenceManager = SharedPreferenceManager(activity) private val sharedPreferenceManager = SharedPreferenceManager(activity)
@ -26,16 +29,25 @@ class ContactsAdapter(
fun onContactClick(contactId: Int) fun onContactClick(contactId: Int)
} }
interface OnContactShortcutListener {
fun onContactShortcut(contactId: Int, contactName: String, shortcutView: TextView)
}
inner class AppViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { inner class AppViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val listItem: FrameLayout = itemView.findViewById(R.id.listItem) val listItem: FrameLayout = itemView.findViewById(R.id.listItem)
val textView: TextView = listItem.findViewById(R.id.appName) val textView: TextView = listItem.findViewById(R.id.appName)
init { init {
textView.setOnClickListener { textView.setOnClickListener {
if (shortcutTextView != null) {
val position = bindingAdapterPosition
val contact = contacts[position]
contactShortcutListener.onContactShortcut(contact.second, contact.first, shortcutTextView!!)
} else {
val position = bindingAdapterPosition val position = bindingAdapterPosition
val contact = contacts[position] val contact = contacts[position]
contactClickListener.onContactClick(contact.second) contactClickListener.onContactClick(contact.second)
}
} }
} }
} }

View file

@ -68,7 +68,7 @@ import java.lang.reflect.Method
import kotlin.math.abs import kotlin.math.abs
class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceChangeListener, AppMenuAdapter.OnItemClickListener, AppMenuAdapter.OnShortcutListener, AppMenuAdapter.OnItemLongClickListener, ContactsAdapter.OnContactClickListener { class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceChangeListener, AppMenuAdapter.OnItemClickListener, AppMenuAdapter.OnShortcutListener, AppMenuAdapter.OnItemLongClickListener, ContactsAdapter.OnContactClickListener, ContactsAdapter.OnContactShortcutListener {
private lateinit var weatherSystem: WeatherSystem private lateinit var weatherSystem: WeatherSystem
private lateinit var appUtils: AppUtils private lateinit var appUtils: AppUtils
@ -211,7 +211,11 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
else { else {
textView.visibility = View.VISIBLE textView.visibility = View.VISIBLE
val savedView = sharedPreferenceManager.getShortcut(textView) val savedView = sharedPreferenceManager.getShortcut(textView)?.toMutableList()
if (savedView != null && savedView.size < 4) {
savedView.add(3, "false")
}
// Set the non-work profile drawable by default // Set the non-work profile drawable by default
textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(resources, R.drawable.ic_empty, null),null,null,null) textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(resources, R.drawable.ic_empty, null),null,null,null)
@ -249,6 +253,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
} }
appAdapter?.shortcutTextView = textView appAdapter?.shortcutTextView = textView
contactAdapter?.shortcutTextView = textView
toAppMenu() toAppMenu()
true true
} }
@ -273,8 +278,8 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
setRenameShortcutListener(textView, savedView) setRenameShortcutListener(textView, savedView)
} }
appAdapter?.shortcutTextView = textView appAdapter?.shortcutTextView = textView
contactAdapter?.shortcutTextView = textView
toAppMenu() toAppMenu()
searchSwitcher.visibility = View.GONE
return@setOnLongClickListener true return@setOnLongClickListener true
} }
@ -296,13 +301,15 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
sharedPreferenceManager.setShortcut( sharedPreferenceManager.setShortcut(
textView, textView,
savedView[0], savedView[0],
savedView[1].toInt() savedView[1].toInt(),
savedView[3].toBoolean()
) )
} catch (_: NumberFormatException) { } catch (_: NumberFormatException) {
sharedPreferenceManager.setShortcut( sharedPreferenceManager.setShortcut(
textView, textView,
savedView[0], savedView[0],
0 0,
savedView[3].toBoolean()
) )
} }
backToHome() backToHome()
@ -313,6 +320,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
} }
private fun toAppMenu() { private fun toAppMenu() {
uiUtils.setContactsVisibility(searchSwitcher, binding.searchLayout, binding.searchReplacement)
try { try {
// The menu opens from the top // The menu opens from the top
appRecycler.scrollToPosition(0) appRecycler.scrollToPosition(0)
@ -349,11 +357,15 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
private fun setShortcutSetup(textView: TextView, savedView: List<String>?) { private fun setShortcutSetup(textView: TextView, savedView: List<String>?) {
// Set the work profile drawable for work profile apps // Set the work profile drawable for work profile apps
textView.text = savedView?.get(2)
if (savedView != null && savedView[3].toBoolean()) {
setShortcutContactListeners(textView, savedView[1].toInt())
return
}
if (savedView?.get(1) != "0") { if (savedView?.get(1) != "0") {
textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(resources, R.drawable.ic_work_app, null),null,null,null) textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(resources, R.drawable.ic_work_app, null),null,null,null)
} }
textView.text = savedView?.get(2)
setShortcutListeners(textView, savedView) setShortcutListeners(textView, savedView)
} }
@ -365,6 +377,12 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
} }
} }
private fun setShortcutContactListeners(textView: TextView, contactId: Int) {
textView.setOnClickListener {
onContactClick(contactId)
}
}
private fun setPreferences() { private fun setPreferences() {
uiUtils.setBackground(window) uiUtils.setBackground(window)
@ -502,8 +520,8 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
private fun openAppMenu() { private fun openAppMenu() {
appAdapter?.shortcutTextView = null appAdapter?.shortcutTextView = null
contactAdapter?.shortcutTextView = null
menuTitle.visibility = View.GONE menuTitle.visibility = View.GONE
uiUtils.setContactsVisibility(searchSwitcher, binding.searchLayout, binding.searchReplacement)
toAppMenu() toAppMenu()
} }
@ -803,7 +821,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
private fun setupContactRecycler() { private fun setupContactRecycler() {
uiUtils.setImageColor(searchSwitcher) uiUtils.setImageColor(searchSwitcher)
contactAdapter = ContactsAdapter(this, mutableListOf(), this) contactAdapter = ContactsAdapter(this, mutableListOf(), this@MainActivity, this@MainActivity)
contactMenuLinearLayoutManager.stackFromEnd = true contactMenuLinearLayoutManager.stackFromEnd = true
contactRecycler = binding.contactRecycler contactRecycler = binding.contactRecycler
contactRecycler.layoutManager = contactMenuLinearLayoutManager contactRecycler.layoutManager = contactMenuLinearLayoutManager
@ -1052,7 +1070,6 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
backToHome() backToHome()
} }
override fun onItemLongClick( override fun onItemLongClick(
textView: TextView, textView: TextView,
actionMenuLayout: LinearLayout, actionMenuLayout: LinearLayout,
@ -1157,4 +1174,19 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
startActivity(intent) startActivity(intent)
returnAllowed = false returnAllowed = false
} }
override fun onContactShortcut(contactId: Int, contactName: String, shortcutView: TextView) {
shortcutView.text = contactName
shortcutView.setOnClickListener {
onContactClick(contactId)
}
sharedPreferenceManager.setShortcut(
shortcutView,
contactName,
contactId,
true
)
uiUtils.setDrawables(shortcutView, sharedPreferenceManager.getShortcutAlignment())
backToHome()
}
} }

View file

@ -89,9 +89,9 @@ class SharedPreferenceManager (private val context: Context) {
return preferences.getString("dateSize", "medium") return preferences.getString("dateSize", "medium")
} }
fun setShortcut(textView: TextView, packageName: String, profile: Int) { fun setShortcut(textView: TextView, packageName: String, profile: Int, isContact: Boolean = false) {
val editor = preferences.edit() val editor = preferences.edit()
editor.putString("shortcut${textView.id}", "$packageName§splitter§$profile§splitter§${textView.text}") editor.putString("shortcut${textView.id}", "$packageName§splitter§$profile§splitter§${textView.text}§splitter§${isContact}")
editor.apply() editor.apply()
} }