Commit f1dd4424 authored by Tiago Cunha's avatar Tiago Cunha

Fix crashes

parent 9120eea0
......@@ -25,9 +25,8 @@ abstract class AbstractFragmentActivity extends RxAppCompatActivity {
Icepick.saveInstanceState(this, outState);
}
protected abstract
@IdRes
int getLayoutContainerForFragment();
protected abstract int getLayoutContainerForFragment();
@Override
public final void onBackPressed() {
......
......@@ -10,6 +10,7 @@ import android.view.View;
import chat.rocket.android.LaunchUtil;
import chat.rocket.android.R;
import chat.rocket.android.RocketChatCache;
import chat.rocket.android.api.MethodCallHelper;
import chat.rocket.android.fragment.chatroom.HomeFragment;
import chat.rocket.android.fragment.chatroom.RoomFragment;
......@@ -136,7 +137,8 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract
createRoomInteractor,
sessionInteractor,
new MethodCallHelper(this, hostname),
ConnectivityManager.getInstance(getApplicationContext())
ConnectivityManager.getInstance(getApplicationContext()),
new RocketChatCache(this)
);
updateSidebarMainFragment();
......
......@@ -8,6 +8,7 @@ import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import chat.rocket.android.BackgroundLooper;
import chat.rocket.android.RocketChatCache;
import chat.rocket.android.api.MethodCallHelper;
import chat.rocket.android.helper.LogIfError;
import chat.rocket.android.service.ConnectivityManagerApi;
......@@ -26,30 +27,33 @@ public class MainPresenter extends BasePresenter<MainContract.View>
private final SessionInteractor sessionInteractor;
private final MethodCallHelper methodCallHelper;
private final ConnectivityManagerApi connectivityManagerApi;
private final RocketChatCache rocketChatCache;
public MainPresenter(RoomInteractor roomInteractor,
CanCreateRoomInteractor canCreateRoomInteractor,
SessionInteractor sessionInteractor,
MethodCallHelper methodCallHelper,
ConnectivityManagerApi connectivityManagerApi) {
ConnectivityManagerApi connectivityManagerApi,
RocketChatCache rocketChatCache) {
this.roomInteractor = roomInteractor;
this.canCreateRoomInteractor = canCreateRoomInteractor;
this.sessionInteractor = sessionInteractor;
this.methodCallHelper = methodCallHelper;
this.connectivityManagerApi = connectivityManagerApi;
this.rocketChatCache = rocketChatCache;
}
@Override
public void bindView(@NonNull MainContract.View view) {
super.bindView(view);
view.showHome();
if (shouldLaunchAddServerActivity()) {
view.showAddServerScreen();
return;
}
openRoom();
subscribeToUnreadCount();
subscribeToSession();
setUserOnline();
......@@ -86,6 +90,18 @@ public class MainPresenter extends BasePresenter<MainContract.View>
addSubscription(subscription);
}
private void openRoom() {
String hostname = rocketChatCache.getSelectedServerHostname();
String roomId = rocketChatCache.getSelectedRoomId();
if (roomId == null || roomId.length() == 0) {
view.showHome();
return;
}
onOpenRoom(hostname, roomId);
}
private void subscribeToUnreadCount() {
final Disposable subscription = Flowable.combineLatest(
roomInteractor.getTotalUnreadRoomsCount(),
......
......@@ -179,6 +179,8 @@ public class RoomPresenter extends BasePresenter<RoomContract.View>
private void getRoomHistoryStateInfo() {
final Disposable subscription = roomRepository.getHistoryStateByRoomId(roomId)
.distinctUntilChanged()
.filter(Optional::isPresent)
.map(Optional::get)
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
......
......@@ -22,6 +22,8 @@ public interface SidebarMainContract {
interface Presenter extends BaseContract.Presenter<View> {
void onRoomSelected(Room room);
void onUserOnline();
void onUserAway();
......
......@@ -19,7 +19,6 @@ import chat.rocket.android.R;
import chat.rocket.android.RocketChatCache;
import chat.rocket.android.api.MethodCallHelper;
import chat.rocket.android.fragment.AbstractFragment;
import chat.rocket.android.fragment.sidebar.dialog.AbstractAddRoomDialogFragment;
import chat.rocket.android.fragment.sidebar.dialog.AddChannelDialogFragment;
import chat.rocket.android.fragment.sidebar.dialog.AddDirectMessageDialogFragment;
import chat.rocket.android.helper.TextUtils;
......@@ -47,8 +46,6 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain
private String hostname;
private RocketChatCache rocketChatCache;
public SidebarMainFragment() {
}
......@@ -72,12 +69,11 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain
Bundle args = getArguments();
hostname = args == null ? null : args.getString(HOSTNAME);
rocketChatCache = new RocketChatCache(getContext());
presenter = new SidebarMainPresenter(
hostname,
new RoomInteractor(new RealmRoomRepository(hostname)),
new RealmUserRepository(hostname),
new RocketChatCache(getContext()),
TextUtils.isEmpty(hostname) ? null : new MethodCallHelper(getContext(), hostname)
);
}
......@@ -107,7 +103,7 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain
setupVersionInfo();
adapter = new RoomListAdapter();
adapter.setOnItemClickListener(room -> rocketChatCache.setSelectedRoomId(room.getRoomId()));
adapter.setOnItemClickListener(room -> presenter.onRoomSelected(room));
RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.room_list_container);
recyclerView.setLayoutManager(
......@@ -119,9 +115,10 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain
final CompoundButton toggleUserAction =
((CompoundButton) rootView.findViewById(R.id.toggle_user_action));
toggleUserAction.setFocusableInTouchMode(false);
rootView.findViewById(R.id.user_info_container).setOnClickListener(view -> {
toggleUserAction.toggle();
});
rootView.findViewById(R.id.user_info_container)
.setOnClickListener(view -> toggleUserAction.toggle());
RxJavaInterop.toV2Flowable(RxCompoundButton.checkedChanges(toggleUserAction))
.compose(bindToLifecycle())
.subscribe(aBoolean -> {
......@@ -201,7 +198,7 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain
}
private void showAddRoomDialog(DialogFragment dialog) {
dialog.show(getFragmentManager(), AbstractAddRoomDialogFragment.class.getSimpleName());
dialog.show(getFragmentManager(), "AbstractAddRoomDialogFragment");
}
@Override
......
......@@ -6,11 +6,13 @@ import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import chat.rocket.android.BackgroundLooper;
import chat.rocket.android.RocketChatCache;
import chat.rocket.android.api.MethodCallHelper;
import chat.rocket.android.helper.LogIfError;
import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.shared.BasePresenter;
import chat.rocket.core.interactors.RoomInteractor;
import chat.rocket.core.models.Room;
import chat.rocket.core.models.User;
import chat.rocket.core.repositories.UserRepository;
......@@ -20,13 +22,16 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View
private final String hostname;
private final RoomInteractor roomInteractor;
private final UserRepository userRepository;
private final RocketChatCache rocketChatCache;
private final MethodCallHelper methodCallHelper;
public SidebarMainPresenter(String hostname, RoomInteractor roomInteractor,
UserRepository userRepository, MethodCallHelper methodCallHelper) {
UserRepository userRepository, RocketChatCache rocketChatCache,
MethodCallHelper methodCallHelper) {
this.hostname = hostname;
this.roomInteractor = roomInteractor;
this.userRepository = userRepository;
this.rocketChatCache = rocketChatCache;
this.methodCallHelper = methodCallHelper;
}
......@@ -45,6 +50,11 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View
subscribeToUser();
}
@Override
public void onRoomSelected(Room room) {
rocketChatCache.setSelectedRoomId(room.getRoomId());
}
@Override
public void onUserOnline() {
updateCurrentUserStatus(User.STATUS_ONLINE);
......
......@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.4-all.zip
......@@ -2,6 +2,7 @@ package chat.rocket.persistence.realm.repositories;
import android.os.Looper;
import android.support.v4.util.Pair;
import com.fernandocejas.arrow.optional.Optional;
import io.reactivex.Flowable;
import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
......@@ -60,20 +61,36 @@ public class RealmRoomRepository extends RealmRepository implements RoomReposito
}
@Override
public Flowable<RoomHistoryState> getHistoryStateByRoomId(String roomId) {
public Flowable<Optional<RoomHistoryState>> getHistoryStateByRoomId(String roomId) {
return Flowable.defer(() -> Flowable.using(
() -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()),
pair -> RxJavaInterop.toV2Flowable(
pair.first.where(LoadMessageProcedure.class)
.equalTo(LoadMessageProcedure.ID, roomId)
.findFirst()
.<LoadMessageProcedure>asObservable()
.filter(loadMessageProcedure -> loadMessageProcedure != null
&& loadMessageProcedure.isLoaded() && loadMessageProcedure.isValid())),
pair -> {
LoadMessageProcedure messageProcedure = pair.first.where(LoadMessageProcedure.class)
.equalTo(LoadMessageProcedure.ID, roomId)
.findFirst();
if (messageProcedure == null) {
return Flowable.just(Optional.<LoadMessageProcedure>absent());
}
return RxJavaInterop.toV2Flowable(
messageProcedure
.<LoadMessageProcedure>asObservable()
.filter(loadMessageProcedure -> loadMessageProcedure.isLoaded()
&& loadMessageProcedure.isValid())
.map(Optional::of));
},
pair -> close(pair.first, pair.second)
)
.unsubscribeOn(AndroidSchedulers.from(Looper.myLooper()))
.map(LoadMessageProcedure::asRoomHistoryState));
.map(optional -> {
if (optional.isPresent()) {
return Optional.of(optional.get().asRoomHistoryState());
}
return Optional.absent();
}));
}
@Override
......
package chat.rocket.core.interactors;
import com.fernandocejas.arrow.optional.Optional;
import io.reactivex.Flowable;
import io.reactivex.Single;
......@@ -38,6 +39,8 @@ public class MessageInteractor {
public Single<Boolean> loadMoreMessages(Room room) {
return roomRepository.getHistoryStateByRoomId(room.getRoomId())
.filter(Optional::isPresent)
.map(Optional::get)
.filter(roomHistoryState -> {
int syncState = roomHistoryState.getSyncState();
return !roomHistoryState.isComplete()
......
......@@ -27,6 +27,7 @@ public abstract class Preferences {
public abstract boolean isAutoImageLoad();
@Nullable
public abstract String getEmailNotificationMode();
public abstract boolean isUnreadAlert();
......
......@@ -15,6 +15,7 @@ public abstract class User {
public abstract String getId();
@Nullable
public abstract String getUsername();
@Nullable
......
package chat.rocket.core.repositories;
import com.fernandocejas.arrow.optional.Optional;
import io.reactivex.Flowable;
import io.reactivex.Single;
......@@ -13,7 +14,7 @@ public interface RoomRepository {
Flowable<Room> getById(String roomId);
Flowable<RoomHistoryState> getHistoryStateByRoomId(String roomId);
Flowable<Optional<RoomHistoryState>> getHistoryStateByRoomId(String roomId);
Single<Boolean> setHistoryState(RoomHistoryState roomHistoryState);
}
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