feat: Basic notif, better layout

parent f7f464bd
......@@ -14,7 +14,7 @@
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:name=".ui.FriendsActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
......
package fr.plnech.dunbar
package fr.plnech.dunbar.data
import android.content.Context
import android.net.Uri
......
package fr.plnech.dunbar
package fr.plnech.dunbar.model
import android.graphics.Bitmap
import android.provider.ContactsContract.Contacts
......@@ -15,6 +15,9 @@ data class Friend(val map: MutableMap<String, String?>, val photo: Bitmap?) {
val id: Int
get() = map[Contacts._ID]!!.toInt()
val phone: String?
get() = map["data1"]
val lastTimeStamp = map[Contacts.LAST_TIME_CONTACTED]!!.toLong()
val lastDate: Date?
......
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
val CHANNEL_ID = "dunbar"
class FriendReminder(val ctx: Context) {
var channelCreated = false
private fun createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
CHANNEL_ID, ctx.getString(fr.plnech.dunbar.R.string.channel_name),
NotificationManager.IMPORTANCE_DEFAULT
).apply { description = ctx.getString(fr.plnech.dunbar.R.string.channel_description) }
(ctx.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager)
.createNotificationChannel(channel)
}
channelCreated = true
}
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)
return NotificationCompat.Builder(ctx, CHANNEL_ID)
.setContentTitle("What is ${friend.name} up to?")
.setContentText("Ask them for some news!")
.setSmallIcon(fr.plnech.dunbar.R.drawable.ic_friend)
// .setStyle(
// NotificationCompat.BigTextStyle()
// .bigText("Much longer text that cannot fit one line...")
// )
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.also {
it.priority = NotificationCompat.PRIORITY_HIGH
}.build()
}
}
\ No newline at end of file
package fr.plnech.dunbar
package fr.plnech.dunbar.ui
import android.content.ContentUris
import android.graphics.Bitmap
......@@ -9,20 +9,26 @@ import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationManagerCompat
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.content_main.*
import fr.plnech.dunbar.R
import fr.plnech.dunbar.data.Messages
import fr.plnech.dunbar.model.Friend
import fr.plnech.dunbar.notif.FriendReminder
import fr.plnech.dunbar.plural
import kotlinx.android.synthetic.main.activity_friends.*
import kotlinx.android.synthetic.main.content_friends.*
import java.io.IOException
class MainActivity : AppCompatActivity() {
class FriendsActivity : AppCompatActivity() {
private lateinit var messages: Messages
private val friends = mutableListOf<Friend>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setContentView(R.layout.activity_friends)
setSupportActionBar(toolbar)
// messages = Messages(applicationContext)
......@@ -32,6 +38,18 @@ class MainActivity : AppCompatActivity() {
}
reloadFriends()
notifyFriend()
}
private fun notifyFriend() {
val idNotif = 0
with(NotificationManagerCompat.from(this)) {
val notification =
FriendReminder(applicationContext).createNotification(friends[0], this@FriendsActivity)
notification?.let {
notify(idNotif, notification)
}
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
......
package fr.plnech.dunbar
package fr.plnech.dunbar.ui
import android.text.format.DateUtils
import android.view.LayoutInflater
......@@ -6,6 +6,8 @@ import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import fr.plnech.dunbar.R
import fr.plnech.dunbar.model.Friend
import kotlinx.android.synthetic.main.contact.view.*
......@@ -14,7 +16,13 @@ class FriendsAdapter(
) : RecyclerView.Adapter<FriendsViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FriendsViewHolder =
FriendsViewHolder(LayoutInflater.from(parent.context).inflate(viewType, parent, false))
FriendsViewHolder(
LayoutInflater.from(parent.context).inflate(
viewType,
parent,
false
)
)
override fun getItemCount(): Int = friends.size
......@@ -39,9 +47,16 @@ class FriendsViewHolder(private val view: View) : RecyclerView.ViewHolder(view)
} else {
append("Never interacted")
}
friend.phone?.let {
append("\n$it")
}
friend.photo?.let {
pic.setImageBitmap(it)
}
}
view.setOnClickListener {
Toast.makeText(view.context, friend.mapString(), Toast.LENGTH_LONG).show()
println(friend.mapString())
}
}
......
......@@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
tools:context=".ui.FriendsActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
......@@ -20,7 +20,7 @@
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_main" />
<include layout="@layout/content_friends" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
......
......@@ -32,9 +32,9 @@
android:id="@+id/contactName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="15dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_marginEnd="15dp"
android:gravity="end"
android:maxLines="2"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
......@@ -49,18 +49,47 @@
android:id="@+id/contactData"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="15dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="end"
android:textAppearance="@style/TextAppearance.AppCompat.Subhead"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintBottom_toTopOf="@id/contactCall"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toRightOf="@id/pic"
app:layout_constraintLeft_toRightOf="@id/contactText"
app:layout_constraintRight_toLeftOf="@id/contactName"
app:layout_constraintTop_toBottomOf="@id/contactName"
tools:text="@tools:sample/full_names" />
tools:text="2 interactions, last 1 hour ago" />
<com.google.android.material.button.MaterialButton
android:id="@+id/contactText"
style="@style/Widget.MaterialComponents.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:text="Text"
app:icon="@android:drawable/ic_menu_send"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toRightOf="@id/pic"
app:layout_constraintRight_toLeftOf="@id/contactCall"
app:layout_constraintTop_toBottomOf="@id/contactData" />
<com.google.android.material.button.MaterialButton
android:id="@+id/contactCall"
style="@style/Widget.MaterialComponents.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:text="Call"
app:icon="@android:drawable/ic_menu_call"
app:layout_constraintBottom_toBottomOf="@id/contactText"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toRightOf="@id/contactText"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/contactData" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
......@@ -5,8 +5,8 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">
tools:context=".ui.FriendsActivity"
tools:showIn="@layout/activity_friends">
<TextView
android:id="@+id/welcomeTitle"
......
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="fr.plnech.dunbar.MainActivity">
tools:context="fr.plnech.dunbar.ui.FriendsActivity">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
......
......@@ -2,5 +2,5 @@
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="colorAccent">#FF5900</color>
</resources>
<resources>
<string name="app_name">Dunbar</string>
<string name="channel_name">Dunbar</string>
<string name="channel_description">Updates about friends you don\'t want to forget</string>
<string name="action_settings">Settings</string>
</resources>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
......
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