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
1e2ee218
Commit
1e2ee218
authored
Aug 28, 2017
by
Lucio Maciel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make RealmUserRepository more readable.
parent
1483458f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
98 additions
and
86 deletions
+98
-86
RealmUserRepository.java
...t/persistence/realm/repositories/RealmUserRepository.java
+98
-86
No files found.
persistence-realm/src/main/java/chat/rocket/persistence/realm/repositories/RealmUserRepository.java
View file @
1e2ee218
...
...
@@ -3,9 +3,11 @@ package chat.rocket.persistence.realm.repositories;
import
android.os.Looper
;
import
android.support.v4.util.Pair
;
import
com.hadisatrio.optional.Optional
;
import
io.reactivex.Flowable
;
import
io.reactivex.android.schedulers.AndroidSchedulers
;
import
io.realm.Case
;
import
io.realm.Realm
;
import
io.realm.RealmResults
;
import
io.realm.Sort
;
...
...
@@ -18,92 +20,102 @@ import chat.rocket.persistence.realm.models.ddp.RealmUser;
import
hu.akarnokd.rxjava.interop.RxJavaInterop
;
public
class
RealmUserRepository
extends
RealmRepository
implements
UserRepository
{
private
final
String
hostname
;
public
RealmUserRepository
(
String
hostname
)
{
this
.
hostname
=
hostname
;
}
@Override
public
Flowable
<
Optional
<
User
>>
getCurrent
()
{
return
Flowable
.
defer
(()
->
Flowable
.
using
(
()
->
new
Pair
<>(
RealmStore
.
getRealm
(
hostname
),
Looper
.
myLooper
()),
pair
->
RxJavaInterop
.
toV2Flowable
(
pair
.
first
.
where
(
RealmUser
.
class
)
.
isNotEmpty
(
RealmUser
.
EMAILS
)
.
findAll
()
.<
RealmResults
<
RealmUser
>>
asObservable
()),
pair
->
close
(
pair
.
first
,
pair
.
second
)
)
.
unsubscribeOn
(
AndroidSchedulers
.
from
(
Looper
.
myLooper
()))
.
filter
(
it
->
it
!=
null
&&
it
.
isLoaded
()
&&
it
.
isValid
())
.
map
(
realmUsers
->
{
if
(
realmUsers
.
size
()
>
0
)
{
return
Optional
.
of
(
realmUsers
.
get
(
0
).
asUser
());
}
return
Optional
.<
User
>
absent
();
}));
}
@Override
public
Flowable
<
Optional
<
User
>>
getByUsername
(
String
username
)
{
return
Flowable
.
defer
(()
->
Flowable
.
using
(
()
->
new
Pair
<>(
RealmStore
.
getRealm
(
hostname
),
Looper
.
myLooper
()),
pair
->
{
RealmUser
realmUser
=
pair
.
first
.
where
(
RealmUser
.
class
)
.
equalTo
(
RealmUser
.
USERNAME
,
username
)
.
findFirst
();
if
(
realmUser
==
null
)
{
return
Flowable
.
just
(
Optional
.<
RealmUser
>
absent
());
}
return
RxJavaInterop
.
toV2Flowable
(
realmUser
.<
RealmUser
>
asObservable
()
.
filter
(
user
->
user
.
isLoaded
()
&&
user
.
isValid
())
.
map
(
Optional:
:
of
));
},
pair
->
close
(
pair
.
first
,
pair
.
second
)
)
.
unsubscribeOn
(
AndroidSchedulers
.
from
(
Looper
.
myLooper
()))
.
map
(
optional
->
{
if
(
optional
.
isPresent
())
{
return
Optional
.
of
(
optional
.
get
().
asUser
());
}
return
Optional
.
absent
();
}));
}
@Override
public
Flowable
<
List
<
User
>>
getSortedLikeName
(
String
name
,
int
limit
)
{
return
Flowable
.
defer
(()
->
Flowable
.
using
(
()
->
new
Pair
<>(
RealmStore
.
getRealm
(
hostname
),
Looper
.
myLooper
()),
pair
->
RxJavaInterop
.
toV2Flowable
(
pair
.
first
.
where
(
RealmUser
.
class
)
.
like
(
RealmUser
.
USERNAME
,
"*"
+
name
+
"*"
,
Case
.
INSENSITIVE
)
.
findAllSorted
(
RealmUser
.
USERNAME
,
Sort
.
DESCENDING
)
.
asObservable
()),
pair
->
close
(
pair
.
first
,
pair
.
second
)
)
.
unsubscribeOn
(
AndroidSchedulers
.
from
(
Looper
.
myLooper
()))
.
filter
(
realmUsers
->
realmUsers
!=
null
&&
realmUsers
.
isLoaded
()
&&
realmUsers
.
isValid
())
.
map
(
realmUsers
->
toList
(
safeSubList
(
realmUsers
,
0
,
limit
))));
}
private
List
<
User
>
toList
(
List
<
RealmUser
>
realmUsers
)
{
int
total
=
realmUsers
.
size
();
final
List
<
User
>
userList
=
new
ArrayList
<>(
total
);
for
(
int
i
=
0
;
i
<
total
;
i
++)
{
userList
.
add
(
realmUsers
.
get
(
i
).
asUser
());
private
final
String
hostname
;
public
RealmUserRepository
(
String
hostname
)
{
this
.
hostname
=
hostname
;
}
@Override
public
Flowable
<
Optional
<
User
>>
getCurrent
()
{
return
Flowable
.
defer
(
this
::
realmGetCurrent
)
.
unsubscribeOn
(
AndroidSchedulers
.
from
(
Looper
.
myLooper
()))
.
filter
(
it
->
it
!=
null
&&
it
.
isLoaded
()
&&
it
.
isValid
())
.
map
(
realmUsers
->
{
if
(
realmUsers
.
size
()
>
0
)
{
return
Optional
.
of
(
realmUsers
.
get
(
0
).
asUser
());
}
return
Optional
.<
User
>
absent
();
});
}
private
Flowable
<
RealmResults
<
RealmUser
>>
realmGetCurrent
()
{
return
Flowable
.
using
(
()
->
new
Pair
<>(
RealmStore
.
getRealm
(
hostname
),
Looper
.
myLooper
()),
pair
->
RxJavaInterop
.
toV2Flowable
(
pair
.
first
.
where
(
RealmUser
.
class
)
.
isNotEmpty
(
RealmUser
.
EMAILS
)
.
findAll
()
.<
RealmResults
<
RealmUser
>>
asObservable
()),
pair
->
close
(
pair
.
first
,
pair
.
second
));
}
@Override
public
Flowable
<
Optional
<
User
>>
getByUsername
(
String
username
)
{
return
Flowable
.
defer
(()
->
realmGetByUsername
(
username
))
.
unsubscribeOn
(
AndroidSchedulers
.
from
(
Looper
.
myLooper
()))
.
map
(
optional
->
{
if
(
optional
.
isPresent
())
{
return
Optional
.
of
(
optional
.
get
().
asUser
());
}
return
Optional
.
absent
();
});
}
private
Flowable
<
Optional
<
RealmUser
>>
realmGetByUsername
(
String
username
)
{
return
Flowable
.
using
(
()
->
new
Pair
<>(
RealmStore
.
getRealm
(
hostname
),
Looper
.
myLooper
()),
pair
->
realmQueryUsername
(
pair
.
first
,
username
),
pair
->
close
(
pair
.
first
,
pair
.
second
));
}
return
userList
;
}
private
Flowable
<
Optional
<
RealmUser
>>
realmQueryUsername
(
Realm
realm
,
String
username
)
{
RealmUser
realmUser
=
realm
.
where
(
RealmUser
.
class
)
.
equalTo
(
RealmUser
.
USERNAME
,
username
)
.
findFirst
();
if
(
realmUser
==
null
)
{
return
Flowable
.
just
(
Optional
.
absent
());
}
return
RxJavaInterop
.
toV2Flowable
(
realmUser
.<
RealmUser
>
asObservable
()
.
filter
(
user
->
user
.
isLoaded
()
&&
user
.
isValid
())
.
map
(
Optional:
:
of
));
}
@Override
public
Flowable
<
List
<
User
>>
getSortedLikeName
(
String
name
,
int
limit
)
{
return
Flowable
.
defer
(()
->
realmGetSortedLikeName
(
name
))
.
unsubscribeOn
(
AndroidSchedulers
.
from
(
Looper
.
myLooper
()))
.
filter
(
realmUsers
->
realmUsers
!=
null
&&
realmUsers
.
isLoaded
()
&&
realmUsers
.
isValid
())
.
map
(
realmUsers
->
toList
(
safeSubList
(
realmUsers
,
0
,
limit
)));
}
private
Flowable
<
RealmResults
<
RealmUser
>>
realmGetSortedLikeName
(
String
name
)
{
return
Flowable
.
using
(
()
->
new
Pair
<>(
RealmStore
.
getRealm
(
hostname
),
Looper
.
myLooper
()),
pair
->
RxJavaInterop
.
toV2Flowable
(
pair
.
first
.
where
(
RealmUser
.
class
)
.
like
(
RealmUser
.
USERNAME
,
"*"
+
name
+
"*"
,
Case
.
INSENSITIVE
)
.
findAllSorted
(
RealmUser
.
USERNAME
,
Sort
.
DESCENDING
)
.
asObservable
()),
pair
->
close
(
pair
.
first
,
pair
.
second
));
}
private
List
<
User
>
toList
(
List
<
RealmUser
>
realmUsers
)
{
int
total
=
realmUsers
.
size
();
final
List
<
User
>
userList
=
new
ArrayList
<>(
total
);
for
(
int
i
=
0
;
i
<
total
;
i
++)
{
userList
.
add
(
realmUsers
.
get
(
i
).
asUser
());
}
return
userList
;
}
}
\ 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