mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-18 20:08:53 +01:00
Reorganize packages
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package com.moez.QKSMS.common
|
||||
|
||||
import android.app.Application
|
||||
import com.bugsnag.android.Bugsnag
|
||||
import com.moez.QKSMS.common.di.AppComponentManager
|
||||
import io.realm.Realm
|
||||
import io.realm.RealmConfiguration
|
||||
import timber.log.Timber
|
||||
|
||||
|
||||
class QKApplication : Application() {
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
|
||||
Bugsnag.init(this)
|
||||
|
||||
Realm.init(this)
|
||||
Realm.setDefaultConfiguration(RealmConfiguration.Builder()
|
||||
.compactOnLaunch()
|
||||
.deleteRealmIfMigrationNeeded()
|
||||
.build())
|
||||
|
||||
AppComponentManager.init(this)
|
||||
|
||||
Timber.plant(Timber.DebugTree())
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.moez.QKSMS.common.di
|
||||
|
||||
import com.moez.QKSMS.common.util.NotificationManager
|
||||
import com.moez.QKSMS.presentation.conversations.ConversationAdapter
|
||||
import com.moez.QKSMS.presentation.conversations.ConversationListViewModel
|
||||
import com.moez.QKSMS.presentation.messages.MessageAdapter
|
||||
import com.moez.QKSMS.presentation.messages.MessageListActivity
|
||||
import com.moez.QKSMS.presentation.messages.MessageListViewModel
|
||||
import com.moez.QKSMS.presentation.view.AvatarView
|
||||
import com.moez.QKSMS.receiver.*
|
||||
import dagger.Component
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
@Component(modules = arrayOf(AppModule::class))
|
||||
interface AppComponent {
|
||||
|
||||
fun inject(activity: MessageListActivity)
|
||||
|
||||
fun inject(adapter: MessageAdapter)
|
||||
fun inject(adapter: ConversationAdapter)
|
||||
|
||||
fun inject(manager: NotificationManager)
|
||||
|
||||
fun inject(receiver: SmsReceiver)
|
||||
fun inject(receiver: MessageSentReceiver)
|
||||
fun inject(receiver: MarkSeenReceiver)
|
||||
fun inject(receiver: MarkReadReceiver)
|
||||
fun inject(receiver: RemoteMessagingReceiver)
|
||||
|
||||
fun inject(view: AvatarView)
|
||||
|
||||
fun inject(viewModel: ConversationListViewModel)
|
||||
fun inject(viewModel: MessageListViewModel)
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.moez.QKSMS.common.di
|
||||
|
||||
import com.moez.QKSMS.common.QKApplication
|
||||
|
||||
internal object AppComponentManager {
|
||||
|
||||
internal lateinit var appComponent: AppComponent
|
||||
private set
|
||||
|
||||
fun init(application: QKApplication) {
|
||||
appComponent = DaggerAppComponent.builder()
|
||||
.appModule(AppModule(application))
|
||||
.build()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.moez.QKSMS.common.di
|
||||
|
||||
import android.app.Application
|
||||
import android.content.Context
|
||||
import com.moez.QKSMS.common.util.DateFormatter
|
||||
import com.moez.QKSMS.common.util.NotificationManager
|
||||
import com.moez.QKSMS.common.util.ThemeManager
|
||||
import com.moez.QKSMS.data.repository.ContactRepository
|
||||
import com.moez.QKSMS.data.repository.MessageRepository
|
||||
import com.moez.QKSMS.data.sync.SyncManager
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Module
|
||||
class AppModule(var application: Application) {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideContext(): Context {
|
||||
return application
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideDateFormatter(context: Context): DateFormatter {
|
||||
return DateFormatter(context)
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideSyncManager(context: Context, contacts: ContactRepository): SyncManager {
|
||||
return SyncManager(context, contacts)
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideThemeMaager(): ThemeManager {
|
||||
return ThemeManager()
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideNotificationHelper(context: Context, themeManager: ThemeManager): NotificationManager {
|
||||
return NotificationManager(context, themeManager)
|
||||
}
|
||||
|
||||
@Singleton
|
||||
@Provides
|
||||
fun provideMessageRepository(context: Context, notificationManager: NotificationManager): MessageRepository {
|
||||
return MessageRepository(context, notificationManager)
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideContactRepository(context: Context): ContactRepository {
|
||||
return ContactRepository(context)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.moez.QKSMS.common.util
|
||||
|
||||
import android.content.Context
|
||||
import android.text.format.DateFormat
|
||||
import com.moez.QKSMS.common.util.extensions.isSameDay
|
||||
import com.moez.QKSMS.common.util.extensions.isSameWeek
|
||||
import com.moez.QKSMS.common.util.extensions.isSameYear
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.*
|
||||
|
||||
class DateFormatter(val context: Context) {
|
||||
|
||||
/**
|
||||
* Replace 12 hour format with 24 hour format if necessary
|
||||
*/
|
||||
private fun getFormatter(pattern: String): SimpleDateFormat {
|
||||
val isUsing24HourTime = DateFormat.is24HourFormat(context)
|
||||
|
||||
return if (isUsing24HourTime) SimpleDateFormat(pattern.replace('h', 'H').replace(" a".toRegex(), ""), Locale.US)
|
||||
else SimpleDateFormat(pattern, Locale.US)
|
||||
}
|
||||
|
||||
fun getMessageTimestamp(date: Long): String {
|
||||
|
||||
val now = Calendar.getInstance()
|
||||
val then = Calendar.getInstance()
|
||||
then.timeInMillis = date
|
||||
|
||||
return when {
|
||||
now.isSameDay(then) -> getFormatter("h:mm a")
|
||||
now.isSameWeek(then) -> getFormatter("E h:mm a")
|
||||
now.isSameYear(then) -> getFormatter("MMM d, h:mm a")
|
||||
else -> getFormatter("d/MM/yy h:mm a")
|
||||
}.format(date)
|
||||
}
|
||||
|
||||
fun getConversationTimestamp(date: Long): String {
|
||||
|
||||
val now = Calendar.getInstance()
|
||||
val then = Calendar.getInstance()
|
||||
then.timeInMillis = date
|
||||
|
||||
return when {
|
||||
now.isSameDay(then) -> getFormatter("h:mm a")
|
||||
now.isSameWeek(then) -> getFormatter("E")
|
||||
now.isSameYear(then) -> getFormatter("MMM d")
|
||||
else -> getFormatter("d/MM/yy")
|
||||
}.format(date)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.moez.QKSMS.common.util
|
||||
|
||||
import android.arch.lifecycle.LiveData
|
||||
import io.realm.RealmChangeListener
|
||||
import io.realm.RealmModel
|
||||
import io.realm.RealmResults
|
||||
|
||||
class LiveRealmData<T : RealmModel>(private val results: RealmResults<T>) : LiveData<RealmResults<T>>() {
|
||||
|
||||
private val listener: RealmChangeListener<RealmResults<T>> = RealmChangeListener { results ->
|
||||
value = results
|
||||
}
|
||||
|
||||
override fun onActive() {
|
||||
super.onActive()
|
||||
results.addChangeListener(listener)
|
||||
}
|
||||
|
||||
override fun onInactive() {
|
||||
super.onInactive()
|
||||
results.removeChangeListener(listener)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package com.moez.QKSMS.common.util
|
||||
|
||||
import android.app.NotificationChannel
|
||||
import android.app.NotificationManager
|
||||
import android.app.PendingIntent
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.graphics.Color
|
||||
import android.os.Build
|
||||
import android.support.v4.app.NotificationCompat
|
||||
import android.support.v4.app.NotificationManagerCompat
|
||||
import android.support.v4.app.RemoteInput
|
||||
import com.moez.QKSMS.R
|
||||
import com.moez.QKSMS.data.repository.MessageRepository
|
||||
import com.moez.QKSMS.receiver.MarkReadReceiver
|
||||
import com.moez.QKSMS.receiver.MarkSeenReceiver
|
||||
import com.moez.QKSMS.receiver.RemoteMessagingReceiver
|
||||
import com.moez.QKSMS.presentation.conversations.ConversationListActivity
|
||||
|
||||
|
||||
class NotificationManager(private val context: Context, private val themeManager: ThemeManager) {
|
||||
|
||||
private val notificationManager = NotificationManagerCompat.from(context)
|
||||
|
||||
init {
|
||||
if (Build.VERSION.SDK_INT >= 26) {
|
||||
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
|
||||
val id = "channel_1"
|
||||
val name = "Message notifications"
|
||||
val description = "Message notifications description"
|
||||
val importance = NotificationManager.IMPORTANCE_MAX
|
||||
val channel = NotificationChannel(id, name, importance)
|
||||
|
||||
channel.description = description
|
||||
channel.enableLights(true)
|
||||
channel.lightColor = Color.WHITE
|
||||
channel.enableVibration(true)
|
||||
channel.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
|
||||
|
||||
notificationManager.createNotificationChannel(channel)
|
||||
}
|
||||
}
|
||||
|
||||
fun update(messageRepo: MessageRepository) {
|
||||
messageRepo.getUnreadUnseenMessages()
|
||||
.groupBy { message -> message.threadId }
|
||||
.forEach { group ->
|
||||
val threadId = group.key
|
||||
val messages = group.value
|
||||
val conversation = messageRepo.getConversation(threadId)
|
||||
|
||||
val style = NotificationCompat.MessagingStyle("Me")
|
||||
messages.forEach { message ->
|
||||
val name = if (message.isMe()) null else conversation?.getTitle() ?: ""
|
||||
style.addMessage(message.body, message.date, name)
|
||||
}
|
||||
|
||||
val contentIntent = Intent(context, ConversationListActivity::class.java).putExtra("threadId", threadId)
|
||||
val contentPI = PendingIntent.getActivity(context, threadId.toInt() + 10000, contentIntent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
|
||||
val seenIntent = Intent(context, MarkSeenReceiver::class.java).putExtra("threadId", threadId)
|
||||
val seenPI = PendingIntent.getBroadcast(context, threadId.toInt() + 20000, seenIntent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
|
||||
val readIntent = Intent(context, MarkReadReceiver::class.java).putExtra("threadId", threadId)
|
||||
val readPI = PendingIntent.getBroadcast(context, threadId.toInt() + 30000, readIntent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
val readAction = NotificationCompat.Action(R.drawable.ic_done_black_24dp, context.getString(R.string.notification_read), readPI)
|
||||
|
||||
val notification = NotificationCompat.Builder(context, "channel_1")
|
||||
.setColor(themeManager.color)
|
||||
.setSmallIcon(R.mipmap.ic_launcher)
|
||||
.setNumber(messages.size)
|
||||
.setAutoCancel(true)
|
||||
.setContentIntent(contentPI)
|
||||
.setDeleteIntent(seenPI)
|
||||
.addAction(readAction)
|
||||
.setStyle(style)
|
||||
|
||||
if (Build.VERSION.SDK_INT >= 24 && conversation != null) {
|
||||
notification.addAction(getReplyAction(conversation.contacts[0].address, conversation.id))
|
||||
}
|
||||
|
||||
notificationManager.notify(threadId.toInt(), notification.build())
|
||||
}
|
||||
}
|
||||
|
||||
private fun getReplyAction(address: String, threadId: Long): NotificationCompat.Action {
|
||||
val replyIntent = Intent(context, RemoteMessagingReceiver::class.java)
|
||||
.putExtra("address", address)
|
||||
.putExtra("threadId", threadId)
|
||||
val replyPI = PendingIntent.getBroadcast(context, threadId.toInt() + 40000, replyIntent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
|
||||
val responseSet = arrayListOf("Hello", "Hey").toTypedArray().sortedArray()
|
||||
val remoteInput = RemoteInput.Builder("body")
|
||||
.setLabel(context.getString(R.string.notification_reply))
|
||||
.setChoices(responseSet)
|
||||
.build()
|
||||
|
||||
return NotificationCompat.Action.Builder(
|
||||
R.drawable.ic_reply_black_24dp,
|
||||
context.getString(R.string.notification_reply), replyPI)
|
||||
.addRemoteInput(remoteInput)
|
||||
.build()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.moez.QKSMS.common.util
|
||||
|
||||
class ThemeManager {
|
||||
|
||||
val color = 0xFF008389.toInt()
|
||||
val bubbleColor = 0xFFF4F4F4.toInt()
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.moez.QKSMS.common.util.extensions
|
||||
|
||||
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)
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.moez.QKSMS.common.util.extensions
|
||||
|
||||
import android.content.Context
|
||||
import android.support.v4.content.ContextCompat
|
||||
|
||||
fun Context.getColorCompat(colorRes: Int): Int {
|
||||
return ContextCompat.getColor(this, colorRes)
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.moez.QKSMS.common.util.extensions
|
||||
|
||||
import android.database.Cursor
|
||||
import io.reactivex.Flowable
|
||||
|
||||
fun Cursor.forEach(closeOnComplete: Boolean = true, method: (Cursor) -> Unit = {}) {
|
||||
moveToPosition(-1)
|
||||
while (moveToNext()) {
|
||||
method.invoke(this)
|
||||
}
|
||||
|
||||
if (closeOnComplete) {
|
||||
close()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* We're using this simple implementation with .range() because of the
|
||||
* complexities of dealing with Backpressure with a Cursor. We can't simply
|
||||
* use a loop and call onNext() from a generator because we'll need to close
|
||||
* the cursor at the end, and if any items are still in the buffer, then
|
||||
* they will be made invalid
|
||||
*/
|
||||
fun Cursor.asFlowable(): Flowable<Cursor> {
|
||||
return Flowable.range(0, count)
|
||||
.map {
|
||||
moveToPosition(it)
|
||||
this
|
||||
}
|
||||
.doOnComplete { close() }
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.moez.QKSMS.common.util.extensions
|
||||
|
||||
import android.content.Context
|
||||
import android.util.TypedValue
|
||||
|
||||
fun Int.dpToPx(context: Context): Int {
|
||||
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, this.toFloat(), context.resources.displayMetrics).toInt()
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.moez.QKSMS.common.util.extensions
|
||||
|
||||
import io.realm.Realm
|
||||
import io.realm.RealmObject
|
||||
|
||||
fun RealmObject.insertOrUpdate() {
|
||||
val realm = Realm.getDefaultInstance()
|
||||
realm.executeTransaction { realm.insertOrUpdate(this) }
|
||||
realm.close()
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.moez.QKSMS.common.util.extensions
|
||||
|
||||
import android.content.res.ColorStateList
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.ImageView
|
||||
|
||||
fun ImageView.setTint(color: Int) {
|
||||
imageTintList = ColorStateList.valueOf(color)
|
||||
}
|
||||
|
||||
fun View.setBackgroundTint(color: Int) {
|
||||
backgroundTintList = ColorStateList.valueOf(color)
|
||||
}
|
||||
|
||||
fun View.setMargins(left: Int? = null, top: Int? = null, right: Int? = null, bottom: Int? = null) {
|
||||
val lp = layoutParams as? ViewGroup.MarginLayoutParams ?: return
|
||||
|
||||
lp.setMargins(
|
||||
left ?: lp.leftMargin,
|
||||
top ?: lp.topMargin,
|
||||
right ?: lp.rightMargin,
|
||||
bottom ?: lp.rightMargin)
|
||||
|
||||
layoutParams = lp
|
||||
}
|
||||
|
||||
fun View.setPadding(left: Int? = null, top: Int? = null, right: Int? = null, bottom: Int? = null) {
|
||||
setPadding(left ?: paddingLeft, top ?: paddingTop, right ?: paddingRight, bottom ?: paddingBottom)
|
||||
}
|
||||
Reference in New Issue
Block a user