Split up app module into domain/data/presentation

This commit is contained in:
Moez Bhatti
2018-03-22 00:32:07 -04:00
parent 4ef97ab854
commit d7d202392c
282 changed files with 1153 additions and 482 deletions
@@ -0,0 +1,70 @@
/*
* 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 interactor
import com.f2prateek.rx.preferences2.RxSharedPreferences
import common.util.NightModeManager
import util.Preferences
import io.reactivex.Flowable
import javax.inject.Inject
/**
* When upgrading from 2.7.3 to 3.0, migrate the preferences
*/
class MigratePreferences @Inject constructor(
private val nightModeManager: NightModeManager,
private val prefs: Preferences,
private val rxPrefs: RxSharedPreferences
) : Interactor<Unit>() {
override fun buildObservable(params: Unit): Flowable<*> {
return Flowable.just(rxPrefs.getBoolean("pref_key_welcome_seen", false))
.filter { seen -> seen.get() } // Only proceed if this value is true. It will be set false at the end
.doOnNext {
// Theme
val defaultTheme = prefs.theme().get().toString()
val oldTheme = rxPrefs.getString("pref_key_theme", defaultTheme).get()
prefs.theme().set(Integer.parseInt(oldTheme))
}
.doOnNext {
// Night mode
val background = rxPrefs.getString("pref_key_background", "light").get()
val autoNight = rxPrefs.getBoolean("pref_key_night_auto", false).get()
when {
autoNight -> nightModeManager.updateNightMode(Preferences.NIGHT_MODE_AUTO)
background == "light" -> nightModeManager.updateNightMode(Preferences.NIGHT_MODE_OFF)
background == "grey" -> nightModeManager.updateNightMode(Preferences.NIGHT_MODE_ON)
background == "black" -> {
nightModeManager.updateNightMode(Preferences.NIGHT_MODE_ON)
prefs.black.set(true)
}
}
}
.doOnNext {
// Delivery
prefs.delivery.set(rxPrefs.getBoolean("pref_key_delivery", prefs.delivery.get()).get())
}
.doOnNext {
// Unicode
prefs.unicode.set(rxPrefs.getBoolean("pref_key_strip_unicode", prefs.unicode.get()).get())
}
.doOnNext { seen -> seen.delete() } // Clear this value so that we don't need to migrate again
}
}
@@ -0,0 +1,46 @@
/*
* 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 interactor
import android.net.Uri
import io.reactivex.Flowable
import manager.NotificationManager
import repository.MessageRepository
import repository.SyncRepository
import util.extensions.mapNotNull
import javax.inject.Inject
class ReceiveMms @Inject constructor(
private val syncManager: SyncRepository,
private val messageRepo: MessageRepository,
private val notificationManager: NotificationManager,
private val updateBadge: UpdateBadge
) : Interactor<Uri>() {
override fun buildObservable(params: Uri): Flowable<*> {
return Flowable.just(params)
.flatMap { uri -> syncManager.syncMessage(uri) } // Sync the message
.mapNotNull { message -> messageRepo.getOrCreateConversation(message.threadId) } // Map message to conversation
.filter { conversation -> !conversation.blocked } // Don't notify for blocked conversations
.doOnNext { conversation -> if (conversation.archived) messageRepo.markUnarchived(conversation.id) } // Unarchive conversation if necessary
.doOnNext { conversation -> notificationManager.update(conversation.id) } // Update the notification
.flatMap { updateBadge.buildObservable(Unit) } // Update the badge
}
}
@@ -0,0 +1,53 @@
/*
* 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 interactor
import android.telephony.SmsMessage
import io.reactivex.Flowable
import manager.NotificationManager
import repository.MessageRepository
import util.extensions.mapNotNull
import javax.inject.Inject
class ReceiveSms @Inject constructor(
private val messageRepo: MessageRepository,
private val notificationManager: NotificationManager,
private val updateBadge: UpdateBadge
) : Interactor<Array<SmsMessage>>() {
override fun buildObservable(params: Array<SmsMessage>): Flowable<*> {
return Flowable.just(params)
.filter { it.isNotEmpty() }
.map { messages ->
val address = messages[0].displayOriginatingAddress
val time = messages[0].timestampMillis
val body: String = messages
.map { message -> message.displayMessageBody }
.reduce { body, new -> body + new }
messageRepo.insertReceivedSms(address, body, time) // Add the message to the db
}
.mapNotNull { message -> messageRepo.getOrCreateConversation(message.threadId) } // Map message to conversation
.filter { conversation -> !conversation.blocked } // Don't notify for blocked conversations
.doOnNext { conversation -> if (conversation.archived) messageRepo.markUnarchived(conversation.id) } // Unarchive conversation if necessary
.doOnNext { conversation -> notificationManager.update(conversation.id) } // Update the notification
.flatMap { updateBadge.buildObservable(Unit) } // Update the badge
}
}
@@ -0,0 +1,96 @@
/*
* 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 interactor
import android.content.Context
import android.graphics.Bitmap
import android.net.Uri
import com.klinker.android.send_message.Message
import com.klinker.android.send_message.Settings
import com.klinker.android.send_message.Transaction
import com.mlsdev.rximagepicker.RxImageConverters
import io.reactivex.Flowable
import repository.MessageRepository
import util.Preferences
import java.io.ByteArrayOutputStream
import javax.inject.Inject
class SendMessage @Inject constructor(
private val context: Context,
private val prefs: Preferences,
private val messageRepo: MessageRepository
) : Interactor<SendMessage.Params>() {
data class Params(val threadId: Long, val addresses: List<String>, val body: String, val attachments: List<Uri> = listOf())
override fun buildObservable(params: Params): Flowable<Unit> {
return Flowable.just(Unit)
.filter { params.addresses.isNotEmpty() }
.doOnNext {
if (params.addresses.size == 1 && params.attachments.isEmpty()) {
messageRepo.sendSmsAndPersist(params.threadId, params.addresses.first(), params.body)
} else {
sendMms(params.threadId, params.addresses, params.body, params.attachments)
}
}
.doOnNext { messageRepo.markUnarchived(params.threadId) }
}
private fun sendMms(threadId: Long, addresses: List<String>, body: String, attachments: List<Uri>) {
val settings = Settings()
val message = Message(body, addresses.toTypedArray())
val bitmaps = attachments.map { uri -> RxImageConverters.uriToBitmap(context, uri).blockingFirst() }
val totalBytes = bitmaps.sumBy { it.allocationByteCount }
bitmaps
.map { bitmap ->
val byteRatio = bitmap.allocationByteCount / totalBytes.toFloat()
shrink(bitmap, (prefs.mmsSize.get() * 1024 * byteRatio).toInt())
}
.forEach { bitmap -> message.addMedia(bitmap, "image/jpeg") }
val transaction = Transaction(context, settings)
transaction.sendNewMessage(message, threadId)
}
private fun shrink(src: Bitmap, maxBytes: Int): ByteArray {
var step = 0.0
val factor = 0.5
val quality = 60
val height = src.height
val width = src.width
val stream = ByteArrayOutputStream()
src.compress(Bitmap.CompressFormat.JPEG, 100, stream)
while (maxBytes > 0 && stream.size() > maxBytes) {
step++
val scale = Math.pow(factor, step)
stream.reset()
Bitmap.createScaledBitmap(src, (width * scale).toInt(), (height * scale).toInt(), false)
.compress(Bitmap.CompressFormat.JPEG, quality, stream)
}
return stream.toByteArray()
}
}