forked from upstream-mirrors/quik
add timestamps with formatting to messages
This commit is contained in:
@@ -2,16 +2,13 @@ package com.moez.QKSMS
|
|||||||
|
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
import com.bugsnag.android.Bugsnag
|
import com.bugsnag.android.Bugsnag
|
||||||
import com.moez.QKSMS.dagger.*
|
import com.moez.QKSMS.dagger.AppComponentManager
|
||||||
import io.realm.Realm
|
import io.realm.Realm
|
||||||
import io.realm.RealmConfiguration
|
import io.realm.RealmConfiguration
|
||||||
|
|
||||||
|
|
||||||
class QKApplication : Application() {
|
class QKApplication : Application() {
|
||||||
|
|
||||||
var appComponent: AppComponent? = null
|
|
||||||
private set
|
|
||||||
|
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
|
|
||||||
@@ -22,11 +19,7 @@ class QKApplication : Application() {
|
|||||||
.deleteRealmIfMigrationNeeded()
|
.deleteRealmIfMigrationNeeded()
|
||||||
.build())
|
.build())
|
||||||
|
|
||||||
appComponent = DaggerAppComponent.builder()
|
AppComponentManager.init(this)
|
||||||
.appModule(AppModule(this))
|
|
||||||
.contactModule(ContactModule())
|
|
||||||
.conversationsModule(ConversationsModule())
|
|
||||||
.build()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
package com.moez.QKSMS.dagger
|
package com.moez.QKSMS.dagger
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import com.moez.QKSMS.ui.conversations.ConversationListActivity
|
import com.moez.QKSMS.data.model.Message
|
||||||
import com.moez.QKSMS.data.repository.ConversationRepository
|
import com.moez.QKSMS.data.repository.ConversationRepository
|
||||||
|
import com.moez.QKSMS.ui.conversations.ConversationListActivity
|
||||||
|
import com.moez.QKSMS.ui.messages.MessageAdapter
|
||||||
import dagger.Component
|
import dagger.Component
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
|
||||||
@@ -11,6 +13,8 @@ import javax.inject.Singleton
|
|||||||
interface AppComponent {
|
interface AppComponent {
|
||||||
|
|
||||||
fun inject(activity: ConversationListActivity)
|
fun inject(activity: ConversationListActivity)
|
||||||
|
fun inject(message: Message)
|
||||||
|
fun inject(adapter: MessageAdapter)
|
||||||
|
|
||||||
fun provideContext(): Context
|
fun provideContext(): Context
|
||||||
fun provideConversationRepository(): ConversationRepository
|
fun provideConversationRepository(): ConversationRepository
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package com.moez.QKSMS.dagger
|
||||||
|
|
||||||
|
import com.moez.QKSMS.QKApplication
|
||||||
|
|
||||||
|
internal object AppComponentManager {
|
||||||
|
|
||||||
|
internal lateinit var appComponent: AppComponent
|
||||||
|
private set
|
||||||
|
|
||||||
|
fun init(application: QKApplication) {
|
||||||
|
appComponent = DaggerAppComponent.builder()
|
||||||
|
.appModule(AppModule(application))
|
||||||
|
.contactModule(ContactModule())
|
||||||
|
.conversationsModule(ConversationsModule())
|
||||||
|
.build()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ import android.app.Application
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import com.moez.QKSMS.data.repository.ContactRepository
|
import com.moez.QKSMS.data.repository.ContactRepository
|
||||||
import com.moez.QKSMS.data.sync.SyncManager
|
import com.moez.QKSMS.data.sync.SyncManager
|
||||||
|
import com.moez.QKSMS.util.DateFormatter
|
||||||
import dagger.Module
|
import dagger.Module
|
||||||
import dagger.Provides
|
import dagger.Provides
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
@@ -17,6 +18,12 @@ class AppModule(var application: Application) {
|
|||||||
return application
|
return application
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
@Singleton
|
||||||
|
fun provideDateFormatter(context: Context): DateFormatter {
|
||||||
|
return DateFormatter(context)
|
||||||
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
fun provideSyncManager(context: Context, contacts: ContactRepository): SyncManager {
|
fun provideSyncManager(context: Context, contacts: ContactRepository): SyncManager {
|
||||||
|
|||||||
@@ -4,8 +4,6 @@ import android.arch.lifecycle.LifecycleRegistry
|
|||||||
import android.arch.lifecycle.LifecycleRegistryOwner
|
import android.arch.lifecycle.LifecycleRegistryOwner
|
||||||
import android.support.v7.app.AppCompatActivity
|
import android.support.v7.app.AppCompatActivity
|
||||||
import android.view.MenuItem
|
import android.view.MenuItem
|
||||||
import com.moez.QKSMS.QKApplication
|
|
||||||
import com.moez.QKSMS.dagger.AppComponent
|
|
||||||
|
|
||||||
abstract class QkActivity : AppCompatActivity(), LifecycleRegistryOwner {
|
abstract class QkActivity : AppCompatActivity(), LifecycleRegistryOwner {
|
||||||
|
|
||||||
@@ -15,10 +13,6 @@ abstract class QkActivity : AppCompatActivity(), LifecycleRegistryOwner {
|
|||||||
return mRegistry
|
return mRegistry
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getAppComponent(): AppComponent? {
|
|
||||||
return (application as QKApplication).appComponent
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||||
return when(item.itemId) {
|
return when(item.itemId) {
|
||||||
android.R.id.home -> {
|
android.R.id.home -> {
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import com.karumi.dexter.PermissionToken
|
|||||||
import com.karumi.dexter.listener.PermissionRequest
|
import com.karumi.dexter.listener.PermissionRequest
|
||||||
import com.karumi.dexter.listener.multi.MultiplePermissionsListener
|
import com.karumi.dexter.listener.multi.MultiplePermissionsListener
|
||||||
import com.moez.QKSMS.R
|
import com.moez.QKSMS.R
|
||||||
|
import com.moez.QKSMS.dagger.AppComponentManager
|
||||||
import com.moez.QKSMS.ui.base.QkActivity
|
import com.moez.QKSMS.ui.base.QkActivity
|
||||||
import kotlinx.android.synthetic.main.conversation_list_activity.*
|
import kotlinx.android.synthetic.main.conversation_list_activity.*
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
@@ -22,7 +23,7 @@ class ConversationListActivity : QkActivity() {
|
|||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContentView(R.layout.conversation_list_activity)
|
setContentView(R.layout.conversation_list_activity)
|
||||||
getAppComponent()?.inject(this)
|
AppComponentManager.appComponent.inject(this)
|
||||||
|
|
||||||
conversationList.layoutManager = LinearLayoutManager(this)
|
conversationList.layoutManager = LinearLayoutManager(this)
|
||||||
conversationList.adapter = ConversationAdapter(this, viewModel.conversations)
|
conversationList.adapter = ConversationAdapter(this, viewModel.conversations)
|
||||||
|
|||||||
@@ -5,9 +5,12 @@ import android.util.Log
|
|||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import com.moez.QKSMS.R
|
import com.moez.QKSMS.R
|
||||||
|
import com.moez.QKSMS.dagger.AppComponentManager
|
||||||
import com.moez.QKSMS.data.model.Message
|
import com.moez.QKSMS.data.model.Message
|
||||||
|
import com.moez.QKSMS.util.DateFormatter
|
||||||
import io.realm.OrderedRealmCollection
|
import io.realm.OrderedRealmCollection
|
||||||
import io.realm.RealmRecyclerViewAdapter
|
import io.realm.RealmRecyclerViewAdapter
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
class MessageAdapter(context: Context, data: OrderedRealmCollection<Message>?) :
|
class MessageAdapter(context: Context, data: OrderedRealmCollection<Message>?) :
|
||||||
RealmRecyclerViewAdapter<Message, MessageViewHolder>(context, data, true) {
|
RealmRecyclerViewAdapter<Message, MessageViewHolder>(context, data, true) {
|
||||||
@@ -16,6 +19,12 @@ class MessageAdapter(context: Context, data: OrderedRealmCollection<Message>?) :
|
|||||||
val VIEWTYPE_IN = 0
|
val VIEWTYPE_IN = 0
|
||||||
val VIEWTYPE_OUT = 1
|
val VIEWTYPE_OUT = 1
|
||||||
|
|
||||||
|
@Inject lateinit var dateFormatter: DateFormatter
|
||||||
|
|
||||||
|
init {
|
||||||
|
AppComponentManager.appComponent.inject(this)
|
||||||
|
}
|
||||||
|
|
||||||
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): MessageViewHolder {
|
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): MessageViewHolder {
|
||||||
val layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
|
val layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
|
||||||
val layoutRes = if (viewType == VIEWTYPE_IN) R.layout.message_list_item_in else R.layout.message_list_item_out
|
val layoutRes = if (viewType == VIEWTYPE_IN) R.layout.message_list_item_in else R.layout.message_list_item_out
|
||||||
@@ -30,6 +39,7 @@ class MessageAdapter(context: Context, data: OrderedRealmCollection<Message>?) :
|
|||||||
}
|
}
|
||||||
|
|
||||||
viewHolder.body.text = message.body
|
viewHolder.body.text = message.body
|
||||||
|
viewHolder.timestamp.text = dateFormatter.getMessageTimestamp(message.date)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getItemViewType(position: Int): Int {
|
override fun getItemViewType(position: Int): Int {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.moez.QKSMS.ui.messages
|
|||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.support.v7.widget.LinearLayoutManager
|
import android.support.v7.widget.LinearLayoutManager
|
||||||
import com.moez.QKSMS.R
|
import com.moez.QKSMS.R
|
||||||
|
import com.moez.QKSMS.dagger.AppComponentManager
|
||||||
import com.moez.QKSMS.dagger.DaggerMessagesComponent
|
import com.moez.QKSMS.dagger.DaggerMessagesComponent
|
||||||
import com.moez.QKSMS.dagger.MessagesModule
|
import com.moez.QKSMS.dagger.MessagesModule
|
||||||
import com.moez.QKSMS.ui.base.QkActivity
|
import com.moez.QKSMS.ui.base.QkActivity
|
||||||
@@ -20,7 +21,7 @@ class MessageListActivity : QkActivity() {
|
|||||||
|
|
||||||
val threadId = intent.getLongExtra("thread_id", 0)
|
val threadId = intent.getLongExtra("thread_id", 0)
|
||||||
DaggerMessagesComponent.builder()
|
DaggerMessagesComponent.builder()
|
||||||
.appComponent(getAppComponent())
|
.appComponent(AppComponentManager.appComponent)
|
||||||
.messagesModule(MessagesModule(threadId))
|
.messagesModule(MessagesModule(threadId))
|
||||||
.build()
|
.build()
|
||||||
.inject(this)
|
.inject(this)
|
||||||
|
|||||||
@@ -8,5 +8,6 @@ import com.moez.QKSMS.R
|
|||||||
class MessageViewHolder(view: View) : RecyclerView.ViewHolder(view) {
|
class MessageViewHolder(view: View) : RecyclerView.ViewHolder(view) {
|
||||||
|
|
||||||
val body: TextView = view.findViewById(R.id.body)
|
val body: TextView = view.findViewById(R.id.body)
|
||||||
|
val timestamp: TextView = view.findViewById(R.id.timestamp)
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package com.moez.QKSMS.util
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.text.format.DateFormat
|
||||||
|
import android.text.format.DateUtils
|
||||||
|
import com.moez.QKSMS.R
|
||||||
|
import java.text.SimpleDateFormat
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
class DateFormatter(val context: Context) {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replace 12 hour format with 24 hour format if necessary
|
||||||
|
*/
|
||||||
|
private fun accountFor24HourTime(context: Context, input: SimpleDateFormat): SimpleDateFormat {
|
||||||
|
val isUsing24HourTime = DateFormat.is24HourFormat(context)
|
||||||
|
|
||||||
|
return if (isUsing24HourTime) SimpleDateFormat(input.toPattern().replace('h', 'H').replace(" a".toRegex(), ""))
|
||||||
|
else input
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getMessageTimestamp(date: Long): String {
|
||||||
|
val then = Calendar.getInstance()
|
||||||
|
then.timeInMillis = date
|
||||||
|
|
||||||
|
val now = Calendar.getInstance()
|
||||||
|
|
||||||
|
val time = ", " + accountFor24HourTime(context, SimpleDateFormat("h:mm a")).format(date)
|
||||||
|
return when {
|
||||||
|
now.isSameDay(then) -> accountFor24HourTime(context, SimpleDateFormat("h:mm a")).format(date)
|
||||||
|
now.isDayAfter(then) -> context.getString(R.string.date_yesterday) + time
|
||||||
|
now.isSameWeek(then) -> DateUtils.formatDateTime(context, date, DateUtils.FORMAT_SHOW_WEEKDAY or DateUtils.FORMAT_ABBREV_WEEKDAY) + time
|
||||||
|
now.isSameYear(then) -> DateUtils.formatDateTime(context, date, DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_NO_YEAR or DateUtils.FORMAT_ABBREV_MONTH) + time
|
||||||
|
else -> DateUtils.formatDateTime(context, date, DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_ABBREV_MONTH) + time
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,6 +2,24 @@ package com.moez.QKSMS.util
|
|||||||
|
|
||||||
import android.database.Cursor
|
import android.database.Cursor
|
||||||
import io.reactivex.Flowable
|
import io.reactivex.Flowable
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
fun Calendar.isSameDay(other: Calendar): Boolean {
|
||||||
|
return get(Calendar.YEAR) == other.get(Calendar.YEAR) && get(Calendar.DAY_OF_YEAR) == other.get(Calendar.DAY_OF_YEAR)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Calendar.isSameWeek(other: Calendar): Boolean {
|
||||||
|
return get(Calendar.YEAR) == other.get(Calendar.YEAR) && get(Calendar.WEEK_OF_YEAR) == other.get(Calendar.WEEK_OF_YEAR)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Calendar.isSameYear(other: Calendar): Boolean {
|
||||||
|
return get(Calendar.YEAR) == other.get(Calendar.YEAR)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Calendar.isDayAfter(other: Calendar): Boolean {
|
||||||
|
other.add(Calendar.DAY_OF_YEAR, 1)
|
||||||
|
return isSameDay(other)
|
||||||
|
}
|
||||||
|
|
||||||
fun Cursor.forEach(closeOnComplete: Boolean = true, method: (Cursor) -> Unit = {}) {
|
fun Cursor.forEach(closeOnComplete: Boolean = true, method: (Cursor) -> Unit = {}) {
|
||||||
moveToPosition(-1)
|
moveToPosition(-1)
|
||||||
|
|||||||
@@ -3,16 +3,26 @@
|
|||||||
xmlns:tools="http://schemas.android.com/tools"
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:orientation="vertical">
|
android:orientation="vertical"
|
||||||
|
android:padding="16dp">
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/body"
|
android:id="@+id/body"
|
||||||
android:padding="16dp"
|
|
||||||
android:layout_marginEnd="56dp"
|
|
||||||
android:gravity="start"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
style="@style/TextPrimary"
|
style="@style/TextPrimary"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginEnd="56dp"
|
||||||
|
android:layout_gravity="start"
|
||||||
|
android:gravity="start"
|
||||||
tools:text="Hey! How's it going?" />
|
tools:text="Hey! How's it going?" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/timestamp"
|
||||||
|
style="@style/TextSecondary"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginEnd="56dp"
|
||||||
|
android:gravity="start"
|
||||||
|
tools:text="2:50pm" />
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
@@ -3,16 +3,26 @@
|
|||||||
xmlns:tools="http://schemas.android.com/tools"
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:orientation="vertical">
|
android:orientation="vertical"
|
||||||
|
android:padding="16dp">
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/body"
|
android:id="@+id/body"
|
||||||
style="@style/TextPrimary"
|
style="@style/TextPrimary"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_gravity="end"
|
||||||
|
android:layout_marginStart="56dp"
|
||||||
|
android:gravity="start"
|
||||||
|
tools:text="Hey! How's it going?" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/timestamp"
|
||||||
|
style="@style/TextSecondary"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginStart="56dp"
|
android:layout_marginStart="56dp"
|
||||||
android:gravity="end"
|
android:gravity="end"
|
||||||
android:padding="16dp"
|
tools:text="2:50pm" />
|
||||||
tools:text="Hey! How's it going?" />
|
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
@@ -1,3 +1,8 @@
|
|||||||
<resources>
|
<resources>
|
||||||
|
|
||||||
<string name="app_name">QKSMS</string>
|
<string name="app_name">QKSMS</string>
|
||||||
|
|
||||||
|
<string name="date_yesterday">Yesterday</string>
|
||||||
|
<string name="date_just_now">Just now</string>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
Reference in New Issue
Block a user