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
04a10cad
Commit
04a10cad
authored
Mar 24, 2018
by
Leonardo Aramaki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement migration of data Realm to preferences
parent
f6429d6f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
56 additions
and
37 deletions
+56
-37
RocketChatApplication.kt
...ain/java/chat/rocket/android/app/RocketChatApplication.kt
+40
-37
RealmSession.kt
...a/chat/rocket/android/app/migration/model/RealmSession.kt
+16
-0
No files found.
app/src/main/java/chat/rocket/android/app/RocketChatApplication.kt
View file @
04a10cad
...
...
@@ -10,12 +10,14 @@ import chat.rocket.android.app.migration.RocketChatLibraryModule
import
chat.rocket.android.app.migration.RocketChatServerModule
import
chat.rocket.android.app.migration.model.RealmBasedServerInfo
import
chat.rocket.android.app.migration.model.RealmPublicSetting
import
chat.rocket.android.app.migration.model.RealmSession
import
chat.rocket.android.app.migration.model.RealmUser
import
chat.rocket.android.authentication.domain.model.TokenModel
import
chat.rocket.android.dagger.DaggerAppComponent
import
chat.rocket.android.helper.CrashlyticsTree
import
chat.rocket.android.
server.domain.GetCurrentServerInteracto
r
import
chat.rocket.android.server.domain.
MultiServerTokenRepository
import
chat.rocket.android.server.domain.
SettingsRepository
import
chat.rocket.android.
helper.UrlHelpe
r
import
chat.rocket.android.server.domain.
*
import
chat.rocket.android.server.domain.
model.Account
import
chat.rocket.android.widget.emoji.EmojiRepository
import
chat.rocket.common.model.Token
import
chat.rocket.core.TokenRepository
...
...
@@ -33,6 +35,8 @@ import dagger.android.HasServiceInjector
import
io.fabric.sdk.android.Fabric
import
io.realm.Realm
import
io.realm.RealmConfiguration
import
kotlinx.coroutines.experimental.CommonPool
import
kotlinx.coroutines.experimental.launch
import
timber.log.Timber
import
javax.inject.Inject
...
...
@@ -59,25 +63,29 @@ class RocketChatApplication : Application(), HasActivityInjector, HasServiceInje
lateinit
var
settingsRepository
:
SettingsRepository
@Inject
lateinit
var
tokenRepository
:
TokenRepository
@Inject
lateinit
var
accountRepository
:
AccountsRepository
@Inject
lateinit
var
saveCurrentServerRepository
:
SaveCurrentServerInteractor
override
fun
onCreate
()
{
super
.
onCreate
()
DaggerAppComponent
.
builder
().
application
(
this
).
build
().
inject
(
this
)
// TODO - remove this when we have a proper service handling connection...
initCurrentServer
()
AndroidThreeTen
.
init
(
this
)
EmojiRepository
.
load
(
this
)
setupCrashlytics
()
setupFresco
()
setupTimber
()
setupMigration
()
startMigrationIfNeeded
()
// TODO - remove this when we have a proper service handling connection...
initCurrentServer
()
}
private
fun
s
etupMigration
()
{
private
fun
s
tartMigrationIfNeeded
()
{
Realm
.
init
(
this
)
val
serveListConfiguration
=
RealmConfiguration
.
Builder
()
.
name
(
"server.list.realm"
)
...
...
@@ -90,10 +98,7 @@ class RocketChatApplication : Application(), HasActivityInjector, HasServiceInje
val
serversInfoList
=
serverRealm
.
where
(
RealmBasedServerInfo
::
class
.
java
).
findAll
().
toList
()
serversInfoList
.
forEach
{
server
->
val
hostname
=
server
.
hostname
val
authToken
=
server
.
session
val
url
=
if
(
server
.
insecure
)
"http://$hostname"
else
"https://$hostname"
val
serverLogo
=
"$url/images/logo/android-chrome-192x192.png"
val
serverBg
=
"$url/images/logo/mstile-310x150.png"
val
config
=
RealmConfiguration
.
Builder
()
.
name
(
"${server.hostname}.realm"
)
...
...
@@ -105,39 +110,38 @@ class RocketChatApplication : Application(), HasActivityInjector, HasServiceInje
val
realm
=
Realm
.
getInstance
(
config
)
val
user
=
realm
.
where
(
RealmUser
::
class
.
java
)
.
isNotEmpty
(
RealmUser
.
EMAILS
).
findFirst
()
val
session
=
realm
.
where
(
RealmSession
::
class
.
java
).
findFirst
()
if
(
user
!=
null
)
{
migrateServerInfo
(
url
,
authToken
!!
,
serverLogo
,
serverBg
,
user
)
}
migratePublicSettings
(
url
,
realm
)
if
(
user
!=
null
&&
session
!=
null
)
{
val
authToken
=
session
.
token
settingsRepository
.
get
(
url
)
migrateServerInfo
(
url
,
authToken
!!
,
settingsRepository
.
get
(
url
),
user
)
}
realm
.
close
()
}
migrateCurrentServer
(
serversInfoList
)
serverRealm
.
close
()
}
private
fun
migrateServerInfo
(
url
:
String
,
authToken
:
String
,
se
rverLogo
:
String
,
serverBg
:
String
,
user
:
RealmUser
)
{
private
fun
migrateServerInfo
(
url
:
String
,
authToken
:
String
,
se
ttings
:
PublicSettings
,
user
:
RealmUser
)
{
val
userId
=
user
.
_id
val
avatar
=
"$url/avatar/${user.username}?format=jpeg"
println
(
"token_$url: "
)
println
(
"{\n"
+
" userId: $userId,\n"
+
" authToken: $authToken\n"
+
"}"
)
println
(
"ACCOUNT_KEY: "
)
println
(
"{\n"
+
" serverUrl: $url,\n"
+
" avatar: $avatar,\n"
+
" serverLogo: $serverLogo,\n"
+
" serverBg: $serverBg\n"
+
"}"
)
val
avatar
=
UrlHelper
.
getAvatarUrl
(
url
,
user
.
username
!!
)
val
icon
=
settings
.
favicon
()
?.
let
{
UrlHelper
.
getServerLogoUrl
(
url
,
it
)
}
val
logo
=
settings
.
wideTile
()
?.
let
{
UrlHelper
.
getServerLogoUrl
(
url
,
it
)
}
val
account
=
Account
(
url
,
icon
,
logo
,
user
.
username
!!
,
avatar
)
launch
(
CommonPool
)
{
val
tokenModel
=
TokenModel
(
userId
!!
,
authToken
)
multiServerRepository
.
save
(
url
,
tokenModel
)
accountRepository
.
save
(
account
)
}
}
private
fun
migratePublicSettings
(
url
:
String
,
realm
:
Realm
)
{
println
(
"settings_$url: "
)
val
settings
=
realm
.
where
(
RealmPublicSetting
::
class
.
java
).
findAll
()
val
serverSettings
=
hashMapOf
<
String
,
Value
<
Any
>>()
...
...
@@ -159,17 +163,17 @@ class RocketChatApplication : Application(), HasActivityInjector, HasServiceInje
if
(
convertedSetting
!=
null
)
{
val
id
=
setting
.
_id
!!
serverSettings
.
put
(
setting
.
_id
!!
,
convertedSetting
)
println
(
"$id: $convertedSetting"
)
serverSettings
.
put
(
id
,
convertedSetting
)
}
}
settingsRepository
.
save
(
url
,
serverSettings
)
}
private
fun
migrateCurrentServer
(
serversList
:
List
<
RealmBasedServerInfo
>)
{
var
currentServer
=
getSharedPreferences
(
"cache"
,
Context
.
MODE_PRIVATE
)
.
getString
(
"KEY_SELECTED_SERVER_HOSTNAME"
,
null
)
currentServer
=
if
(
serversList
.
toList
().
isNotEmpty
())
{
currentServer
=
if
(
serversList
.
isNotEmpty
())
{
val
server
=
serversList
.
find
{
it
.
hostname
==
currentServer
}
val
hostname
=
server
!!
.
hostname
if
(
server
.
insecure
)
{
...
...
@@ -180,8 +184,7 @@ class RocketChatApplication : Application(), HasActivityInjector, HasServiceInje
}
else
{
"http://$currentServer"
}
println
(
"current_server: $currentServer"
)
saveCurrentServerRepository
.
save
(
currentServer
)
}
// TODO - remove this when we have a proper service handling connection...
...
...
app/src/main/java/chat/rocket/android/app/migration/model/RealmSession.kt
0 → 100644
View file @
04a10cad
package
chat.rocket.android.app.migration.model
import
io.realm.RealmObject
import
io.realm.annotations.PrimaryKey
open
class
RealmSession
:
RealmObject
()
{
@JvmField
@PrimaryKey
var
sessionId
:
Int
=
0
//only 0 is used!
@JvmField
var
token
:
String
?
=
null
@JvmField
var
tokenVerified
:
Boolean
=
false
@JvmField
var
error
:
String
?
=
null
}
\ 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