mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-17 19:38:52 +01:00
Move packages to com.moez.QKSMS
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
|
||||
*
|
||||
* This file is part of QKSMS.
|
||||
*
|
||||
* QKSMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* QKSMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.moez.QKSMS.extensions
|
||||
|
||||
import android.database.Cursor
|
||||
import io.reactivex.Flowable
|
||||
import io.reactivex.Maybe
|
||||
import io.reactivex.subjects.MaybeSubject
|
||||
|
||||
fun Cursor.forEach(closeOnComplete: Boolean = true, method: (Cursor) -> Unit = {}) {
|
||||
moveToPosition(-1)
|
||||
while (moveToNext()) {
|
||||
method.invoke(this)
|
||||
}
|
||||
|
||||
if (closeOnComplete) {
|
||||
close()
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> Cursor.map(map: (Cursor) -> T): List<T> {
|
||||
return List(count, { position ->
|
||||
moveToPosition(position)
|
||||
map(this)
|
||||
})
|
||||
}
|
||||
|
||||
fun <T> Cursor.mapWhile(map: (Cursor) -> T, predicate: (T) -> Boolean): ArrayList<T> {
|
||||
val result = ArrayList<T>()
|
||||
|
||||
moveToPosition(-1)
|
||||
while (moveToNext()) {
|
||||
val item = map(this)
|
||||
|
||||
if (!predicate(item)) break
|
||||
|
||||
result.add(item)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 Cursor.asMaybe(): Maybe<Cursor> {
|
||||
val subject = MaybeSubject.create<Cursor>()
|
||||
|
||||
if (moveToFirst()) {
|
||||
subject.onSuccess(this)
|
||||
} else {
|
||||
subject.onError(IndexOutOfBoundsException("The cursor has no items"))
|
||||
}
|
||||
|
||||
subject.doOnComplete { close() }
|
||||
return subject
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
|
||||
*
|
||||
* This file is part of QKSMS.
|
||||
*
|
||||
* QKSMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* QKSMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.moez.QKSMS.extensions
|
||||
|
||||
import com.google.android.mms.ContentType
|
||||
import com.moez.QKSMS.model.MmsPart
|
||||
|
||||
fun MmsPart.isSmil() = ContentType.APP_SMIL == type
|
||||
|
||||
fun MmsPart.isImage() = ContentType.isImageType(type)
|
||||
|
||||
fun MmsPart.isVideo() = ContentType.isVideoType(type)
|
||||
|
||||
fun MmsPart.isText() = ContentType.isTextType(type)
|
||||
|
||||
fun MmsPart.isVCard() = ContentType.TEXT_VCARD == type
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
|
||||
*
|
||||
* This file is part of QKSMS.
|
||||
*
|
||||
* QKSMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* QKSMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.moez.QKSMS.extensions
|
||||
|
||||
import io.reactivex.Observable
|
||||
import io.realm.Realm
|
||||
import io.realm.RealmModel
|
||||
import io.realm.RealmObject
|
||||
import io.realm.RealmQuery
|
||||
import io.realm.RealmResults
|
||||
import timber.log.Timber
|
||||
|
||||
fun RealmModel.insertOrUpdate() {
|
||||
val realm = Realm.getDefaultInstance()
|
||||
realm.executeTransaction { realm.insertOrUpdate(this) }
|
||||
realm.close()
|
||||
}
|
||||
|
||||
fun <T : RealmModel> Collection<T>.insertOrUpdate() {
|
||||
val realm = Realm.getDefaultInstance()
|
||||
realm.executeTransaction { realm.insertOrUpdate(this) }
|
||||
realm.close()
|
||||
}
|
||||
|
||||
fun <T : RealmObject> T.asObservable(): Observable<T> {
|
||||
return asFlowable<T>().toObservable()
|
||||
}
|
||||
|
||||
fun <T : RealmObject> RealmResults<T>.asObservable(): Observable<RealmResults<T>> {
|
||||
return asFlowable().toObservable()
|
||||
}
|
||||
|
||||
fun <T : RealmObject> RealmQuery<T>.anyOf(fieldName: String, values: LongArray): RealmQuery<T> {
|
||||
values.forEach { Timber.v("vararg: $it") }
|
||||
|
||||
return when (values.isEmpty()) {
|
||||
true -> equalTo(fieldName, -1L)
|
||||
false -> `in`(fieldName, values.toTypedArray())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
|
||||
*
|
||||
* This file is part of QKSMS.
|
||||
*
|
||||
* QKSMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* QKSMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.moez.QKSMS.extensions
|
||||
|
||||
import java.text.Normalizer
|
||||
|
||||
/**
|
||||
* Strip the accents from a string
|
||||
*/
|
||||
fun CharSequence.removeAccents() = Normalizer.normalize(this, Normalizer.Form.NFD)
|
||||
.replace(Regex("[^\\p{ASCII}]"), "")
|
||||
Reference in New Issue
Block a user