feat(Friends): Call/Text shortcuts

parent 0fac2ce4
package fr.plnech.dunbar package fr.plnech.dunbar
import android.content.Intent
import android.net.Uri
import fr.plnech.dunbar.model.Friend
fun String.plural(count: Int = 1): String { fun String.plural(count: Int = 1): String {
return when { return when {
count > 1 -> this + "s" count > 1 -> this + "s"
else -> this else -> this
} }
}
fun Friend.smsIntent(): Intent? {
return phone?.let {
val uri = Uri.parse("smsto:${it}")
Intent(Intent.ACTION_SENDTO, uri).apply {
putExtra("sms_body", "Hey ${this@smsIntent.firstName}, how are you?")
}
}
}
fun Friend.callIntent(): Intent? {
return phone?.let {
Intent(Intent.ACTION_DIAL, Uri.parse("tel:${it}"))
}
} }
\ No newline at end of file
...@@ -12,11 +12,14 @@ data class Friend(val map: MutableMap<String, String?>, val photo: Bitmap?) { ...@@ -12,11 +12,14 @@ data class Friend(val map: MutableMap<String, String?>, val photo: Bitmap?) {
val name: String? val name: String?
get() = map[Contacts.DISPLAY_NAME] get() = map[Contacts.DISPLAY_NAME]
val firstName: String?
get() = name?.split(Regex("\\s"))?.firstOrNull()
val id: Int val id: Int
get() = map[Contacts._ID]!!.toInt() get() = map[Contacts._ID]!!.toInt()
val phone: String? val phone: String?
get() = map["data1"] get() = if(map[Contacts.HAS_PHONE_NUMBER] == "1") map["data1"] else null
val lastTimeStamp = map[Contacts.LAST_TIME_CONTACTED]!!.toLong() val lastTimeStamp = map[Contacts.LAST_TIME_CONTACTED]!!.toLong()
......
...@@ -2,11 +2,10 @@ package fr.plnech.dunbar.notif ...@@ -2,11 +2,10 @@ package fr.plnech.dunbar.notif
import android.app.* import android.app.*
import android.content.Context import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build import android.os.Build
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import fr.plnech.dunbar.model.Friend import fr.plnech.dunbar.model.Friend
import fr.plnech.dunbar.smsIntent
val CHANNEL_ID = "dunbar" val CHANNEL_ID = "dunbar"
...@@ -34,10 +33,8 @@ class FriendReminder(val ctx: Context) { ...@@ -34,10 +33,8 @@ class FriendReminder(val ctx: Context) {
fun createNotification(friend: Friend, activity: Activity): Notification? { fun createNotification(friend: Friend, activity: Activity): Notification? {
if (!channelCreated) createNotificationChannel() if (!channelCreated) createNotificationChannel()
val uriSms = Uri.parse("smsto:1234567899") val pendingIntent: PendingIntent =
val intentSMS = Intent(Intent.ACTION_SENDTO, uriSms) PendingIntent.getActivity(activity, 0, friend.smsIntent(), 0)
intentSMS.putExtra("sms_body", "The SMS text")
val pendingIntent: PendingIntent = PendingIntent.getActivity(activity, 0, intentSMS, 0)
return NotificationCompat.Builder(ctx, CHANNEL_ID) return NotificationCompat.Builder(ctx, CHANNEL_ID)
.setContentTitle("What is ${friend.name} up to?") .setContentTitle("What is ${friend.name} up to?")
......
...@@ -86,7 +86,7 @@ class FriendsActivity : AppCompatActivity() { ...@@ -86,7 +86,7 @@ class FriendsActivity : AppCompatActivity() {
val newFriends = mutableListOf<Friend>() val newFriends = mutableListOf<Friend>()
contentResolver.query( contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, ContactsContract.CommonDataKinds.Contactables.CONTENT_URI,
null, null,
null, null,
null, null,
...@@ -111,7 +111,7 @@ class FriendsActivity : AppCompatActivity() { ...@@ -111,7 +111,7 @@ class FriendsActivity : AppCompatActivity() {
friends.addAll(newFriends) friends.addAll(newFriends)
} }
friends.sortByDescending { it.timesContacted } friends.sortBy { it.timesContacted }
} }
private fun displayFriends() { private fun displayFriends() {
......
...@@ -7,7 +7,9 @@ import android.view.ViewGroup ...@@ -7,7 +7,9 @@ import android.view.ViewGroup
import android.widget.Toast import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import fr.plnech.dunbar.R import fr.plnech.dunbar.R
import fr.plnech.dunbar.callIntent
import fr.plnech.dunbar.model.Friend import fr.plnech.dunbar.model.Friend
import fr.plnech.dunbar.smsIntent
import kotlinx.android.synthetic.main.contact.view.* import kotlinx.android.synthetic.main.contact.view.*
...@@ -48,6 +50,15 @@ class FriendsViewHolder(private val view: View) : RecyclerView.ViewHolder(view) ...@@ -48,6 +50,15 @@ class FriendsViewHolder(private val view: View) : RecyclerView.ViewHolder(view)
append("Never interacted") append("Never interacted")
} }
friend.phone?.let { friend.phone?.let {
view.contactCall.visibility = View.VISIBLE
view.contactCall.setOnClickListener {
view.context.startActivity(friend.callIntent())
}
view.contactText.visibility = View.VISIBLE
view.contactText.setOnClickListener {
view.context.startActivity(friend.smsIntent())
}
append("\n$it") append("\n$it")
} }
friend.photo?.let { friend.photo?.let {
......
...@@ -64,12 +64,14 @@ ...@@ -64,12 +64,14 @@
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
android:id="@+id/contactText" android:id="@+id/contactText"
style="@style/Widget.MaterialComponents.Button" style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginEnd="10dp" android:layout_marginEnd="10dp"
android:layout_marginStart="10dp" android:layout_marginStart="10dp"
android:text="Text" android:text="Text"
android:visibility="invisible"
tools:visibility="visible"
app:icon="@android:drawable/ic_menu_send" app:icon="@android:drawable/ic_menu_send"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1.0" app:layout_constraintHorizontal_bias="1.0"
...@@ -79,12 +81,14 @@ ...@@ -79,12 +81,14 @@
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
android:id="@+id/contactCall" android:id="@+id/contactCall"
style="@style/Widget.MaterialComponents.Button" style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginEnd="10dp" android:layout_marginEnd="10dp"
android:text="Call" android:text="Call"
app:icon="@android:drawable/ic_menu_call" app:icon="@android:drawable/ic_menu_call"
android:visibility="invisible"
tools:visibility="visible"
app:layout_constraintBottom_toBottomOf="@id/contactText" app:layout_constraintBottom_toBottomOf="@id/contactText"
app:layout_constraintHorizontal_bias="1.0" app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toRightOf="@id/contactText" app:layout_constraintLeft_toRightOf="@id/contactText"
......
...@@ -2,5 +2,6 @@ ...@@ -2,5 +2,6 @@
<resources> <resources>
<color name="colorPrimary">#008577</color> <color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color> <color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#FF5900</color> <color name="colorAccent">#FF9800</color>
<color name="colorComplementAccent">#FF5900</color>
</resources> </resources>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment