Unverified Commit e7fa51c0 authored by Filipe Brito's avatar Filipe Brito Committed by GitHub

Merge pull request #2082 from dev-ritik/avatar_realtime_api

[IMPROVEMENT] Listen to changes on user avatar.
parents 24c8acd3 465a8596
package chat.rocket.android.db package chat.rocket.android.db
import android.app.Application import android.app.Application
import androidx.core.net.toUri
import chat.rocket.android.R import chat.rocket.android.R
import chat.rocket.android.db.model.BaseMessageEntity import chat.rocket.android.db.model.BaseMessageEntity
import chat.rocket.android.db.model.BaseUserEntity import chat.rocket.android.db.model.BaseUserEntity
...@@ -15,6 +16,7 @@ import chat.rocket.android.db.model.UrlEntity ...@@ -15,6 +16,7 @@ import chat.rocket.android.db.model.UrlEntity
import chat.rocket.android.db.model.UserEntity import chat.rocket.android.db.model.UserEntity
import chat.rocket.android.db.model.UserStatus import chat.rocket.android.db.model.UserStatus
import chat.rocket.android.db.model.asEntity import chat.rocket.android.db.model.asEntity
import chat.rocket.android.util.extensions.avatarUrl
import chat.rocket.android.util.extensions.exhaustive import chat.rocket.android.util.extensions.exhaustive
import chat.rocket.android.util.extensions.removeTrailingSlash import chat.rocket.android.util.extensions.removeTrailingSlash
import chat.rocket.android.util.extensions.toEntity import chat.rocket.android.util.extensions.toEntity
...@@ -23,6 +25,7 @@ import chat.rocket.android.util.retryDB ...@@ -23,6 +25,7 @@ import chat.rocket.android.util.retryDB
import chat.rocket.common.model.BaseRoom import chat.rocket.common.model.BaseRoom
import chat.rocket.common.model.RoomType import chat.rocket.common.model.RoomType
import chat.rocket.common.model.SimpleUser import chat.rocket.common.model.SimpleUser
import chat.rocket.common.model.Token
import chat.rocket.common.model.User import chat.rocket.common.model.User
import chat.rocket.core.internal.model.Subscription import chat.rocket.core.internal.model.Subscription
import chat.rocket.core.internal.realtime.socket.model.StreamMessage import chat.rocket.core.internal.realtime.socket.model.StreamMessage
...@@ -32,6 +35,7 @@ import chat.rocket.core.model.Message ...@@ -32,6 +35,7 @@ import chat.rocket.core.model.Message
import chat.rocket.core.model.Myself import chat.rocket.core.model.Myself
import chat.rocket.core.model.Room import chat.rocket.core.model.Room
import chat.rocket.core.model.userId import chat.rocket.core.model.userId
import com.facebook.drawee.backends.pipeline.Fresco
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
...@@ -48,8 +52,7 @@ import kotlin.collections.component2 ...@@ -48,8 +52,7 @@ import kotlin.collections.component2
import kotlin.collections.set import kotlin.collections.set
import kotlin.system.measureTimeMillis import kotlin.system.measureTimeMillis
class DatabaseManager(val context: Application, val serverUrl: String) { class DatabaseManager(val context: Application, val serverUrl: String, val token: Token) {
private val database: RCDatabase = androidx.room.Room.databaseBuilder( private val database: RCDatabase = androidx.room.Room.databaseBuilder(
context, context,
RCDatabase::class.java, serverUrl.databaseName() RCDatabase::class.java, serverUrl.databaseName()
...@@ -58,23 +61,23 @@ class DatabaseManager(val context: Application, val serverUrl: String) { ...@@ -58,23 +61,23 @@ class DatabaseManager(val context: Application, val serverUrl: String) {
.build() .build()
private val dbContext = newSingleThreadContext("$serverUrl-db-context") private val dbContext = newSingleThreadContext("$serverUrl-db-context")
private val dbManagerContext = newSingleThreadContext("$serverUrl-db-manager-context") private val dbManagerContext = newSingleThreadContext("$serverUrl-db-manager-context")
private val writeChannel = Channel<Operation>(Channel.UNLIMITED) private val writeChannel = Channel<Operation>(Channel.UNLIMITED)
private var dbJob: Job? = null private var dbJob: Job? = null
private val insertSubs = HashMap<String, Subscription>() private val insertSubs = HashMap<String, Subscription>()
private val insertRooms = HashMap<String, Room>() private val insertRooms = HashMap<String, Room>()
private val updateSubs = LinkedHashMap<String, Subscription>() private val updateSubs = LinkedHashMap<String, Subscription>()
private val updateRooms = LinkedHashMap<String, Room>() private val updateRooms = LinkedHashMap<String, Room>()
fun chatRoomDao(): ChatRoomDao = database.chatRoomDao()
fun userDao(): UserDao = database.userDao()
fun messageDao(): MessageDao = database.messageDao()
init { init {
start() start()
} }
fun chatRoomDao(): ChatRoomDao = database.chatRoomDao()
fun userDao(): UserDao = database.userDao()
fun messageDao(): MessageDao = database.messageDao()
fun start() { fun start() {
dbJob?.cancel() dbJob?.cancel()
dbJob = GlobalScope.launch(dbContext) { dbJob = GlobalScope.launch(dbContext) {
...@@ -189,6 +192,20 @@ class DatabaseManager(val context: Application, val serverUrl: String) { ...@@ -189,6 +192,20 @@ class DatabaseManager(val context: Application, val serverUrl: String) {
status = myself.status?.toString() ?: user.status status = myself.status?.toString() ?: user.status
) ?: myself.asUser().toEntity() ) ?: myself.asUser().toEntity()
if (myself.avatarOrigin != null && myself.active == null &&
myself.name == null && myself.username == null
) {
user?.username?.let {
Fresco.getImagePipeline().evictFromCache(
serverUrl.avatarUrl(
it,
token.userId,
token.authToken
).toUri()
)
}
}
Timber.d("UPDATING SELF: $entity") Timber.d("UPDATING SELF: $entity")
entity?.let { sendOperation(Operation.UpsertUser(it)) } entity?.let { sendOperation(Operation.UpsertUser(it)) }
} }
......
package chat.rocket.android.db package chat.rocket.android.db
import android.app.Application import android.app.Application
import chat.rocket.android.server.domain.TokenRepository
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class DatabaseManagerFactory @Inject constructor(private val context: Application) { class DatabaseManagerFactory @Inject constructor(
private val context: Application,
private val tokenRepository: TokenRepository
) {
private val cache = HashMap<String, DatabaseManager>() private val cache = HashMap<String, DatabaseManager>()
fun create(serverUrl: String): DatabaseManager { fun create(serverUrl: String): DatabaseManager {
...@@ -15,9 +19,10 @@ class DatabaseManagerFactory @Inject constructor(private val context: Applicatio ...@@ -15,9 +19,10 @@ class DatabaseManagerFactory @Inject constructor(private val context: Applicatio
return it return it
} }
Timber.d("Returning FRESH database for $serverUrl") Timber.d("Returning fresh database for $serverUrl")
val db = DatabaseManager(context, serverUrl) with(DatabaseManager(context, serverUrl, tokenRepository.get(serverUrl)!!)) {
cache[serverUrl] = db cache[serverUrl] = this
return db return this
}
} }
} }
\ No newline at end of file
...@@ -4,4 +4,4 @@ distributionPath=wrapper/dists ...@@ -4,4 +4,4 @@ distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
distributionSha256Sum=b7aedd369a26b177147bcb715f8b1fc4fe32b0a6ade0d7fd8ee5ed0c6f731f2c distributionSha256Sum=53b71812f18cdb2777e9f1b2a0f2038683907c90bdc406bc64d8b400e1fb2c3b
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment