forked from upstream-mirrors/quik
Reorganize extension functions
This commit is contained in:
@@ -5,7 +5,7 @@ import android.net.Uri
|
||||
import com.moez.QKSMS.data.model.Conversation
|
||||
import com.moez.QKSMS.data.model.Message
|
||||
import com.moez.QKSMS.data.repository.ContactRepository
|
||||
import com.moez.QKSMS.util.asFlowable
|
||||
import com.moez.QKSMS.util.extensions.asFlowable
|
||||
import io.reactivex.Flowable
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
|
||||
@@ -12,7 +12,6 @@ import com.karumi.dexter.listener.PermissionRequest
|
||||
import com.karumi.dexter.listener.multi.MultiplePermissionsListener
|
||||
import com.moez.QKSMS.R
|
||||
import com.moez.QKSMS.ui.base.QkActivity
|
||||
import com.moez.QKSMS.util.observe
|
||||
import kotlinx.android.synthetic.main.conversation_list_activity.*
|
||||
import timber.log.Timber
|
||||
|
||||
@@ -26,7 +25,7 @@ class ConversationListActivity : QkActivity(), Observer<ConversationListViewStat
|
||||
requestPermissions()
|
||||
|
||||
viewModel = ViewModelProviders.of(this)[ConversationListViewModel::class.java]
|
||||
viewModel.state.observe(this)
|
||||
viewModel.state.observe(this, this)
|
||||
|
||||
conversationList.layoutManager = LinearLayoutManager(this)
|
||||
|
||||
|
||||
@@ -8,8 +8,7 @@ import com.jakewharton.rxbinding2.view.RxView
|
||||
import com.jakewharton.rxbinding2.widget.RxTextView
|
||||
import com.moez.QKSMS.R
|
||||
import com.moez.QKSMS.ui.base.QkActivity
|
||||
import com.moez.QKSMS.util.observe
|
||||
import com.moez.QKSMS.util.setTint
|
||||
import com.moez.QKSMS.util.extensions.setTint
|
||||
import kotlinx.android.synthetic.main.message_list_activity.*
|
||||
|
||||
class MessageListActivity : QkActivity(), Observer<MessageListViewState> {
|
||||
@@ -23,7 +22,7 @@ class MessageListActivity : QkActivity(), Observer<MessageListViewState> {
|
||||
|
||||
viewModel = ViewModelProviders.of(this)[MessageListViewModel::class.java]
|
||||
viewModel.threadId = intent.getLongExtra("thread_id", 0)
|
||||
viewModel.state.observe(this)
|
||||
viewModel.state.observe(this, this)
|
||||
|
||||
messageList.layoutManager = LinearLayoutManager(this).apply { stackFromEnd = true }
|
||||
|
||||
|
||||
@@ -4,6 +4,10 @@ import android.content.Context
|
||||
import android.text.format.DateFormat
|
||||
import android.text.format.DateUtils
|
||||
import com.moez.QKSMS.R
|
||||
import com.moez.QKSMS.util.extensions.isDayAfter
|
||||
import com.moez.QKSMS.util.extensions.isSameDay
|
||||
import com.moez.QKSMS.util.extensions.isSameWeek
|
||||
import com.moez.QKSMS.util.extensions.isSameYear
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.*
|
||||
|
||||
|
||||
@@ -1,64 +0,0 @@
|
||||
package com.moez.QKSMS.util
|
||||
|
||||
import android.arch.lifecycle.LifecycleOwner
|
||||
import android.arch.lifecycle.LiveData
|
||||
import android.arch.lifecycle.Observer
|
||||
import android.content.res.ColorStateList
|
||||
import android.database.Cursor
|
||||
import android.widget.ImageView
|
||||
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 = {}) {
|
||||
moveToPosition(-1)
|
||||
while (moveToNext()) {
|
||||
method.invoke(this)
|
||||
}
|
||||
|
||||
if (closeOnComplete) {
|
||||
close()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun <T, O> LiveData<T>.observe(observer: O) where O : LifecycleOwner, O : Observer<T> {
|
||||
observe(observer, observer)
|
||||
}
|
||||
|
||||
/**
|
||||
* 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() }
|
||||
}
|
||||
|
||||
fun ImageView.setTint(color: Int) {
|
||||
imageTintList = ColorStateList.valueOf(color)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.moez.QKSMS.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)
|
||||
}
|
||||
|
||||
fun Calendar.isDayAfter(other: Calendar): Boolean {
|
||||
other.add(Calendar.DAY_OF_YEAR, 1)
|
||||
return isSameDay(other)
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.moez.QKSMS.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.util.extensions
|
||||
|
||||
import android.content.res.ColorStateList
|
||||
import android.widget.ImageView
|
||||
|
||||
fun ImageView.setTint(color: Int) {
|
||||
imageTintList = ColorStateList.valueOf(color)
|
||||
}
|
||||
Reference in New Issue
Block a user