Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
AloqaIM-Android
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
AloqaIM-Android
Commits
b1a6a73b
Commit
b1a6a73b
authored
Apr 11, 2018
by
Leonardo Aramaki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Scheduling job when fail to send any messages
parent
b5238704
Changes
10
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
275 additions
and
47 deletions
+275
-47
AndroidManifest.xml
app/src/main/AndroidManifest.xml
+2
-2
ChatRoomPresenter.kt
...rocket/android/chatroom/presentation/ChatRoomPresenter.kt
+7
-7
MessageService.kt
...va/chat/rocket/android/chatroom/service/MessageService.kt
+45
-3
ViewModelMapper.kt
...chat/rocket/android/chatroom/viewmodel/ViewModelMapper.kt
+2
-1
AppModule.kt
.../main/java/chat/rocket/android/dagger/module/AppModule.kt
+39
-12
JobSchedulerInteractor.kt
...at/rocket/android/server/domain/JobSchedulerInteractor.kt
+8
-0
MessagesRepository.kt
...a/chat/rocket/android/server/domain/MessagesRepository.kt
+13
-9
JobSchedulerInteractorImpl.kt
...roid/server/infraestructure/JobSchedulerInteractorImpl.kt
+19
-0
MemoryMessagesRepository.kt
...ndroid/server/infraestructure/MemoryMessagesRepository.kt
+35
-13
SharedPreferencesMessagesRepository.kt
...er/infraestructure/SharedPreferencesMessagesRepository.kt
+105
-0
No files found.
app/src/main/AndroidManifest.xml
View file @
b1a6a73b
...
@@ -102,8 +102,8 @@
...
@@ -102,8 +102,8 @@
</service>
</service>
<service
<service
android:name=
".chatroom.service.MessageService"
android:name=
".chatroom.service.MessageService"
android:exported=
"
false"
>
android:exported=
"
true"
</service
>
android:permission=
"android.permission.BIND_JOB_SERVICE"
/
>
<meta-data
<meta-data
android:name=
"io.fabric.ApiKey"
android:name=
"io.fabric.ApiKey"
...
...
app/src/main/java/chat/rocket/android/chatroom/presentation/ChatRoomPresenter.kt
View file @
b1a6a73b
...
@@ -54,7 +54,8 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
...
@@ -54,7 +54,8 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
private
val
roomsRepository
:
RoomRepository
,
private
val
roomsRepository
:
RoomRepository
,
private
val
localRepository
:
LocalRepository
,
private
val
localRepository
:
LocalRepository
,
factory
:
ConnectionManagerFactory
,
factory
:
ConnectionManagerFactory
,
private
val
mapper
:
ViewModelMapper
)
{
private
val
mapper
:
ViewModelMapper
,
private
val
jobSchedulerInteractor
:
JobSchedulerInteractor
)
{
private
val
currentServer
=
serverInteractor
.
get
()
!!
private
val
currentServer
=
serverInteractor
.
get
()
!!
private
val
manager
=
factory
.
create
(
currentServer
)
private
val
manager
=
factory
.
create
(
currentServer
)
private
val
client
=
manager
.
client
private
val
client
=
manager
.
client
...
@@ -131,13 +132,11 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
...
@@ -131,13 +132,11 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
senderAlias
=
null
,
senderAlias
=
null
,
type
=
null
,
type
=
null
,
updatedAt
=
null
,
updatedAt
=
null
,
urls
=
null
urls
=
null
,
isTemporary
=
true
)
)
val
offlineMessage
=
mapper
.
map
(
newMessage
).
map
{
messagesRepository
.
save
(
newMessage
)
it
.
isTemporary
=
true
view
.
showNewMessage
(
mapper
.
map
(
newMessage
))
it
}
view
.
showNewMessage
(
offlineMessage
)
client
.
sendMessage
(
id
,
chatRoomId
,
text
)
client
.
sendMessage
(
id
,
chatRoomId
,
text
)
}
else
{
}
else
{
client
.
updateMessage
(
chatRoomId
,
messageId
,
text
)
client
.
updateMessage
(
chatRoomId
,
messageId
,
text
)
...
@@ -151,6 +150,7 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
...
@@ -151,6 +150,7 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
}.
ifNull
{
}.
ifNull
{
view
.
showGenericErrorMessage
()
view
.
showGenericErrorMessage
()
}
}
jobSchedulerInteractor
.
scheduleSendingMessages
()
}
finally
{
}
finally
{
view
.
enableSendMessageButton
()
view
.
enableSendMessageButton
()
}
}
...
...
app/src/main/java/chat/rocket/android/chatroom/service/MessageService.kt
View file @
b1a6a73b
...
@@ -3,9 +3,14 @@ package chat.rocket.android.chatroom.service
...
@@ -3,9 +3,14 @@ package chat.rocket.android.chatroom.service
import
android.app.job.JobParameters
import
android.app.job.JobParameters
import
android.app.job.JobService
import
android.app.job.JobService
import
chat.rocket.android.server.domain.CurrentServerRepository
import
chat.rocket.android.server.domain.CurrentServerRepository
import
chat.rocket.android.server.domain.MessagesRepository
import
chat.rocket.android.server.infraestructure.ConnectionManagerFactory
import
chat.rocket.android.server.infraestructure.ConnectionManagerFactory
import
chat.rocket.common.RocketChatException
import
chat.rocket.core.internal.rest.sendMessage
import
dagger.android.AndroidInjection
import
kotlinx.coroutines.experimental.CommonPool
import
kotlinx.coroutines.experimental.CommonPool
import
kotlinx.coroutines.experimental.launch
import
kotlinx.coroutines.experimental.launch
import
timber.log.Timber
import
javax.inject.Inject
import
javax.inject.Inject
class
MessageService
:
JobService
()
{
class
MessageService
:
JobService
()
{
...
@@ -13,19 +18,56 @@ class MessageService : JobService() {
...
@@ -13,19 +18,56 @@ class MessageService : JobService() {
lateinit
var
factory
:
ConnectionManagerFactory
lateinit
var
factory
:
ConnectionManagerFactory
@Inject
@Inject
lateinit
var
currentServerRepository
:
CurrentServerRepository
lateinit
var
currentServerRepository
:
CurrentServerRepository
@Inject
lateinit
var
messageRepository
:
MessagesRepository
override
fun
onCreate
()
{
super
.
onCreate
()
AndroidInjection
.
inject
(
this
)
}
override
fun
onStopJob
(
params
:
JobParameters
?):
Boolean
{
override
fun
onStopJob
(
params
:
JobParameters
?):
Boolean
{
return
tru
e
return
fals
e
}
}
override
fun
onStartJob
(
params
:
JobParameters
?):
Boolean
{
override
fun
onStartJob
(
params
:
JobParameters
?):
Boolean
{
launch
(
CommonPool
)
{
launch
(
CommonPool
)
{
val
currentServer
=
currentServerRepository
.
get
()
val
currentServer
=
currentServerRepository
.
get
()
if
(
currentServer
!=
null
)
{
if
(
currentServer
!=
null
)
{
val
connectionManager
=
factory
.
create
(
currentServer
)
params
?.
let
{
}
try
{
retrySendingMessages
(
currentServer
)
jobFinished
(
params
,
false
)
jobFinished
(
params
,
false
)
}
catch
(
ex
:
RocketChatException
)
{
Timber
.
e
(
ex
)
jobFinished
(
params
,
true
)
}
}
}
}
}
return
true
return
true
}
}
private
suspend
fun
retrySendingMessages
(
currentServer
:
String
)
{
val
temporaryMessages
=
messageRepository
.
getAllTemporary
()
if
(
temporaryMessages
.
isNotEmpty
())
{
val
connectionManager
=
factory
.
create
(
currentServer
)
val
client
=
connectionManager
.
client
temporaryMessages
.
forEach
{
message
->
client
.
sendMessage
(
message
=
message
.
message
,
messageId
=
message
.
id
,
roomId
=
message
.
roomId
,
avatar
=
message
.
avatar
,
attachments
=
message
.
attachments
,
alias
=
message
.
senderAlias
)
}
}
}
companion
object
{
const
val
EXTRA_MESSAGE_ID
=
"extra_message_id"
const
val
RETRY_SEND_MESSAGE_ID
=
1
}
}
}
\ No newline at end of file
app/src/main/java/chat/rocket/android/chatroom/viewmodel/ViewModelMapper.kt
View file @
b1a6a73b
...
@@ -212,12 +212,13 @@ class ViewModelMapper @Inject constructor(private val context: Context,
...
@@ -212,12 +212,13 @@ class ViewModelMapper @Inject constructor(private val context: Context,
val
time
=
getTime
(
message
.
timestamp
)
val
time
=
getTime
(
message
.
timestamp
)
val
avatar
=
getUserAvatar
(
message
)
val
avatar
=
getUserAvatar
(
message
)
val
preview
=
mapMessagePreview
(
message
)
val
preview
=
mapMessagePreview
(
message
)
val
isTemp
=
message
.
isTemporary
?:
false
val
content
=
getContent
(
stripMessageQuotes
(
message
))
val
content
=
getContent
(
stripMessageQuotes
(
message
))
MessageViewModel
(
message
=
stripMessageQuotes
(
message
),
rawData
=
message
,
MessageViewModel
(
message
=
stripMessageQuotes
(
message
),
rawData
=
message
,
messageId
=
message
.
id
,
avatar
=
avatar
!!
,
time
=
time
,
senderName
=
sender
,
messageId
=
message
.
id
,
avatar
=
avatar
!!
,
time
=
time
,
senderName
=
sender
,
content
=
content
,
isPinned
=
message
.
pinned
,
reactions
=
getReactions
(
message
),
content
=
content
,
isPinned
=
message
.
pinned
,
reactions
=
getReactions
(
message
),
isFirstUnread
=
false
,
preview
=
preview
)
isFirstUnread
=
false
,
preview
=
preview
,
isTemporary
=
isTemp
)
}
}
private
suspend
fun
mapMessagePreview
(
message
:
Message
):
Message
{
private
suspend
fun
mapMessagePreview
(
message
:
Message
):
Message
{
...
...
app/src/main/java/chat/rocket/android/dagger/module/AppModule.kt
View file @
b1a6a73b
...
@@ -2,7 +2,10 @@ package chat.rocket.android.dagger.module
...
@@ -2,7 +2,10 @@ package chat.rocket.android.dagger.module
import
android.app.Application
import
android.app.Application
import
android.app.NotificationManager
import
android.app.NotificationManager
import
android.app.job.JobInfo
import
android.app.job.JobScheduler
import
android.arch.persistence.room.Room
import
android.arch.persistence.room.Room
import
android.content.ComponentName
import
android.content.Context
import
android.content.Context
import
android.content.SharedPreferences
import
android.content.SharedPreferences
import
androidx.core.content.systemService
import
androidx.core.content.systemService
...
@@ -11,6 +14,7 @@ import chat.rocket.android.R
...
@@ -11,6 +14,7 @@ import chat.rocket.android.R
import
chat.rocket.android.app.RocketChatDatabase
import
chat.rocket.android.app.RocketChatDatabase
import
chat.rocket.android.authentication.infraestructure.SharedPreferencesMultiServerTokenRepository
import
chat.rocket.android.authentication.infraestructure.SharedPreferencesMultiServerTokenRepository
import
chat.rocket.android.authentication.infraestructure.SharedPreferencesTokenRepository
import
chat.rocket.android.authentication.infraestructure.SharedPreferencesTokenRepository
import
chat.rocket.android.chatroom.service.MessageService
import
chat.rocket.android.dagger.qualifier.ForFresco
import
chat.rocket.android.dagger.qualifier.ForFresco
import
chat.rocket.android.helper.FrescoAuthInterceptor
import
chat.rocket.android.helper.FrescoAuthInterceptor
import
chat.rocket.android.helper.MessageParser
import
chat.rocket.android.helper.MessageParser
...
@@ -19,19 +23,17 @@ import chat.rocket.android.infrastructure.SharedPrefsLocalRepository
...
@@ -19,19 +23,17 @@ import chat.rocket.android.infrastructure.SharedPrefsLocalRepository
import
chat.rocket.android.push.GroupedPush
import
chat.rocket.android.push.GroupedPush
import
chat.rocket.android.push.PushManager
import
chat.rocket.android.push.PushManager
import
chat.rocket.android.server.domain.*
import
chat.rocket.android.server.domain.*
import
chat.rocket.android.server.infraestructure.MemoryChatRoomsRepository
import
chat.rocket.android.server.infraestructure.*
import
chat.rocket.android.server.infraestructure.MemoryMessagesRepository
import
chat.rocket.android.server.infraestructure.MemoryRoomRepository
import
chat.rocket.android.server.infraestructure.MemoryUsersRepository
import
chat.rocket.android.server.infraestructure.ServerDao
import
chat.rocket.android.server.infraestructure.SharedPreferencesAccountsRepository
import
chat.rocket.android.server.infraestructure.SharedPreferencesSettingsRepository
import
chat.rocket.android.server.infraestructure.SharedPrefsCurrentServerRepository
import
chat.rocket.android.util.AppJsonAdapterFactory
import
chat.rocket.android.util.AppJsonAdapterFactory
import
chat.rocket.android.util.TimberLogger
import
chat.rocket.android.util.TimberLogger
import
chat.rocket.common.internal.FallbackSealedClassJsonAdapter
import
chat.rocket.common.internal.FallbackSealedClassJsonAdapter
import
chat.rocket.common.internal.ISO8601Date
import
chat.rocket.common.model.TimestampAdapter
import
chat.rocket.common.util.CalendarISO8601Converter
import
chat.rocket.common.util.Logger
import
chat.rocket.common.util.PlatformLogger
import
chat.rocket.common.util.PlatformLogger
import
chat.rocket.core.RocketChatClient
import
chat.rocket.core.RocketChatClient
import
chat.rocket.core.internal.AttachmentAdapterFactory
import
com.facebook.drawee.backends.pipeline.DraweeConfig
import
com.facebook.drawee.backends.pipeline.DraweeConfig
import
com.facebook.imagepipeline.backends.okhttp3.OkHttpImagePipelineConfigFactory
import
com.facebook.imagepipeline.backends.okhttp3.OkHttpImagePipelineConfigFactory
import
com.facebook.imagepipeline.core.ImagePipelineConfig
import
com.facebook.imagepipeline.core.ImagePipelineConfig
...
@@ -121,7 +123,7 @@ class AppModule {
...
@@ -121,7 +123,7 @@ class AppModule {
@Provides
@Provides
@ForFresco
@ForFresco
@Singleton
@Singleton
fun
provideFrescoAuthIntercept
e
r
(
tokenRepository
:
TokenRepository
,
currentServerInteractor
:
GetCurrentServerInteractor
):
Interceptor
{
fun
provideFrescoAuthIntercept
o
r
(
tokenRepository
:
TokenRepository
,
currentServerInteractor
:
GetCurrentServerInteractor
):
Interceptor
{
return
FrescoAuthInterceptor
(
tokenRepository
,
currentServerInteractor
)
return
FrescoAuthInterceptor
(
tokenRepository
,
currentServerInteractor
)
}
}
...
@@ -202,10 +204,16 @@ class AppModule {
...
@@ -202,10 +204,16 @@ class AppModule {
@Provides
@Provides
@Singleton
@Singleton
fun
provideMoshi
():
Moshi
{
fun
provideMoshi
(
logger
:
PlatformLogger
,
currentServerInteractor
:
GetCurrentServerInteractor
):
Moshi
{
val
url
=
currentServerInteractor
.
get
()
?:
""
return
Moshi
.
Builder
()
return
Moshi
.
Builder
()
.
add
(
FallbackSealedClassJsonAdapter
.
ADAPTER_FACTORY
)
.
add
(
FallbackSealedClassJsonAdapter
.
ADAPTER_FACTORY
)
.
add
(
AppJsonAdapterFactory
.
INSTANCE
)
.
add
(
AppJsonAdapterFactory
.
INSTANCE
)
.
add
(
AttachmentAdapterFactory
(
Logger
(
logger
,
url
)))
.
add
(
java
.
lang
.
Long
::
class
.
java
,
ISO8601Date
::
class
.
java
,
TimestampAdapter
(
CalendarISO8601Converter
()))
.
add
(
Long
::
class
.
java
,
ISO8601Date
::
class
.
java
,
TimestampAdapter
(
CalendarISO8601Converter
()))
.
build
()
.
build
()
}
}
...
@@ -217,8 +225,9 @@ class AppModule {
...
@@ -217,8 +225,9 @@ class AppModule {
@Provides
@Provides
@Singleton
@Singleton
fun
provideMessageRepository
():
MessagesRepository
{
fun
provideMessageRepository
(
context
:
Application
,
moshi
:
Moshi
):
MessagesRepository
{
return
MemoryMessagesRepository
()
val
preferences
=
context
.
getSharedPreferences
(
"messages"
,
Context
.
MODE_PRIVATE
)
return
SharedPreferencesMessagesRepository
(
preferences
,
moshi
)
}
}
@Provides
@Provides
...
@@ -278,4 +287,22 @@ class AppModule {
...
@@ -278,4 +287,22 @@ class AppModule {
getSettingsInteractor
:
GetSettingsInteractor
):
PushManager
{
getSettingsInteractor
:
GetSettingsInteractor
):
PushManager
{
return
PushManager
(
groupedPushes
,
manager
,
moshi
,
getAccountInteractor
,
getSettingsInteractor
,
context
)
return
PushManager
(
groupedPushes
,
manager
,
moshi
,
getAccountInteractor
,
getSettingsInteractor
,
context
)
}
}
@Provides
fun
provideJobScheduler
(
context
:
Application
):
JobScheduler
{
return
context
.
getSystemService
(
Context
.
JOB_SCHEDULER_SERVICE
)
as
JobScheduler
}
@Provides
fun
provideSendMessageJob
(
context
:
Application
):
JobInfo
{
return
JobInfo
.
Builder
(
MessageService
.
RETRY_SEND_MESSAGE_ID
,
ComponentName
(
context
,
MessageService
::
class
.
java
))
.
setRequiredNetworkType
(
JobInfo
.
NETWORK_TYPE_ANY
)
.
build
()
}
@Provides
fun
provideJobSchedulerInteractor
(
jobScheduler
:
JobScheduler
,
jobInfo
:
JobInfo
):
JobSchedulerInteractor
{
return
JobSchedulerInteractorImpl
(
jobScheduler
,
jobInfo
)
}
}
}
\ No newline at end of file
app/src/main/java/chat/rocket/android/server/domain/JobSchedulerInteractor.kt
0 → 100644
View file @
b1a6a73b
package
chat.rocket.android.server.domain
interface
JobSchedulerInteractor
{
/**
* Schedule job to retry previously failed sending messages.
*/
fun
scheduleSendingMessages
()
}
\ No newline at end of file
app/src/main/java/chat/rocket/android/server/domain/MessagesRepository.kt
View file @
b1a6a73b
...
@@ -11,7 +11,7 @@ interface MessagesRepository {
...
@@ -11,7 +11,7 @@ interface MessagesRepository {
*
*
* @return The Message object given by the id or null if message wasn't found.
* @return The Message object given by the id or null if message wasn't found.
*/
*/
fun
getById
(
id
:
String
):
Message
?
suspend
fun
getById
(
id
:
String
):
Message
?
/**
/**
* Get all messages from the current room id.
* Get all messages from the current room id.
...
@@ -19,7 +19,7 @@ interface MessagesRepository {
...
@@ -19,7 +19,7 @@ interface MessagesRepository {
* @param rid The room id.
* @param rid The room id.
* @return A list of Message objects for the room with given room id or an empty list.
* @return A list of Message objects for the room with given room id or an empty list.
*/
*/
fun
getByRoomId
(
rid
:
String
):
List
<
Message
>
suspend
fun
getByRoomId
(
rid
:
String
):
List
<
Message
>
/**
/**
* Get most recent messages up to count different users.
* Get most recent messages up to count different users.
...
@@ -29,43 +29,47 @@ interface MessagesRepository {
...
@@ -29,43 +29,47 @@ interface MessagesRepository {
*
*
* @return List of last count messages.
* @return List of last count messages.
*/
*/
fun
getRecentMessages
(
rid
:
String
,
count
:
Long
):
List
<
Message
>
suspend
fun
getRecentMessages
(
rid
:
String
,
count
:
Long
):
List
<
Message
>
/**
/**
* Get all messages. Use carefully!
* Get all messages. Use carefully!
*
*
* @return All messages or an empty list.
* @return All messages or an empty list.
*/
*/
fun
getAll
():
List
<
Message
>
suspend
fun
getAll
():
List
<
Message
>
/**
/**
* Save a single message object.
* Save a single message object.
*
*
* @param The message object to saveAll.
* @param The message object to saveAll.
*/
*/
fun
save
(
message
:
Message
)
suspend
fun
save
(
message
:
Message
)
/**
/**
* Save a list of messages.
* Save a list of messages.
*/
*/
fun
saveAll
(
newMessages
:
List
<
Message
>)
suspend
fun
saveAll
(
newMessages
:
List
<
Message
>)
/**
/**
* Removes all messages.
* Removes all messages.
*/
*/
fun
clear
()
suspend
fun
clear
()
/**
/**
* Remove message by id.
* Remove message by id.
*
*
* @param id The id of the message to remove.
* @param id The id of the message to remove.
*/
*/
fun
removeById
(
id
:
String
)
suspend
fun
removeById
(
id
:
String
)
/**
/**
* Remove all messages from a given room.
* Remove all messages from a given room.
*
*
* @param rid The room id where messages are to be removed.
* @param rid The room id where messages are to be removed.
*/
*/
fun
removeByRoomId
(
rid
:
String
)
suspend
fun
removeByRoomId
(
rid
:
String
)
suspend
fun
getAllTemporary
():
List
<
Message
>
suspend
fun
getAllTemporaryByRoomId
(
roomId
:
String
):
List
<
Message
>
}
}
\ No newline at end of file
app/src/main/java/chat/rocket/android/server/infraestructure/JobSchedulerInteractorImpl.kt
0 → 100644
View file @
b1a6a73b
package
chat.rocket.android.server.infraestructure
import
android.app.job.JobInfo
import
android.app.job.JobScheduler
import
chat.rocket.android.server.domain.JobSchedulerInteractor
import
javax.inject.Inject
/**
* Uses the Android framework [JobScheduler].
*/
class
JobSchedulerInteractorImpl
@Inject
constructor
(
private
val
jobScheduler
:
JobScheduler
,
private
val
jobInfo
:
JobInfo
)
:
JobSchedulerInteractor
{
override
fun
scheduleSendingMessages
()
{
jobScheduler
.
schedule
(
jobInfo
)
}
}
\ No newline at end of file
app/src/main/java/chat/rocket/android/server/infraestructure/MemoryMessagesRepository.kt
View file @
b1a6a73b
...
@@ -2,45 +2,67 @@ package chat.rocket.android.server.infraestructure
...
@@ -2,45 +2,67 @@ package chat.rocket.android.server.infraestructure
import
chat.rocket.android.server.domain.MessagesRepository
import
chat.rocket.android.server.domain.MessagesRepository
import
chat.rocket.core.model.Message
import
chat.rocket.core.model.Message
import
kotlinx.coroutines.experimental.CommonPool
import
kotlinx.coroutines.experimental.withContext
class
MemoryMessagesRepository
:
MessagesRepository
{
class
MemoryMessagesRepository
:
MessagesRepository
{
private
val
messages
:
HashMap
<
String
,
Message
>
=
HashMap
()
private
val
messages
:
HashMap
<
String
,
Message
>
=
HashMap
()
override
fun
getById
(
id
:
String
):
Message
?
{
override
suspend
fun
getById
(
id
:
String
):
Message
?
=
withContext
(
CommonPool
)
{
return
messages
[
id
]
return
@withContext
messages
[
id
]
}
}
override
fun
getByRoomId
(
rid
:
String
):
List
<
Message
>
{
override
suspend
fun
getByRoomId
(
rid
:
String
):
List
<
Message
>
=
withContext
(
CommonPool
)
{
return
messages
.
filter
{
it
.
value
.
roomId
==
rid
}.
values
.
toList
()
return
@withContext
messages
.
filter
{
it
.
value
.
roomId
==
rid
}.
values
.
toList
()
}
}
override
fun
getRecentMessages
(
rid
:
String
,
count
:
Long
):
List
<
Message
>
{
override
suspend
fun
getRecentMessages
(
rid
:
String
,
count
:
Long
):
List
<
Message
>
=
withContext
(
CommonPool
)
{
return
getByRoomId
(
rid
).
sortedByDescending
{
it
.
timestamp
}
return
@withContext
getByRoomId
(
rid
).
sortedByDescending
{
it
.
timestamp
}
.
distinctBy
{
it
.
sender
}.
take
(
count
.
toInt
())
.
distinctBy
{
it
.
sender
}.
take
(
count
.
toInt
())
}
}
override
fun
getAll
():
List
<
Message
>
=
messages
.
values
.
toList
()
override
suspend
fun
getAll
():
List
<
Message
>
=
withContext
(
CommonPool
)
{
return
@withContext
messages
.
values
.
toList
()
}
override
suspend
fun
getAllTemporaryByRoomId
(
roomId
:
String
):
List
<
Message
>
=
withContext
(
CommonPool
)
{
val
allByRoomId
=
getByRoomId
(
roomId
)
if
(
allByRoomId
.
isEmpty
())
{
return
@withContext
emptyList
<
Message
>()
}
return
@withContext
allByRoomId
.
filter
{
it
.
isTemporary
?:
false
&&
it
.
roomId
==
roomId
}
}
override
fun
save
(
message
:
Message
)
{
override
suspend
fun
getAllTemporary
():
List
<
Message
>
=
withContext
(
CommonPool
)
{
val
all
=
getAll
()
if
(
all
.
isEmpty
())
{
return
@withContext
emptyList
<
Message
>()
}
return
@withContext
all
.
filter
{
it
.
isTemporary
?:
false
}
}
override
suspend
fun
save
(
message
:
Message
)
=
withContext
(
CommonPool
)
{
messages
[
message
.
id
]
=
message
messages
[
message
.
id
]
=
message
}
}
override
fun
saveAll
(
newMessages
:
List
<
Message
>
)
{
override
suspend
fun
saveAll
(
newMessages
:
List
<
Message
>)
=
withContext
(
CommonPool
)
{
for
(
msg
in
newMessages
)
{
for
(
msg
in
newMessages
)
{
messages
[
msg
.
id
]
=
msg
messages
[
msg
.
id
]
=
msg
}
}
}
}
override
fun
clear
(
)
{
override
suspend
fun
clear
()
=
withContext
(
CommonPool
)
{
messages
.
clear
()
messages
.
clear
()
}
}
override
fun
removeById
(
id
:
String
)
{
override
suspend
fun
removeById
(
id
:
String
)
{
withContext
(
CommonPool
)
{
messages
.
remove
(
id
)
messages
.
remove
(
id
)
}
}
}
override
fun
removeByRoomId
(
rid
:
String
)
{
override
suspend
fun
removeByRoomId
(
rid
:
String
)
=
withContext
(
CommonPool
)
{
val
roomMessages
=
messages
.
filter
{
it
.
value
.
roomId
==
rid
}.
values
val
roomMessages
=
messages
.
filter
{
it
.
value
.
roomId
==
rid
}.
values
roomMessages
.
forEach
{
roomMessages
.
forEach
{
messages
.
remove
(
it
.
roomId
)
messages
.
remove
(
it
.
roomId
)
...
...
app/src/main/java/chat/rocket/android/server/infraestructure/SharedPreferencesMessagesRepository.kt
0 → 100644
View file @
b1a6a73b
package
chat.rocket.android.server.infraestructure
import
android.content.SharedPreferences
import
chat.rocket.android.server.domain.MessagesRepository
import
chat.rocket.core.model.Message
import
com.squareup.moshi.Moshi
import
kotlinx.coroutines.experimental.CommonPool
import
kotlinx.coroutines.experimental.withContext
class
SharedPreferencesMessagesRepository
(
private
val
prefs
:
SharedPreferences
,
private
val
moshi
:
Moshi
)
:
MessagesRepository
{
override
suspend
fun
getById
(
id
:
String
):
Message
?
=
withContext
(
CommonPool
)
{
val
adapter
=
moshi
.
adapter
<
Message
>(
Message
::
class
.
java
)
if
(
prefs
.
all
.
values
.
isEmpty
())
{
return
@withContext
null
}
val
values
=
prefs
.
all
.
values
as
Collection
<
String
>
return
@withContext
values
.
map
{
adapter
.
fromJson
(
it
)
}.
firstOrNull
{
it
?.
id
==
id
}
}
override
suspend
fun
getByRoomId
(
rid
:
String
):
List
<
Message
>
=
withContext
(
CommonPool
)
{
val
adapter
=
moshi
.
adapter
<
Message
>(
Message
::
class
.
java
)
if
(
prefs
.
all
.
values
.
isEmpty
())
{
return
@withContext
emptyList
<
Message
>()
}
val
values
=
prefs
.
all
.
values
as
Collection
<
String
>
return
@withContext
values
.
mapNotNull
{
adapter
.
fromJson
(
it
)
}.
filter
{
it
.
roomId
==
rid
}.
toList
()
}
override
suspend
fun
getRecentMessages
(
rid
:
String
,
count
:
Long
):
List
<
Message
>
{
return
getByRoomId
(
rid
).
sortedByDescending
{
it
.
timestamp
}
.
distinctBy
{
it
.
sender
}.
take
(
count
.
toInt
())
}
override
suspend
fun
getAll
():
List
<
Message
>
=
withContext
(
CommonPool
)
{
val
adapter
=
moshi
.
adapter
<
Message
>(
Message
::
class
.
java
)
if
(
prefs
.
all
.
values
.
isEmpty
())
{
return
@withContext
emptyList
<
Message
>()
}
val
values
=
prefs
.
all
.
values
as
Collection
<
String
>
return
@withContext
values
.
mapNotNull
{
adapter
.
fromJson
(
it
)
}
}
override
suspend
fun
getAllTemporary
():
List
<
Message
>
=
withContext
(
CommonPool
)
{
if
(
prefs
.
all
.
values
.
isEmpty
())
{
return
@withContext
emptyList
<
Message
>()
}
val
all
=
prefs
.
all
.
values
as
Collection
<
String
>
val
adapter
=
moshi
.
adapter
<
Message
>(
Message
::
class
.
java
)
return
@withContext
all
.
mapNotNull
{
adapter
.
fromJson
(
it
)
}
.
filter
{
it
.
isTemporary
?:
false
}
}
override
suspend
fun
getAllTemporaryByRoomId
(
roomId
:
String
):
List
<
Message
>
=
withContext
(
CommonPool
)
{
val
allByRoomId
=
getByRoomId
(
roomId
)
if
(
allByRoomId
.
isEmpty
())
{
return
@withContext
emptyList
<
Message
>()
}
return
@withContext
allByRoomId
.
filter
{
it
.
isTemporary
?:
false
}
}
override
suspend
fun
save
(
message
:
Message
)
=
withContext
(
CommonPool
)
{
val
adapter
=
moshi
.
adapter
<
Message
>(
Message
::
class
.
java
)
prefs
.
edit
().
putString
(
message
.
id
,
adapter
.
toJson
(
message
)).
apply
()
}
override
suspend
fun
saveAll
(
newMessages
:
List
<
Message
>)
=
withContext
(
CommonPool
)
{
val
adapter
=
moshi
.
adapter
<
Message
>(
Message
::
class
.
java
)
val
editor
=
prefs
.
edit
()
for
(
msg
in
newMessages
)
{
editor
.
putString
(
msg
.
id
,
adapter
.
toJson
(
msg
))
}
editor
.
apply
()
}
override
suspend
fun
clear
()
=
withContext
(
CommonPool
)
{
prefs
.
edit
().
clear
().
apply
()
}
override
suspend
fun
removeById
(
id
:
String
)
=
withContext
(
CommonPool
)
{
prefs
.
edit
().
putString
(
id
,
null
).
apply
()
}
override
suspend
fun
removeByRoomId
(
rid
:
String
)
{
withContext
(
CommonPool
)
{
val
adapter
=
moshi
.
adapter
<
Message
>(
Message
::
class
.
java
)
val
editor
=
prefs
.
edit
()
prefs
.
all
.
entries
.
forEach
{
val
value
=
it
.
value
if
(
value
is
String
)
{
val
message
=
adapter
.
fromJson
(
value
)
if
(
message
?.
roomId
==
rid
)
{
editor
.
putString
(
message
.
id
,
null
)
}
}
}
editor
.
apply
()
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment