Commit b1514363 authored by Leonardo Aramaki's avatar Leonardo Aramaki

Convert HomeFragment, RoomContract and RoomPresenter to Kotlin

parent ada461e5
package chat.rocket.android.fragment.chatroom;
import chat.rocket.android.R;
public class HomeFragment extends AbstractChatRoomFragment {
public HomeFragment() {}
@Override
protected int getLayout() {
return R.layout.fragment_home;
}
@Override
protected void onSetupView() {
setToolbarTitle(getText(R.string.home_fragment_title));
}
}
\ No newline at end of file
package chat.rocket.android.fragment.chatroom
import chat.rocket.android.R
class HomeFragment : AbstractChatRoomFragment() {
override fun getLayout(): Int {
return R.layout.fragment_home
}
override fun onSetupView() {
setToolbarTitle(getText(R.string.home_fragment_title))
}
}
\ No newline at end of file
package chat.rocket.android.fragment.chatroom;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import java.util.List;
import chat.rocket.android.shared.BaseContract;
import chat.rocket.android.widget.AbsoluteUrl;
import chat.rocket.core.models.Message;
import chat.rocket.core.models.Room;
import chat.rocket.core.models.User;
public interface RoomContract {
interface View extends BaseContract.View {
void setupWith(RocketChatAbsoluteUrl rocketChatAbsoluteUrl);
void render(Room room);
void showUserStatus(User user);
void updateHistoryState(boolean hasNext, boolean isLoaded);
void onMessageSendSuccessfully();
void disableMessageInput();
void enableMessageInput();
void showUnreadCount(int count);
void showMessages(List<Message> messages);
void showMessageSendFailure(Message message);
void showMessageDeleteFailure(Message message);
void autoloadImages();
void manualLoadImages();
void onReply(AbsoluteUrl absoluteUrl, String markdown, Message message);
void onCopy(String message);
void showMessageActions(Message message);
}
interface Presenter extends BaseContract.Presenter<View> {
void loadMessages();
void loadMoreMessages();
void onMessageSelected(@Nullable Message message);
void onMessageTap(@Nullable Message message);
void sendMessage(String messageText);
void resendMessage(@NonNull Message message);
void updateMessage(@NonNull Message message, String content);
void deleteMessage(@NonNull Message message);
void onUnreadCount();
void onMarkAsRead();
void refreshRoom();
void replyMessage(@NonNull Message message, boolean justQuote);
void acceptMessageDeleteFailure(Message message);
void loadMissedMessages();
}
}
package chat.rocket.android.fragment.chatroom
import chat.rocket.android.shared.BaseContract
import chat.rocket.android.widget.AbsoluteUrl
import chat.rocket.core.models.Message
import chat.rocket.core.models.Room
import chat.rocket.core.models.User
interface RoomContract {
interface View : BaseContract.View {
fun setupWith(rocketChatAbsoluteUrl: RocketChatAbsoluteUrl)
fun render(room: Room)
fun showUserStatus(user: User)
fun updateHistoryState(hasNext: Boolean, isLoaded: Boolean)
fun onMessageSendSuccessfully()
fun disableMessageInput()
fun enableMessageInput()
fun showUnreadCount(count: Int)
fun showMessages(messages: List<Message>)
fun showMessageSendFailure(message: Message)
fun showMessageDeleteFailure(message: Message)
fun autoloadImages()
fun manualLoadImages()
fun onReply(absoluteUrl: AbsoluteUrl, markdown: String, message: Message)
fun onCopy(message: String)
fun showMessageActions(message: Message)
}
interface Presenter : BaseContract.Presenter<View> {
fun loadMessages()
fun loadMoreMessages()
fun onMessageSelected(message: Message?)
fun onMessageTap(message: Message?)
fun sendMessage(messageText: String)
fun resendMessage(message: Message)
fun updateMessage(message: Message, content: String)
fun deleteMessage(message: Message)
fun onUnreadCount()
fun onMarkAsRead()
fun refreshRoom()
fun replyMessage(message: Message, justQuote: Boolean)
fun acceptMessageDeleteFailure(message: Message)
fun loadMissedMessages()
}
}
package chat.rocket.android.fragment.chatroom; package chat.rocket.android.fragment.chatroom
import android.support.annotation.NonNull; import android.support.v4.util.Pair
import android.support.annotation.Nullable;
import android.support.v4.util.Pair; import com.hadisatrio.optional.Optional
import com.hadisatrio.optional.Optional; import org.json.JSONException
import org.json.JSONObject
import org.json.JSONException;
import org.json.JSONObject; import chat.rocket.android.BackgroundLooper
import chat.rocket.android.RocketChatApplication
import chat.rocket.android.BackgroundLooper; import chat.rocket.android.RocketChatCache
import chat.rocket.android.RocketChatApplication; import chat.rocket.android.api.MethodCallHelper
import chat.rocket.android.RocketChatCache; import chat.rocket.android.helper.AbsoluteUrlHelper
import chat.rocket.android.api.MethodCallHelper; import chat.rocket.android.helper.LogIfError
import chat.rocket.android.helper.AbsoluteUrlHelper; import chat.rocket.android.helper.Logger
import chat.rocket.android.helper.LogIfError; import chat.rocket.android.log.RCLog
import chat.rocket.android.helper.Logger; import chat.rocket.android.service.ConnectivityManagerApi
import chat.rocket.android.log.RCLog; import chat.rocket.android.shared.BasePresenter
import chat.rocket.android.service.ConnectivityManagerApi; import chat.rocket.core.SyncState
import chat.rocket.android.shared.BasePresenter; import chat.rocket.core.interactors.MessageInteractor
import chat.rocket.core.SyncState; import chat.rocket.core.models.Message
import chat.rocket.core.interactors.MessageInteractor; import chat.rocket.core.models.Room
import chat.rocket.core.models.Message; import chat.rocket.core.models.Settings
import chat.rocket.core.models.Room; import chat.rocket.core.models.User
import chat.rocket.core.models.Settings; import chat.rocket.core.repositories.RoomRepository
import chat.rocket.core.models.User; import chat.rocket.core.repositories.UserRepository
import chat.rocket.core.repositories.RoomRepository; import io.reactivex.Flowable
import chat.rocket.core.repositories.UserRepository; import io.reactivex.Single
import io.reactivex.Flowable; import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.Single; import io.reactivex.disposables.Disposable
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable; class RoomPresenter/* package */ internal constructor(private val roomId: String,
private val userRepository: UserRepository,
public class RoomPresenter extends BasePresenter<RoomContract.View> private val messageInteractor: MessageInteractor,
implements RoomContract.Presenter { private val roomRepository: RoomRepository,
private val absoluteUrlHelper: AbsoluteUrlHelper,
private final String roomId; private val methodCallHelper: MethodCallHelper,
private final MessageInteractor messageInteractor; private val connectivityManagerApi: ConnectivityManagerApi) : BasePresenter<RoomContract.View>(), RoomContract.Presenter {
private final UserRepository userRepository; private var currentRoom: Room? = null
private final RoomRepository roomRepository;
private final AbsoluteUrlHelper absoluteUrlHelper; private val roomUserPair: Single<Pair<Room, User>>
private final MethodCallHelper methodCallHelper; get() = Single.zip(
private final ConnectivityManagerApi connectivityManagerApi; singleRoom,
private Room currentRoom; currentUser,
BiFunction<Room, User, Pair<Room, User>> { first, second -> Pair(first, second) }
/* package */RoomPresenter(String roomId, )
UserRepository userRepository,
MessageInteractor messageInteractor, private val singleRoom: Single<Room>
RoomRepository roomRepository, get() = roomRepository.getById(roomId)
AbsoluteUrlHelper absoluteUrlHelper, .filter(Predicate<Optional<Room>> { it.isPresent() })
MethodCallHelper methodCallHelper, .map<Room>(Function<Optional<Room>, Room> { it.get() })
ConnectivityManagerApi connectivityManagerApi) { .firstElement()
this.roomId = roomId; .toSingle()
this.userRepository = userRepository;
this.messageInteractor = messageInteractor; private val currentUser: Single<User>
this.roomRepository = roomRepository; get() = userRepository.getCurrent()
this.absoluteUrlHelper = absoluteUrlHelper; .filter(Predicate<Optional<User>> { it.isPresent() })
this.methodCallHelper = methodCallHelper; .map<User>(Function<Optional<User>, User> { it.get() })
this.connectivityManagerApi = connectivityManagerApi; .firstElement()
} .toSingle()
@Override override fun bindView(view: RoomContract.View) {
public void bindView(@NonNull RoomContract.View view) { super.bindView(view)
super.bindView(view); refreshRoom()
refreshRoom(); }
}
override fun refreshRoom() {
@Override getRoomRoles()
public void refreshRoom() { getRoomInfo()
getRoomRoles(); getRoomHistoryStateInfo()
getRoomInfo(); getMessages()
getRoomHistoryStateInfo(); getUserPreferences()
getMessages(); }
getUserPreferences();
} override fun loadMessages() {
val subscription = singleRoom
@Override .flatMap<Boolean>(Function<Room, SingleSource<out Boolean>> { messageInteractor.loadMessages(it) })
public void loadMessages() {
final Disposable subscription = getSingleRoom()
.flatMap(messageInteractor::loadMessages)
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())) .subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe( .subscribe(
success -> { { success ->
if (!success) { if (!success) {
connectivityManagerApi.keepAliveServer(); connectivityManagerApi.keepAliveServer()
} }
}, },
Logger.INSTANCE::report Consumer<Throwable> { Logger.report(it) }
); )
addSubscription(subscription); addSubscription(subscription)
} }
@Override override fun loadMoreMessages() {
public void loadMoreMessages() { val subscription = singleRoom
final Disposable subscription = getSingleRoom() .flatMap<Boolean>(Function<Room, SingleSource<out Boolean>> { messageInteractor.loadMoreMessages(it) })
.flatMap(messageInteractor::loadMoreMessages)
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())) .subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe( .subscribe(
success -> { { success ->
if (!success) { if (!success) {
connectivityManagerApi.keepAliveServer(); connectivityManagerApi.keepAliveServer()
} }
}, },
Logger.INSTANCE::report Consumer<Throwable> { Logger.report(it) }
); )
addSubscription(subscription); addSubscription(subscription)
} }
@Override override fun onMessageSelected(message: Message?) {
public void onMessageSelected(@Nullable Message message) {
if (message == null) { if (message == null) {
return; return
} }
if (message.getSyncState() == SyncState.DELETE_FAILED) { if (message.syncState == SyncState.DELETE_FAILED) {
view.showMessageDeleteFailure(message); view.showMessageDeleteFailure(message)
} else if (message.getSyncState() == SyncState.FAILED) { } else if (message.syncState == SyncState.FAILED) {
view.showMessageSendFailure(message); view.showMessageSendFailure(message)
} else if (message.getType() == null && message.getSyncState() == SyncState.SYNCED) { } else if (message.type == null && message.syncState == SyncState.SYNCED) {
// If message is not a system message show applicable actions. // If message is not a system message show applicable actions.
view.showMessageActions(message); view.showMessageActions(message)
} }
} }
@Override override fun onMessageTap(message: Message?) {
public void onMessageTap(@Nullable Message message) {
if (message == null) { if (message == null) {
return; return
} }
if (message.getSyncState() == SyncState.FAILED) { if (message.syncState == SyncState.FAILED) {
view.showMessageSendFailure(message); view.showMessageSendFailure(message)
} }
} }
@Override override fun replyMessage(message: Message, justQuote: Boolean) {
public void replyMessage(@NonNull Message message, boolean justQuote) { val subscription = this.absoluteUrlHelper.rocketChatAbsoluteUrl
final Disposable subscription = this.absoluteUrlHelper.getRocketChatAbsoluteUrl()
.cache() .cache()
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())) .subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe( .subscribe(
serverUrl -> { { serverUrl ->
if (serverUrl.isPresent()) { if (serverUrl.isPresent) {
RocketChatAbsoluteUrl absoluteUrl = serverUrl.get(); val absoluteUrl = serverUrl.get()
String baseUrl = absoluteUrl.getBaseUrl(); val baseUrl = absoluteUrl.baseUrl
view.onReply(absoluteUrl, buildReplyOrQuoteMarkdown(baseUrl, message, justQuote), message); view.onReply(absoluteUrl, buildReplyOrQuoteMarkdown(baseUrl, message, justQuote), message)
} }
}, },
Logger.INSTANCE::report Consumer<Throwable> { Logger.report(it) }
); )
addSubscription(subscription); addSubscription(subscription)
} }
public void acceptMessageDeleteFailure(Message message) { override fun acceptMessageDeleteFailure(message: Message) {
final Disposable subscription = messageInteractor.acceptDeleteFailure(message) val subscription = messageInteractor.acceptDeleteFailure(message)
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())) .subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(); .subscribe()
addSubscription(subscription); addSubscription(subscription)
} }
@Override override fun loadMissedMessages() {
public void loadMissedMessages() { val appContext = RocketChatApplication.getInstance()
RocketChatApplication appContext = RocketChatApplication.getInstance(); val openedRooms = RocketChatCache(appContext).openedRooms
JSONObject openedRooms = new RocketChatCache(appContext).getOpenedRooms();
if (openedRooms.has(roomId)) { if (openedRooms.has(roomId)) {
try { try {
JSONObject room = openedRooms.getJSONObject(roomId); val room = openedRooms.getJSONObject(roomId)
String rid = room.optString("rid"); val rid = room.optString("rid")
long ls = room.optLong("ls"); val ls = room.optLong("ls")
methodCallHelper.loadMissedMessages(rid, ls) methodCallHelper.loadMissedMessages(rid, ls)
.continueWith(new LogIfError()); .continueWith(LogIfError())
} catch (JSONException e) { } catch (e: JSONException) {
RCLog.e(e); RCLog.e(e)
} }
} }
} }
private String buildReplyOrQuoteMarkdown(String baseUrl, Message message, boolean justQuote) { private fun buildReplyOrQuoteMarkdown(baseUrl: String, message: Message, justQuote: Boolean): String {
if (currentRoom == null || message.getUser() == null) { if (currentRoom == null || message.user == null) {
return ""; return ""
} }
if (currentRoom.isDirectMessage()) { return if (currentRoom!!.isDirectMessage) {
return String.format("[ ](%s/direct/%s?msg=%s) ", baseUrl, String.format("[ ](%s/direct/%s?msg=%s) ", baseUrl,
message.getUser().getUsername(), message.user!!.username,
message.getId()); message.id)
} else { } else {
return String.format("[ ](%s/channel/%s?msg=%s) %s", baseUrl, String.format("[ ](%s/channel/%s?msg=%s) %s", baseUrl,
currentRoom.getName(), currentRoom!!.name,
message.getId(), message.id,
justQuote ? "" : "@" + message.getUser().getUsername() + " "); if (justQuote) "" else "@" + message.user!!.username + " ")
} }
} }
@Override override fun sendMessage(messageText: String) {
public void sendMessage(String messageText) { view.disableMessageInput()
view.disableMessageInput(); val subscription = roomUserPair
final Disposable subscription = getRoomUserPair() .flatMap { pair -> messageInteractor.send(pair.first!!, pair.second!!, messageText) }
.flatMap(pair -> messageInteractor.send(pair.first, pair.second, messageText))
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())) .subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe( .subscribe(
success -> { { success ->
if (success) { if (success) {
view.onMessageSendSuccessfully(); view.onMessageSendSuccessfully()
} }
view.enableMessageInput(); view.enableMessageInput()
}, }
throwable -> { ) { throwable ->
view.enableMessageInput(); view.enableMessageInput()
Logger.INSTANCE.report(throwable); Logger.report(throwable)
} }
);
addSubscription(subscription); addSubscription(subscription)
} }
@Override override fun resendMessage(message: Message) {
public void resendMessage(@NonNull Message message) { val subscription = currentUser
final Disposable subscription = getCurrentUser() .flatMap { user -> messageInteractor.resend(message, user) }
.flatMap(user -> messageInteractor.resend(message, user))
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())) .subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(); .subscribe()
addSubscription(subscription); addSubscription(subscription)
} }
@Override override fun updateMessage(message: Message, content: String) {
public void updateMessage(@NonNull Message message, String content) { view.disableMessageInput()
view.disableMessageInput(); val subscription = currentUser
final Disposable subscription = getCurrentUser() .flatMap { user -> messageInteractor.update(message, user, content) }
.flatMap(user -> messageInteractor.update(message, user, content))
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())) .subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe( .subscribe(
success -> { { success ->
if (success) { if (success) {
view.onMessageSendSuccessfully(); view.onMessageSendSuccessfully()
} }
view.enableMessageInput(); view.enableMessageInput()
}, }
throwable -> { ) { throwable ->
view.enableMessageInput(); view.enableMessageInput()
Logger.INSTANCE.report(throwable); Logger.report(throwable)
} }
);
addSubscription(subscription); addSubscription(subscription)
} }
@Override override fun deleteMessage(message: Message) {
public void deleteMessage(@NonNull Message message) { val subscription = messageInteractor.delete(message)
final Disposable subscription = messageInteractor.delete(message)
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())) .subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(); .subscribe()
addSubscription(subscription); addSubscription(subscription)
} }
@Override override fun onUnreadCount() {
public void onUnreadCount() { val subscription = roomUserPair
final Disposable subscription = getRoomUserPair() .flatMap { roomUserPair ->
.flatMap(roomUserPair -> messageInteractor messageInteractor
.unreadCountFor(roomUserPair.first, roomUserPair.second)) .unreadCountFor(roomUserPair.first!!, roomUserPair.second!!)
}
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())) .subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe( .subscribe(
count -> view.showUnreadCount(count), { count -> view.showUnreadCount(count) },
Logger.INSTANCE::report Consumer<Throwable> { Logger.report(it) }
); )
addSubscription(subscription); addSubscription(subscription)
} }
@Override override fun onMarkAsRead() {
public void onMarkAsRead() { val subscription = roomRepository.getById(roomId)
final Disposable subscription = roomRepository.getById(roomId) .filter(Predicate<Optional<Room>> { it.isPresent() })
.filter(Optional::isPresent) .map<Room>(Function<Optional<Room>, Room> { it.get() })
.map(Optional::get)
.firstElement() .firstElement()
.filter(Room::isAlert) .filter(Predicate<Room> { it.isAlert() })
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())) .subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe( .subscribe(
room -> methodCallHelper.readMessages(room.getRoomId()) { room ->
.continueWith(new LogIfError()), methodCallHelper.readMessages(room.roomId)
Logger.INSTANCE::report .continueWith(LogIfError())
); },
Consumer<Throwable> { Logger.report(it) }
)
addSubscription(subscription); addSubscription(subscription)
} }
private void getRoomRoles() { private fun getRoomRoles() {
methodCallHelper.getRoomRoles(roomId); methodCallHelper.getRoomRoles(roomId)
} }
private void getRoomInfo() { private fun getRoomInfo() {
final Disposable subscription = roomRepository.getById(roomId) val subscription = roomRepository.getById(roomId)
.distinctUntilChanged() .distinctUntilChanged()
.filter(Optional::isPresent) .filter(Predicate<Optional<Room>> { it.isPresent() })
.map(Optional::get) .map<Room>(Function<Optional<Room>, Room> { it.get() })
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())) .subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(this::processRoom, Logger.INSTANCE::report); .subscribe(Consumer<Room> { this.processRoom(it) }, Consumer<Throwable> { Logger.report(it) })
addSubscription(subscription); addSubscription(subscription)
} }
private void processRoom(Room room) { private fun processRoom(room: Room) {
this.currentRoom = room; this.currentRoom = room
view.render(room); view.render(room)
if (room.isDirectMessage()) { if (room.isDirectMessage) {
getUserByUsername(room.getName()); getUserByUsername(room.name)
} }
} }
private void getUserByUsername(String username) { private fun getUserByUsername(username: String) {
final Disposable disposable = userRepository.getByUsername(username) val disposable = userRepository.getByUsername(username)
.distinctUntilChanged() .distinctUntilChanged()
.filter(Optional::isPresent) .filter(Predicate<Optional<User>> { it.isPresent() })
.map(Optional::get) .map<User>(Function<Optional<User>, User> { it.get() })
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())) .subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(view::showUserStatus, Logger.INSTANCE::report); .subscribe(Consumer<User> { view.showUserStatus(it) }, Consumer<Throwable> { Logger.report(it) })
addSubscription(disposable); addSubscription(disposable)
} }
private void getRoomHistoryStateInfo() { private fun getRoomHistoryStateInfo() {
final Disposable subscription = roomRepository.getHistoryStateByRoomId(roomId) val subscription = roomRepository.getHistoryStateByRoomId(roomId)
.distinctUntilChanged() .distinctUntilChanged()
.filter(Optional::isPresent) .filter(Predicate<Optional<RoomHistoryState>> { it.isPresent() })
.map(Optional::get) .map<RoomHistoryState>(Function<Optional<RoomHistoryState>, RoomHistoryState> { it.get() })
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())) .subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe( .subscribe(
roomHistoryState -> { { roomHistoryState ->
int syncState = roomHistoryState.getSyncState(); val syncState = roomHistoryState.getSyncState()
view.updateHistoryState( view.updateHistoryState(
!roomHistoryState.isComplete(), !roomHistoryState.isComplete(),
syncState == SyncState.SYNCED || syncState == SyncState.FAILED syncState == SyncState.SYNCED || syncState == SyncState.FAILED
); )
}, },
Logger.INSTANCE::report Consumer<Throwable> { Logger.report(it) }
); )
addSubscription(subscription); addSubscription(subscription)
} }
private void getMessages() { private fun getMessages() {
final Disposable subscription = Flowable.zip(roomRepository.getById(roomId), val subscription = Flowable.zip<Optional<Room>, Optional<RocketChatAbsoluteUrl>, Pair<Optional<Room>, Optional<RocketChatAbsoluteUrl>>>(roomRepository.getById(roomId),
absoluteUrlHelper.getRocketChatAbsoluteUrl().toFlowable().cache(), Pair::new) absoluteUrlHelper.rocketChatAbsoluteUrl.toFlowable().cache(), BiFunction<Optional<Room>, Optional<RocketChatAbsoluteUrl>, Pair<Optional<Room>, Optional<RocketChatAbsoluteUrl>>> { first, second -> Pair(first, second) })
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())) .subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.map(pair -> { .map<Optional<Room>> { pair ->
view.setupWith(pair.second.orNull()); view.setupWith(pair.second!!.orNull())
return pair.first; pair.first
}) }
.filter(Optional::isPresent) .filter(Predicate<Optional<Room>> { it.isPresent() })
.map(Optional::get) .map<Room>(Function<Optional<Room>, Room> { it.get() })
.map(room -> { .map { room ->
new RocketChatCache(RocketChatApplication.getInstance()) RocketChatCache(RocketChatApplication.getInstance())
.addOpenedRoom(room.getRoomId(), room.getLastSeen()); .addOpenedRoom(room.roomId, room.lastSeen)
return room; room
}) }
.flatMap(messageInteractor::getAllFrom) .flatMap<List<Message>>(Function<Room, Publisher<out List<Message>>> { messageInteractor.getAllFrom(it) })
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())) .subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe( .subscribe(
view::showMessages, Consumer<List<Message>> { view.showMessages(it) },
Logger.INSTANCE::report Consumer<Throwable> { Logger.report(it) }
); )
addSubscription(subscription); addSubscription(subscription)
} }
private void getUserPreferences() { private fun getUserPreferences() {
final Disposable subscription = userRepository.getCurrent() val subscription = userRepository.getCurrent()
.filter(Optional::isPresent) .filter(Predicate<Optional<User>> { it.isPresent() })
.map(Optional::get) .map<User>(Function<Optional<User>, User> { it.get() })
.filter(user -> user.getSettings() != null) .filter { user -> user.settings != null }
.map(User::getSettings) .map<Settings>(Function<User, Settings> { it.getSettings() })
.filter(settings -> settings.getPreferences() != null) .filter { settings -> settings.preferences != null }
.map(Settings::getPreferences) .map<Preferences>(Function<Settings, Preferences> { it.getPreferences() })
.distinctUntilChanged() .distinctUntilChanged()
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())) .subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe( .subscribe(
preferences -> { { preferences ->
if (preferences.isAutoImageLoad()) { if (preferences.isAutoImageLoad()) {
view.autoloadImages(); view.autoloadImages()
} else { } else {
view.manualLoadImages(); view.manualLoadImages()
} }
}, },
Logger.INSTANCE::report Consumer<Throwable> { Logger.report(it) }
); )
addSubscription(subscription); addSubscription(subscription)
} }
private void getAbsoluteUrl() { private fun getAbsoluteUrl() {
final Disposable subscription = absoluteUrlHelper.getRocketChatAbsoluteUrl() val subscription = absoluteUrlHelper.rocketChatAbsoluteUrl
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())) .subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe( .subscribe(
it -> view.setupWith(it.orNull()), { it -> view.setupWith(it.orNull()) },
Logger.INSTANCE::report Consumer<Throwable> { Logger.report(it) }
); )
addSubscription(subscription); addSubscription(subscription)
}
private Single<Pair<Room, User>> getRoomUserPair() {
return Single.zip(
getSingleRoom(),
getCurrentUser(),
Pair::new
);
}
private Single<Room> getSingleRoom() {
return roomRepository.getById(roomId)
.filter(Optional::isPresent)
.map(Optional::get)
.firstElement()
.toSingle();
}
private Single<User> getCurrentUser() {
return userRepository.getCurrent()
.filter(Optional::isPresent)
.map(Optional::get)
.firstElement()
.toSingle();
} }
} }
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