|
@@ -17,19 +17,30 @@ import com.google.firebase.messaging.FirebaseMessaging
|
|
|
import io.nexilis.service.Api
|
|
|
import io.nexilis.service.Service
|
|
|
import io.nexilis.service.apiScope
|
|
|
+import io.nexilis.service.data.daos.BuddyDao
|
|
|
+import io.nexilis.service.data.daos.GroupDao
|
|
|
+import io.nexilis.service.data.daos.GroupMemberDao
|
|
|
+import io.nexilis.service.data.daos.MessageDao
|
|
|
+import io.nexilis.service.data.daos.MessageStatusDao
|
|
|
import io.nexilis.service.data.entities.Buddy
|
|
|
import io.nexilis.service.data.entities.Group
|
|
|
import io.nexilis.service.data.entities.GroupMember
|
|
|
import io.nexilis.service.data.entities.Message
|
|
|
import io.nexilis.service.data.entities.MessageStatus
|
|
|
-import io.nexilis.service.data.rooms.ApiRoomDatabase
|
|
|
import io.nexilis.service.tag
|
|
|
import kotlinx.coroutines.launch
|
|
|
import org.json.JSONArray
|
|
|
import java.util.zip.ZipInputStream
|
|
|
import javax.inject.Inject
|
|
|
|
|
|
-class Incoming @Inject constructor(private val context: Context) {
|
|
|
+class Incoming @Inject constructor(
|
|
|
+ private val context: Context,
|
|
|
+ private val buddyDao: BuddyDao,
|
|
|
+ private val groupDao: GroupDao,
|
|
|
+ private val groupMemberDao: GroupMemberDao,
|
|
|
+ private val messageDao: MessageDao,
|
|
|
+ private val messageStatusDao: MessageStatusDao
|
|
|
+) {
|
|
|
|
|
|
fun process(data: Data, completion: ((Boolean) -> Unit)? = null) {
|
|
|
Log.d(tag, "process(${data.code}):$data")
|
|
@@ -99,9 +110,7 @@ class Incoming @Inject constructor(private val context: Context) {
|
|
|
Service.response(data.responseOk())
|
|
|
Service.sendAsync(
|
|
|
Data(
|
|
|
- code = "IFF",
|
|
|
- status = System.nanoTime().toString(),
|
|
|
- f_pin = data.f_pin
|
|
|
+ code = "IFF", status = System.nanoTime().toString(), f_pin = data.f_pin
|
|
|
)
|
|
|
)
|
|
|
FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task ->
|
|
@@ -122,9 +131,8 @@ class Incoming @Inject constructor(private val context: Context) {
|
|
|
private suspend fun pushMySelf(data: Data) {
|
|
|
try {
|
|
|
val me = context.getSharedPreferences().getString("pin", null)
|
|
|
- val dao = ApiRoomDatabase.getDatabase(context).buddyDao()
|
|
|
- dao.deleteMe()
|
|
|
- dao.insert(
|
|
|
+ buddyDao.deleteMe()
|
|
|
+ buddyDao.insert(
|
|
|
Buddy(
|
|
|
data.bodies["A00"] ?: "",
|
|
|
data.bodies["A23"] ?: "",
|
|
@@ -211,7 +219,7 @@ class Incoming @Inject constructor(private val context: Context) {
|
|
|
Log.d(tag, "pushBuddies:$jsonArray")
|
|
|
for (i in 0 until jsonArray.length()) {
|
|
|
val jsonObject = jsonArray.getJSONObject(i)
|
|
|
- ApiRoomDatabase.getDatabase(context).buddyDao().insert(
|
|
|
+ buddyDao.insert(
|
|
|
Buddy(
|
|
|
jsonObject.optString("A00") ?: "",
|
|
|
jsonObject.optString("A23") ?: "",
|
|
@@ -298,7 +306,7 @@ class Incoming @Inject constructor(private val context: Context) {
|
|
|
val jsonArray = JSONArray(d)
|
|
|
for (i in 0 until jsonArray.length()) {
|
|
|
val jsonObject = jsonArray.getJSONObject(i)
|
|
|
- ApiRoomDatabase.getDatabase(context).groupDao().insert(
|
|
|
+ groupDao.insert(
|
|
|
Group(
|
|
|
group_id = jsonObject.optString("A04") ?: "",
|
|
|
f_name = jsonObject.optString("A05") ?: "",
|
|
@@ -326,7 +334,7 @@ class Incoming @Inject constructor(private val context: Context) {
|
|
|
val members = JSONArray(jsonObject.getString("member"))
|
|
|
for (j in 0 until members.length()) {
|
|
|
val jsonMember = members.getJSONObject(j)
|
|
|
- ApiRoomDatabase.getDatabase(context).groupMemberDao().insert(
|
|
|
+ groupMemberDao.insert(
|
|
|
GroupMember(
|
|
|
group_id = jsonMember.optString("A04") ?: "",
|
|
|
f_pin = jsonMember.optString("A00") ?: "",
|
|
@@ -353,7 +361,7 @@ class Incoming @Inject constructor(private val context: Context) {
|
|
|
private suspend fun pushMember(data: Data) {
|
|
|
try {
|
|
|
val me = context.getSharedPreferences().getString("pin", null)
|
|
|
- ApiRoomDatabase.getDatabase(context).groupMemberDao().insert(
|
|
|
+ groupMemberDao.insert(
|
|
|
GroupMember(
|
|
|
group_id = data.bodies["A04"] ?: "",
|
|
|
f_pin = data.bodies["A00"] ?: "",
|
|
@@ -379,7 +387,7 @@ class Incoming @Inject constructor(private val context: Context) {
|
|
|
val messageId = data.bodies["A18"] ?: ""
|
|
|
if (messageId.isEmpty()) return
|
|
|
val me = context.getSharedPreferences().getString("pin", null)
|
|
|
- ApiRoomDatabase.getDatabase(context).messageDao().getSync(messageId)?.let {
|
|
|
+ messageDao.getSync(messageId)?.let {
|
|
|
Service.sendAck(me, data.status)
|
|
|
return
|
|
|
}
|
|
@@ -392,11 +400,10 @@ class Incoming @Inject constructor(private val context: Context) {
|
|
|
var uri: Uri? = null
|
|
|
imageId?.let { fileName ->
|
|
|
uri = Network().download(
|
|
|
- context,
|
|
|
- "https://202.158.33.27/filepalio/image/$fileName"
|
|
|
+ context, "https://202.158.33.27/filepalio/image/$fileName"
|
|
|
)
|
|
|
}
|
|
|
- ApiRoomDatabase.getDatabase(context).messageDao().insert(
|
|
|
+ messageDao.insert(
|
|
|
Message(
|
|
|
messageId,
|
|
|
fPin,
|
|
@@ -456,9 +463,9 @@ class Incoming @Inject constructor(private val context: Context) {
|
|
|
)
|
|
|
if (scope == "4") {
|
|
|
val id = chatId.ifEmpty { lPin }
|
|
|
- ApiRoomDatabase.getDatabase(context).groupMemberDao().getSync(id)?.let {
|
|
|
+ groupMemberDao.getSync(id)?.let {
|
|
|
it.forEach { member ->
|
|
|
- ApiRoomDatabase.getDatabase(context).messageStatusDao().insert(
|
|
|
+ messageStatusDao.insert(
|
|
|
MessageStatus(
|
|
|
message_id = messageId,
|
|
|
f_pin = member.f_pin,
|
|
@@ -472,7 +479,7 @@ class Incoming @Inject constructor(private val context: Context) {
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
- ApiRoomDatabase.getDatabase(context).messageStatusDao().insert(
|
|
|
+ messageStatusDao.insert(
|
|
|
MessageStatus(
|
|
|
message_id = messageId,
|
|
|
f_pin = opposite,
|
|
@@ -488,29 +495,23 @@ class Incoming @Inject constructor(private val context: Context) {
|
|
|
completion?.let { it(true) }
|
|
|
with(NotificationManagerCompat.from(context)) {
|
|
|
if (ActivityCompat.checkSelfPermission(
|
|
|
- context,
|
|
|
- Manifest.permission.POST_NOTIFICATIONS
|
|
|
+ context, Manifest.permission.POST_NOTIFICATIONS
|
|
|
) != PackageManager.PERMISSION_GRANTED
|
|
|
) {
|
|
|
return@with
|
|
|
}
|
|
|
- val buddy = ApiRoomDatabase.getDatabase(context).buddyDao().getBuddySync(opposite)
|
|
|
- ?: return@with
|
|
|
+ val buddy = buddyDao.getBuddySync(opposite) ?: return@with
|
|
|
Log.d(tag, "getBuddy:${buddy}")
|
|
|
- val request = ImageRequest.Builder(context)
|
|
|
- .addHeader("User-Agent", "Mozilla/5.0")
|
|
|
+ val request = ImageRequest.Builder(context).addHeader("User-Agent", "Mozilla/5.0")
|
|
|
.addHeader("Cookie", "PHPSESSID=123;MOBILE=123")
|
|
|
.data("https://202.158.33.27/filepalio/image/${buddy.image_id}")
|
|
|
- .transformations(CircleCropTransformation())
|
|
|
- .build()
|
|
|
- val drawable = context.getUnsafeImageLoader().execute(request).drawable as BitmapDrawable
|
|
|
- val user = Person.Builder()
|
|
|
- .setIcon(IconCompat.createWithBitmap(drawable.bitmap))
|
|
|
- .setName("${buddy.first_name} ${buddy.last_name}".trim())
|
|
|
- .build()
|
|
|
+ .transformations(CircleCropTransformation()).build()
|
|
|
+ val drawable =
|
|
|
+ context.getUnsafeImageLoader().execute(request).drawable as BitmapDrawable
|
|
|
+ val user = Person.Builder().setIcon(IconCompat.createWithBitmap(drawable.bitmap))
|
|
|
+ .setName("${buddy.first_name} ${buddy.last_name}".trim()).build()
|
|
|
notify(
|
|
|
- 112346,
|
|
|
- ChatNotification(context).getNotification(
|
|
|
+ 112346, ChatNotification(context).getNotification(
|
|
|
user,
|
|
|
"",
|
|
|
data.bodies["A07"]?.toNormalString() ?: "",
|
|
@@ -528,7 +529,7 @@ class Incoming @Inject constructor(private val context: Context) {
|
|
|
try {
|
|
|
val messageId = data.bodies["A18"] ?: ""
|
|
|
if (messageId.isEmpty()) return
|
|
|
- ApiRoomDatabase.getDatabase(context).messageDao().getSync(messageId)?.let {
|
|
|
+ messageDao.getSync(messageId)?.let {
|
|
|
Service.sendAsync(data.responseOk())
|
|
|
return
|
|
|
}
|
|
@@ -551,31 +552,20 @@ class Incoming @Inject constructor(private val context: Context) {
|
|
|
messageIds.split(",").forEach {
|
|
|
val messageId = it.trim()
|
|
|
when (status) {
|
|
|
- 3 -> ApiRoomDatabase.getDatabase(context).messageStatusDao().updateDelivered(
|
|
|
- status = status,
|
|
|
- timeDelivered = time,
|
|
|
- messageId = messageId,
|
|
|
- pin = opposite
|
|
|
+ 3 -> messageStatusDao.updateDelivered(
|
|
|
+ status = status, timeDelivered = time, messageId = messageId, pin = opposite
|
|
|
)
|
|
|
|
|
|
- 4 -> ApiRoomDatabase.getDatabase(context).messageStatusDao().updateRead(
|
|
|
- status = status,
|
|
|
- timeRead = time,
|
|
|
- messageId = messageId,
|
|
|
- pin = opposite
|
|
|
+ 4 -> messageStatusDao.updateRead(
|
|
|
+ status = status, timeRead = time, messageId = messageId, pin = opposite
|
|
|
)
|
|
|
|
|
|
- 8 -> ApiRoomDatabase.getDatabase(context).messageStatusDao().updateAck(
|
|
|
- status = status,
|
|
|
- timeAck = time,
|
|
|
- messageId = messageId,
|
|
|
- pin = opposite
|
|
|
+ 8 -> messageStatusDao.updateAck(
|
|
|
+ status = status, timeAck = time, messageId = messageId, pin = opposite
|
|
|
)
|
|
|
|
|
|
- else -> ApiRoomDatabase.getDatabase(context).messageStatusDao().updateStatus(
|
|
|
- status = status,
|
|
|
- messageId = messageId,
|
|
|
- pin = opposite
|
|
|
+ else -> messageStatusDao.updateStatus(
|
|
|
+ status = status, messageId = messageId, pin = opposite
|
|
|
)
|
|
|
}
|
|
|
}
|