Commit 83acd2ad authored by Leonardo Aramaki's avatar Leonardo Aramaki

Merge branch 'develop' into fix/remove-keepalive-task

parents 0921ac06 9cf18839
...@@ -2,7 +2,7 @@ package chat.rocket.android.api; ...@@ -2,7 +2,7 @@ package chat.rocket.android.api;
import android.content.Context; import android.content.Context;
import android.util.Patterns; import android.util.Patterns;
import chat.rocket.persistence.realm.models.ddp.RealmSpotlight;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
...@@ -463,15 +463,53 @@ public class MethodCallHelper { ...@@ -463,15 +463,53 @@ public class MethodCallHelper {
} }
public Task<Void> searchSpotlightUsers(String term) { public Task<Void> searchSpotlightUsers(String term) {
return searchSpotlight( return searchSpotlight(RealmSpotlightUser.class, "users", term);
RealmSpotlightUser.class, "users", term
);
} }
public Task<Void> searchSpotlightRooms(String term) { public Task<Void> searchSpotlightRooms(String term) {
return searchSpotlight( return searchSpotlight(RealmSpotlightRoom.class, "rooms", term);
RealmSpotlightRoom.class, "rooms", term }
);
public Task<Void> searchSpotlight(String term) {
return call("spotlight", TIMEOUT_MS, () ->
new JSONArray()
.put(term)
.put(JSONObject.NULL)
.put(new JSONObject().put("rooms", true).put("users", true))
).onSuccessTask(CONVERT_TO_JSON_OBJECT)
.onSuccessTask(task -> {
String jsonString = null;
final JSONObject result = task.getResult();
JSONArray roomJsonArray = (JSONArray) result.get("rooms");
if (roomJsonArray.length() > 0) {
jsonString = roomJsonArray.toString();
}
JSONArray userJsonArray = (JSONArray) result.get("users");
int usersTotal = userJsonArray.length();
if (usersTotal > 0) {
for (int i = 0; i < usersTotal; ++i) {
RealmSpotlight.Companion.customizeUserJSONObject(userJsonArray.getJSONObject(i));
}
if (jsonString == null) {
jsonString = userJsonArray.toString();
} else {
jsonString = jsonString.replace("]", "") + "," + userJsonArray.toString().replace("[", "");
}
}
if (jsonString != null) {
String jsonStringResults = jsonString;
realmHelper.executeTransaction(realm -> {
realm.delete(RealmSpotlight.class);
realm.createOrUpdateAllFromJson(RealmSpotlight.class, jsonStringResults);
return null;
});
}
return null;
});
} }
private Task<Void> searchSpotlight(Class clazz, String key, String term) { private Task<Void> searchSpotlight(Class clazz, String key, String term) {
......
...@@ -2,11 +2,11 @@ package chat.rocket.android.fragment.sidebar; ...@@ -2,11 +2,11 @@ package chat.rocket.android.fragment.sidebar;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import io.reactivex.Flowable;
import java.util.List; import java.util.List;
import chat.rocket.android.fragment.chatroom.RocketChatAbsoluteUrl;
import chat.rocket.android.shared.BaseContract; import chat.rocket.android.shared.BaseContract;
import chat.rocket.core.models.Room; import chat.rocket.core.models.Room;
import chat.rocket.core.models.SpotlightRoom; import chat.rocket.core.models.Spotlight;
import chat.rocket.core.models.User; import chat.rocket.core.models.User;
public interface SidebarMainContract { public interface SidebarMainContract {
...@@ -19,14 +19,16 @@ public interface SidebarMainContract { ...@@ -19,14 +19,16 @@ public interface SidebarMainContract {
void showRoomList(@NonNull List<Room> roomList); void showRoomList(@NonNull List<Room> roomList);
void show(User user, RocketChatAbsoluteUrl absoluteUrl); void show(User user);
} }
interface Presenter extends BaseContract.Presenter<View> { interface Presenter extends BaseContract.Presenter<View> {
void onRoomSelected(Room room); void onRoomSelected(Room room);
void onSpotlightRoomSelected(SpotlightRoom spotlightRoom); void onSpotlightSelected(Spotlight spotlight);
Flowable<List<Spotlight>> searchSpotlight(String term);
void onUserOnline(); void onUserOnline();
......
...@@ -3,6 +3,8 @@ package chat.rocket.android.fragment.sidebar; ...@@ -3,6 +3,8 @@ package chat.rocket.android.fragment.sidebar;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.util.Pair; import android.support.v4.util.Pair;
import chat.rocket.core.models.Spotlight;
import chat.rocket.persistence.realm.repositories.RealmSpotlightRepository;
import io.reactivex.Flowable; import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable; import io.reactivex.disposables.Disposable;
...@@ -17,31 +19,34 @@ import chat.rocket.android.helper.TextUtils; ...@@ -17,31 +19,34 @@ import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.shared.BasePresenter; import chat.rocket.android.shared.BasePresenter;
import chat.rocket.core.interactors.RoomInteractor; import chat.rocket.core.interactors.RoomInteractor;
import chat.rocket.core.models.Room; import chat.rocket.core.models.Room;
import chat.rocket.core.models.SpotlightRoom;
import chat.rocket.core.models.User; import chat.rocket.core.models.User;
import chat.rocket.core.repositories.UserRepository; import chat.rocket.core.repositories.UserRepository;
import java.util.List;
public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View> public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View> implements SidebarMainContract.Presenter {
implements SidebarMainContract.Presenter {
private final String hostname; private final String hostname;
private String userId;
private final RoomInteractor roomInteractor; private final RoomInteractor roomInteractor;
private final UserRepository userRepository; private final UserRepository userRepository;
private final RocketChatCache rocketChatCache; private final RocketChatCache rocketChatCache;
private final AbsoluteUrlHelper absoluteUrlHelper; private final AbsoluteUrlHelper absoluteUrlHelper;
private final MethodCallHelper methodCallHelper; private final MethodCallHelper methodCallHelper;
private RealmSpotlightRepository realmSpotlightRepository;
public SidebarMainPresenter(String hostname, RoomInteractor roomInteractor, public SidebarMainPresenter(String hostname,
RoomInteractor roomInteractor,
UserRepository userRepository, UserRepository userRepository,
RocketChatCache rocketChatCache, RocketChatCache rocketChatCache,
AbsoluteUrlHelper absoluteUrlHelper, AbsoluteUrlHelper absoluteUrlHelper,
MethodCallHelper methodCallHelper) { MethodCallHelper methodCallHelper,
RealmSpotlightRepository realmSpotlightRepository) {
this.hostname = hostname; this.hostname = hostname;
this.roomInteractor = roomInteractor; this.roomInteractor = roomInteractor;
this.userRepository = userRepository; this.userRepository = userRepository;
this.rocketChatCache = rocketChatCache; this.rocketChatCache = rocketChatCache;
this.absoluteUrlHelper = absoluteUrlHelper; this.absoluteUrlHelper = absoluteUrlHelper;
this.methodCallHelper = methodCallHelper; this.methodCallHelper = methodCallHelper;
this.realmSpotlightRepository = realmSpotlightRepository;
} }
@Override @Override
...@@ -65,7 +70,10 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View ...@@ -65,7 +70,10 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())) .subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe( .subscribe(
pair -> view.show(pair.first.orNull(), pair.second.orNull()), pair -> {
userId = pair.first.orNull().getId();
view.show(pair.first.orNull());
},
Logger::report Logger::report
); );
...@@ -78,8 +86,31 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View ...@@ -78,8 +86,31 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View
} }
@Override @Override
public void onSpotlightRoomSelected(SpotlightRoom spotlightRoom) { public Flowable<List<Spotlight>> searchSpotlight(String term) {
rocketChatCache.setSelectedRoomId(spotlightRoom.getId()); methodCallHelper.searchSpotlight(term);
return realmSpotlightRepository.getSuggestionsFor(term, 10);
}
@Override
public void onSpotlightSelected(Spotlight spotlight) {
if (spotlight.getType().equals(Room.TYPE_DIRECT_MESSAGE)) {
String username = spotlight.getName();
methodCallHelper.createDirectMessage(username)
.continueWithTask(task -> {
if (task.isCompleted()) {
rocketChatCache.setSelectedRoomId(spotlight.getId() + userId);
}
return null;
});
} else {
methodCallHelper.joinRoom(spotlight.getId())
.continueWithTask(task -> {
if (task.isCompleted()) {
rocketChatCache.setSelectedRoomId(spotlight.getId());
}
return null;
});
}
} }
@Override @Override
...@@ -104,10 +135,8 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View ...@@ -104,10 +135,8 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View
@Override @Override
public void onLogout() { public void onLogout() {
if (methodCallHelper != null) {
methodCallHelper.logout().continueWith(new LogIfError()); methodCallHelper.logout().continueWith(new LogIfError());
} }
}
private void subscribeToRooms() { private void subscribeToRooms() {
final Disposable subscription = roomInteractor.getOpenRooms() final Disposable subscription = roomInteractor.getOpenRooms()
...@@ -123,8 +152,6 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View ...@@ -123,8 +152,6 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View
} }
private void updateCurrentUserStatus(String status) { private void updateCurrentUserStatus(String status) {
if (methodCallHelper != null) {
methodCallHelper.setUserStatus(status).continueWith(new LogIfError()); methodCallHelper.setUserStatus(status).continueWith(new LogIfError());
} }
}
} }
\ No newline at end of file
...@@ -5,6 +5,7 @@ import android.support.v7.widget.RecyclerView; ...@@ -5,6 +5,7 @@ import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.ViewGroup; import android.view.ViewGroup;
import chat.rocket.core.models.Spotlight;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
...@@ -12,18 +13,17 @@ import java.util.Map; ...@@ -12,18 +13,17 @@ import java.util.Map;
import chat.rocket.android.R; import chat.rocket.android.R;
import chat.rocket.android.widget.internal.RoomListItemView; import chat.rocket.android.widget.internal.RoomListItemView;
import chat.rocket.core.models.Room; import chat.rocket.core.models.Room;
import chat.rocket.core.models.SpotlightRoom;
public class RoomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { public class RoomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static final int MODE_ROOM = 0; public static final int MODE_ROOM = 0;
public static final int MODE_SPOTLIGHT_ROOM = 1; public static final int MODE_SPOTLIGHT = 1;
private static final int VIEW_TYPE_HEADER = 0; private static final int VIEW_TYPE_HEADER = 0;
private static final int VIEW_TYPE_ROOM = 1; private static final int VIEW_TYPE_ROOM = 1;
private List<Room> roomList = Collections.emptyList(); private List<Room> roomList = Collections.emptyList();
private List<SpotlightRoom> spotlightRoomList = Collections.emptyList(); private List<Spotlight> spotlightList = Collections.emptyList();
private List<RoomListHeader> roomListHeaders = Collections.emptyList(); private List<RoomListHeader> roomListHeaders = Collections.emptyList();
private Map<Integer, RoomListHeader> headersPosition = new HashMap<>(); private Map<Integer, RoomListHeader> headersPosition = new HashMap<>();
...@@ -39,9 +39,9 @@ public class RoomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde ...@@ -39,9 +39,9 @@ public class RoomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
} }
@Override @Override
public void onItemClick(SpotlightRoom spotlightRoom) { public void onItemClick(Spotlight spotlight) {
if (externalListener != null) { if (externalListener != null) {
externalListener.onItemClick(spotlightRoom); externalListener.onItemClick(spotlight);
} }
} }
}; };
...@@ -56,8 +56,8 @@ public class RoomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde ...@@ -56,8 +56,8 @@ public class RoomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
updateRoomList(); updateRoomList();
} }
public void setSpotlightRoomList(@NonNull List<SpotlightRoom> spotlightRoomList) { public void setSpotlightList(@NonNull List<Spotlight> spotlightList) {
this.spotlightRoomList = spotlightRoomList; this.spotlightList = spotlightList;
updateRoomList(); updateRoomList();
} }
...@@ -66,7 +66,7 @@ public class RoomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde ...@@ -66,7 +66,7 @@ public class RoomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
if (mode == MODE_ROOM) { if (mode == MODE_ROOM) {
// clean up // clean up
spotlightRoomList.clear(); spotlightList.clear();
} }
} }
...@@ -96,23 +96,22 @@ public class RoomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde ...@@ -96,23 +96,22 @@ public class RoomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
((RoomListItemViewHolder) holder) ((RoomListItemViewHolder) holder)
.bind(roomList.get(position - getTotalHeadersBeforePosition(position))); .bind(roomList.get(position - getTotalHeadersBeforePosition(position)));
} else if (mode == MODE_SPOTLIGHT_ROOM) { } else if (mode == MODE_SPOTLIGHT) {
((RoomListItemViewHolder) holder) ((RoomListItemViewHolder) holder).bind(spotlightList.get(position));
.bind(spotlightRoomList.get(position));
} }
} }
@Override @Override
public int getItemCount() { public int getItemCount() {
if (mode == MODE_SPOTLIGHT_ROOM) { if (mode == MODE_SPOTLIGHT) {
return spotlightRoomList.size(); return spotlightList.size();
} }
return roomList.size() + headersPosition.size(); return roomList.size() + headersPosition.size();
} }
@Override @Override
public int getItemViewType(int position) { public int getItemViewType(int position) {
if (mode == MODE_SPOTLIGHT_ROOM) { if (mode == MODE_SPOTLIGHT) {
return VIEW_TYPE_ROOM; return VIEW_TYPE_ROOM;
} }
...@@ -188,6 +187,6 @@ public class RoomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde ...@@ -188,6 +187,6 @@ public class RoomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
public interface OnItemClickListener { public interface OnItemClickListener {
void onItemClick(Room room); void onItemClick(Room room);
void onItemClick(SpotlightRoom spotlightRoom); void onItemClick(Spotlight spotlight);
} }
} }
\ No newline at end of file
...@@ -4,7 +4,7 @@ import android.support.v7.widget.RecyclerView; ...@@ -4,7 +4,7 @@ import android.support.v7.widget.RecyclerView;
import chat.rocket.android.widget.internal.RoomListItemView; import chat.rocket.android.widget.internal.RoomListItemView;
import chat.rocket.core.models.Room; import chat.rocket.core.models.Room;
import chat.rocket.core.models.SpotlightRoom; import chat.rocket.core.models.Spotlight;
public class RoomListItemViewHolder extends RecyclerView.ViewHolder { public class RoomListItemViewHolder extends RecyclerView.ViewHolder {
public RoomListItemViewHolder(RoomListItemView itemView, public RoomListItemViewHolder(RoomListItemView itemView,
...@@ -17,8 +17,8 @@ public class RoomListItemViewHolder extends RecyclerView.ViewHolder { ...@@ -17,8 +17,8 @@ public class RoomListItemViewHolder extends RecyclerView.ViewHolder {
if (tag instanceof Room) { if (tag instanceof Room) {
listener.onItemClick((Room) view.getTag()); listener.onItemClick((Room) view.getTag());
} else if (tag instanceof SpotlightRoom) { } else if (tag instanceof Spotlight) {
listener.onItemClick((SpotlightRoom) view.getTag()); listener.onItemClick((Spotlight) view.getTag());
} }
} }
}); });
...@@ -34,13 +34,13 @@ public class RoomListItemViewHolder extends RecyclerView.ViewHolder { ...@@ -34,13 +34,13 @@ public class RoomListItemViewHolder extends RecyclerView.ViewHolder {
.setTag(room); .setTag(room);
} }
public void bind(SpotlightRoom spotlightRoom) { public void bind(Spotlight spotlight) {
((RoomListItemView) itemView) ((RoomListItemView) itemView)
.setRoomId(spotlightRoom.getId()) .setRoomId(spotlight.getId())
.setRoomName(spotlightRoom.getName()) .setRoomName(spotlight.getName())
.setRoomType(spotlightRoom.getType()) .setRoomType(spotlight.getType())
.setAlert(false) .setAlert(false)
.setUnreadCount(0) .setUnreadCount(0)
.setTag(spotlightRoom); .setTag(spotlight);
} }
} }
...@@ -5,8 +5,7 @@ ...@@ -5,8 +5,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:theme="@style/AppTheme.Dark" android:theme="@style/AppTheme.Dark"
android:visibility="invisible" tools:context="chat.rocket.android.fragment.sidebar.SidebarMainFragment">
tools:visibility="visible">
<LinearLayout <LinearLayout
android:id="@+id/user_info_container" android:id="@+id/user_info_container"
...@@ -53,7 +52,6 @@ ...@@ -53,7 +52,6 @@
android:id="@+id/toggle_user_action" android:id="@+id/toggle_user_action"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" /> android:layout_height="wrap_content" />
</LinearLayout> </LinearLayout>
<FrameLayout <FrameLayout
...@@ -76,7 +74,7 @@ ...@@ -76,7 +74,7 @@
android:id="@+id/search" android:id="@+id/search"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:queryHint="@string/navigation_search_rooms" app:queryHint="@string/spotlight_search"
app:iconifiedByDefault="false" /> app:iconifiedByDefault="false" />
</FrameLayout> </FrameLayout>
......
...@@ -62,7 +62,7 @@ ...@@ -62,7 +62,7 @@
<string name="two_factor_authentication_title">Two-factor authentication</string> <string name="two_factor_authentication_title">Two-factor authentication</string>
<string name="open_your_authentication_app_and_enter_the_code">Open your authentication app and enter the code</string> <string name="open_your_authentication_app_and_enter_the_code">Open your authentication app and enter the code</string>
<string name="two_factor_code">Two-factor code</string> <string name="two_factor_code">Two-factor code</string>
<string name="navigation_search_rooms">Search Rooms</string> <string name="spotlight_search">Search</string>
<string name="edit_message">Edit message</string> <string name="edit_message">Edit message</string>
<string name="message_options_no_message_info">Ooops. Something\'s up!</string> <string name="message_options_no_message_info">Ooops. Something\'s up!</string>
<string name="message_options_no_permissions_info">You have no permissions</string> <string name="message_options_no_permissions_info">You have no permissions</string>
......
apply plugin: 'com.android.library' apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'realm-android' apply plugin: 'realm-android'
apply plugin: 'com.jakewharton.hugo' apply plugin: 'com.jakewharton.hugo'
apply plugin: 'me.tatarka.retrolambda' apply plugin: 'me.tatarka.retrolambda'
...@@ -9,6 +10,7 @@ buildscript { ...@@ -9,6 +10,7 @@ buildscript {
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:2.3.3' classpath 'com.android.tools.build:gradle:2.3.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$rootProject.ext.kotlinVersion"
classpath 'io.realm:realm-gradle-plugin:2.3.2' classpath 'io.realm:realm-gradle-plugin:2.3.2'
classpath 'me.tatarka:gradle-retrolambda:3.5.0' classpath 'me.tatarka:gradle-retrolambda:3.5.0'
classpath 'me.tatarka.retrolambda.projectlombok:lombok.ast:0.2.3.a2' classpath 'me.tatarka.retrolambda.projectlombok:lombok.ast:0.2.3.a2'
...@@ -36,6 +38,11 @@ android { ...@@ -36,6 +38,11 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
} }
} }
sourceSets {
test.java.srcDirs += 'src/test/kotlin'
androidTest.java.srcDirs += 'src/androidTest/kotlin'
}
} }
dependencies { dependencies {
...@@ -46,6 +53,13 @@ dependencies { ...@@ -46,6 +53,13 @@ dependencies {
compile "com.android.support:appcompat-v7:$rootProject.ext.supportLibraryVersion" compile "com.android.support:appcompat-v7:$rootProject.ext.supportLibraryVersion"
compile "com.android.support:design:$rootProject.ext.supportLibraryVersion" compile "com.android.support:design:$rootProject.ext.supportLibraryVersion"
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$rootProject.ext.kotlinVersion"
testCompile "org.jetbrains.kotlin:kotlin-test:$rootProject.ext.kotlinVersion"
testCompile "org.jetbrains.kotlin:kotlin-test-junit:$rootProject.ext.kotlinVersion"
testCompile 'org.json:json:20170516'
testCompile 'org.skyscreamer:jsonassert:1.5.0'
compile 'io.reactivex.rxjava2:rxjava:2.1.0' compile 'io.reactivex.rxjava2:rxjava:2.1.0'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1' compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
......
package chat.rocket.persistence.realm.models.ddp
import chat.rocket.core.models.Spotlight
import io.realm.RealmObject
import io.realm.annotations.PrimaryKey
import org.json.JSONObject
// This class must be annotated with open to work properly with Realm (Kotlin classes are final by default).
open class RealmSpotlight : RealmObject() {
@PrimaryKey var _id: String? = null
var name: String? = null
var t: String? = null
fun asSpotlight(): Spotlight {
return Spotlight.builder()
.setId(_id)
.setName(name)
.setType(t)
.build()
}
companion object {
fun customizeUserJSONObject(userJsonObject: JSONObject) {
userJsonObject.put(Columns.NAME, userJsonObject.get("username"))
userJsonObject.put(Columns.TYPE, "d")
userJsonObject.remove("username")
userJsonObject.remove("status")
}
}
interface Columns {
companion object {
const val ID = "_id"
const val NAME = "name"
const val TYPE = "t"
}
}
}
\ No newline at end of file
package chat.rocket.persistence.realm.repositories
import android.os.Looper
import android.support.v4.util.Pair
import chat.rocket.core.models.Spotlight
import chat.rocket.core.repositories.SpotlightRepository
import chat.rocket.persistence.realm.RealmStore
import chat.rocket.persistence.realm.models.ddp.RealmSpotlight
import chat.rocket.persistence.realm.models.ddp.RealmSpotlight.Columns
import hu.akarnokd.rxjava.interop.RxJavaInterop
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
import java.util.ArrayList
class RealmSpotlightRepository(private val hostname: String) : RealmRepository(), SpotlightRepository {
override fun getSuggestionsFor(term: String, limit: Int): Flowable<List<Spotlight>> {
return Flowable.defer { Flowable.using<RealmResults<RealmSpotlight>, Pair<Realm, Looper>>({
Pair<Realm, Looper>(RealmStore.getRealm(hostname), Looper.myLooper())
}, { pair -> RxJavaInterop.toV2Flowable<RealmResults<RealmSpotlight>>(pair.first.where(RealmSpotlight::class.java)
.findAllSorted(Columns.TYPE, Sort.DESCENDING)
.asObservable())
}) { pair -> close(pair.first, pair.second) }
.unsubscribeOn(AndroidSchedulers.from(Looper.myLooper()!!))
.filter { realmSpotlightResults -> realmSpotlightResults.isLoaded && realmSpotlightResults.isValid }
.map { realmSpotlightResults -> toList(safeSubList<RealmSpotlight>(realmSpotlightResults, 0, limit)) }
}
}
private fun toList(realmSpotlightList: List<RealmSpotlight>): List<Spotlight> {
val total = realmSpotlightList.size
val spotlightList = ArrayList<Spotlight>(total)
(0..total - 1).mapTo(spotlightList) {
realmSpotlightList[it].asSpotlight()
}
return spotlightList
}
}
\ No newline at end of file
...@@ -17,8 +17,7 @@ import chat.rocket.persistence.realm.models.ddp.RealmRoom; ...@@ -17,8 +17,7 @@ import chat.rocket.persistence.realm.models.ddp.RealmRoom;
import chat.rocket.persistence.realm.models.ddp.RealmSpotlightRoom; import chat.rocket.persistence.realm.models.ddp.RealmSpotlightRoom;
import hu.akarnokd.rxjava.interop.RxJavaInterop; import hu.akarnokd.rxjava.interop.RxJavaInterop;
public class RealmSpotlightRoomRepository extends RealmRepository public class RealmSpotlightRoomRepository extends RealmRepository implements SpotlightRoomRepository {
implements SpotlightRoomRepository {
private final String hostname; private final String hostname;
...@@ -27,8 +26,7 @@ public class RealmSpotlightRoomRepository extends RealmRepository ...@@ -27,8 +26,7 @@ public class RealmSpotlightRoomRepository extends RealmRepository
} }
@Override @Override
public Flowable<List<SpotlightRoom>> getSuggestionsFor(String name, SortDirection direction, public Flowable<List<SpotlightRoom>> getSuggestionsFor(String name, SortDirection direction, int limit) {
int limit) {
return Flowable.defer(() -> Flowable.using( return Flowable.defer(() -> Flowable.using(
() -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()), () -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()),
pair -> RxJavaInterop.toV2Flowable( pair -> RxJavaInterop.toV2Flowable(
...@@ -39,8 +37,7 @@ public class RealmSpotlightRoomRepository extends RealmRepository ...@@ -39,8 +37,7 @@ public class RealmSpotlightRoomRepository extends RealmRepository
.or() .or()
.equalTo(RealmSpotlightRoom.Columns.TYPE, RealmRoom.TYPE_PRIVATE) .equalTo(RealmSpotlightRoom.Columns.TYPE, RealmRoom.TYPE_PRIVATE)
.endGroup() .endGroup()
.findAllSorted(RealmSpotlightRoom.Columns.NAME, .findAllSorted(RealmSpotlightRoom.Columns.NAME, direction.equals(SortDirection.ASC) ? Sort.ASCENDING : Sort.DESCENDING)
direction.equals(SortDirection.ASC) ? Sort.ASCENDING : Sort.DESCENDING)
.asObservable()), .asObservable()),
pair -> close(pair.first, pair.second) pair -> close(pair.first, pair.second)
) )
......
...@@ -16,8 +16,7 @@ import chat.rocket.persistence.realm.RealmStore; ...@@ -16,8 +16,7 @@ import chat.rocket.persistence.realm.RealmStore;
import chat.rocket.persistence.realm.models.ddp.RealmSpotlightUser; import chat.rocket.persistence.realm.models.ddp.RealmSpotlightUser;
import hu.akarnokd.rxjava.interop.RxJavaInterop; import hu.akarnokd.rxjava.interop.RxJavaInterop;
public class RealmSpotlightUserRepository extends RealmRepository public class RealmSpotlightUserRepository extends RealmRepository implements SpotlightUserRepository {
implements SpotlightUserRepository {
private final String hostname; private final String hostname;
...@@ -26,8 +25,7 @@ public class RealmSpotlightUserRepository extends RealmRepository ...@@ -26,8 +25,7 @@ public class RealmSpotlightUserRepository extends RealmRepository
} }
@Override @Override
public Flowable<List<SpotlightUser>> getSuggestionsFor(String name, SortDirection direction, public Flowable<List<SpotlightUser>> getSuggestionsFor(String name, SortDirection direction, int limit) {
int limit) {
return Flowable.defer(() -> Flowable.using( return Flowable.defer(() -> Flowable.using(
() -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()), () -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()),
pair -> RxJavaInterop.toV2Flowable( pair -> RxJavaInterop.toV2Flowable(
......
package chat.rocket.persistence.realm.models.ddp
import org.json.JSONArray
import org.junit.Test
import org.skyscreamer.jsonassert.JSONAssert
class RealmSpotlightTest {
@Test
fun customizeUserJsonObjectTest() {
// This is a JSONArray that contains a single simulated user data returned from the R.C server.
val userJSONArray = JSONArray("[{\"_id\":\"1234567890\",\"status\":\"offline\",\"name\":\"John Doe\",\"username\":\"John.doe\"}]")
// We have only one JSONObject, so let's customize it.
RealmSpotlight.customizeUserJSONObject(userJSONArray.getJSONObject(0))
// The desired JSON array we want.
val expectedUserJSONArray = JSONArray("[{\"_id\":\"1234567890\",\"name\":\"John.doe\",\"t\":\"d\"}]")
JSONAssert.assertEquals(expectedUserJSONArray, userJSONArray, false)
}
}
\ No newline at end of file
package chat.rocket.core.models;
import com.google.auto.value.AutoValue;
@AutoValue
public abstract class Spotlight {
public abstract String getId();
public abstract String getName();
public abstract String getType();
public static Spotlight.Builder builder() {
return new AutoValue_Spotlight.Builder();
}
@AutoValue.Builder
public abstract static class Builder {
public abstract Builder setId(String id);
public abstract Builder setName(String name);
public abstract Builder setType(String type);
public abstract Spotlight build();
}
}
\ No newline at end of file
package chat.rocket.core.repositories
import chat.rocket.core.models.Spotlight
import io.reactivex.Flowable
interface SpotlightRepository {
fun getSuggestionsFor(term: String, limit: Int): Flowable<List<Spotlight>>
}
\ No newline at end of file
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