feat(Friends): Call/Text shortcuts

parent 0fac2ce4
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 {
return when {
count > 1 -> this + "s"
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?) {
val name: String?
get() = map[Contacts.DISPLAY_NAME]
val firstName: String?
get() = name?.split(Regex("\\s"))?.firstOrNull()
val id: Int
get() = map[Contacts._ID]!!.toInt()
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()
......
......@@ -2,11 +2,10 @@ package fr.plnech.dunbar.notif
import android.app.*
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import androidx.core.app.NotificationCompat
import fr.plnech.dunbar.model.Friend
import fr.plnech.dunbar.smsIntent
val CHANNEL_ID = "dunbar"
......@@ -34,10 +33,8 @@ class FriendReminder(val ctx: Context) {
fun createNotification(friend: Friend, activity: Activity): Notification? {
if (!channelCreated) createNotificationChannel()
val uriSms = Uri.parse("smsto:1234567899")
val intentSMS = Intent(Intent.ACTION_SENDTO, uriSms)
intentSMS.putExtra("sms_body", "The SMS text")
val pendingIntent: PendingIntent = PendingIntent.getActivity(activity, 0, intentSMS, 0)
val pendingIntent: PendingIntent =
PendingIntent.getActivity(activity, 0, friend.smsIntent(), 0)
return NotificationCompat.Builder(ctx, CHANNEL_ID)
.setContentTitle("What is ${friend.name} up to?")
......
......@@ -86,7 +86,7 @@ class FriendsActivity : AppCompatActivity() {
val newFriends = mutableListOf<Friend>()
contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
ContactsContract.CommonDataKinds.Contactables.CONTENT_URI,
null,
null,
null,
......@@ -111,7 +111,7 @@ class FriendsActivity : AppCompatActivity() {
friends.addAll(newFriends)
}
friends.sortByDescending { it.timesContacted }
friends.sortBy { it.timesContacted }
}
private fun displayFriends() {
......
......@@ -7,7 +7,9 @@ import android.view.ViewGroup
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import fr.plnech.dunbar.R
import fr.plnech.dunbar.callIntent
import fr.plnech.dunbar.model.Friend
import fr.plnech.dunbar.smsIntent
import kotlinx.android.synthetic.main.contact.view.*
......@@ -48,6 +50,15 @@ class FriendsViewHolder(private val view: View) : RecyclerView.ViewHolder(view)
append("Never interacted")
}
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")
}
friend.photo?.let {
......
......@@ -64,12 +64,14 @@
<com.google.android.material.button.MaterialButton
android:id="@+id/contactText"
style="@style/Widget.MaterialComponents.Button"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:text="Text"
android:visibility="invisible"
tools:visibility="visible"
app:icon="@android:drawable/ic_menu_send"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1.0"
......@@ -79,12 +81,14 @@
<com.google.android.material.button.MaterialButton
android:id="@+id/contactCall"
style="@style/Widget.MaterialComponents.Button"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:text="Call"
app:icon="@android:drawable/ic_menu_call"
android:visibility="invisible"
tools:visibility="visible"
app:layout_constraintBottom_toBottomOf="@id/contactText"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toRightOf="@id/contactText"
......
......@@ -2,5 +2,6 @@
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#FF5900</color>
<color name="colorAccent">#FF9800</color>
<color name="colorComplementAccent">#FF5900</color>
</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