.
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
package de.beyondsoft.ownchat.data.repository;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import de.beyondsoft.ownchat.data.repository.mapper.Mapper;
|
||||
import de.beyondsoft.ownchat.data.repository.models.GroupRealm;
|
||||
import de.beyondsoft.ownchat.data.repository.models.MessageRealm;
|
||||
import de.beyondsoft.ownchat.data.repository.models.UserRealm;
|
||||
import de.beyondsoft.ownchat.data.repository.specifications.GroupByIdSpecification;
|
||||
import de.beyondsoft.ownchat.data.repository.specifications.RealmSpecification;
|
||||
@@ -12,13 +15,16 @@ import de.beyondsoft.ownchat.model.User;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import io.realm.Realm;
|
||||
import io.realm.RealmList;
|
||||
import io.realm.RealmQuery;
|
||||
import io.realm.RealmResults;
|
||||
import io.realm.Sort;
|
||||
import rx.Completable;
|
||||
import rx.Observable;
|
||||
import rx.functions.Func1;
|
||||
|
||||
/**
|
||||
* Created by imarneanu on 3/6/17.
|
||||
@@ -70,11 +76,11 @@ public class GroupRealmRepository implements Repository<Group> {
|
||||
.map(mToRealmMapper::from)
|
||||
.doOnNext(newGroup -> {
|
||||
realmSpecification.setGroupId(newGroup.getId());
|
||||
GroupRealm groupToUpdate =
|
||||
realmSpecification.toRealmQuery(realmWrapper.realm).findFirst();
|
||||
GroupRealm groupToUpdate = realmSpecification.toRealmQuery(realmWrapper.realm).findFirst();
|
||||
if (groupToUpdate != null) {
|
||||
newGroup.setUniqueId(groupToUpdate.getUniqueId());
|
||||
}
|
||||
|
||||
realmWrapper.realm.copyToRealmOrUpdate(newGroup);
|
||||
})
|
||||
.doOnError(throwable -> {
|
||||
@@ -91,30 +97,80 @@ public class GroupRealmRepository implements Repository<Group> {
|
||||
@Override
|
||||
public Completable update(Iterable<Group> items, Specification specification) {
|
||||
final RealmWrapper realmWrapper = new RealmWrapper();
|
||||
|
||||
GroupByIdSpecification realmSpecification = (GroupByIdSpecification) specification;
|
||||
|
||||
try {
|
||||
realmWrapper.realm = mRealmProvider.provide();
|
||||
realmWrapper.realm.beginTransaction();
|
||||
|
||||
Long[] ids = new Long[((ArrayList<Group>) items).size()];
|
||||
for(int i = 0; i < ids.length; i++) {
|
||||
ids[i] = ((ArrayList<Group>) items).get(i).id;
|
||||
}
|
||||
|
||||
RealmResults<GroupRealm> allWhoAreNotComingFromTheServer = realmWrapper.realm.where(GroupRealm.class).not().in(GroupRealm.ID, ids).findAll();
|
||||
RealmResults<MessageRealm> messagesToDelete = realmWrapper.realm.where(MessageRealm.class).not().in(MessageRealm.GROUP_ID, ids).findAll();
|
||||
|
||||
long userId = ((GroupByIdSpecification) specification).getUserId();
|
||||
Log.d("REALM_TRANSACTION", String.format(Locale.getDefault(),"UserId: %d", userId));
|
||||
|
||||
|
||||
for(GroupRealm groupRealm : allWhoAreNotComingFromTheServer) {
|
||||
RealmList<UserRealm> userIdList = groupRealm.getUserIdList();
|
||||
for(UserRealm userRealm : userIdList) {
|
||||
// TODO: Are there groups with just two users that aren't normal chats?
|
||||
}
|
||||
}
|
||||
|
||||
Log.d("REALM_TRANSACTION", String.format(Locale.getDefault(), "Number of groups to delete: %d and the number of their messages: %d", allWhoAreNotComingFromTheServer.size(), messagesToDelete.size()));
|
||||
//realmWrapper.realm.where(GroupRealm.class).not().in(GroupRealm.ID, ids).findAll().deleteAllFromRealm();
|
||||
//realmWrapper.realm.where(MessageRealm.class).not().in(MessageRealm.GROUP_ID, ids).findAll().deleteAllFromRealm();
|
||||
|
||||
int j = 0;
|
||||
for(MessageRealm mr : messagesToDelete) {
|
||||
if(j == 10) {
|
||||
break;
|
||||
}
|
||||
|
||||
String[] splitFileName = mr.getFilePath().split("/");
|
||||
|
||||
Log.d("REALM_TRANSACTION", String.format(Locale.getDefault(), "%2d. %s; Has been downloaded: %s", (j+1), splitFileName[splitFileName.length - 1], mr.isFileDownloaded()));
|
||||
|
||||
j++;
|
||||
}
|
||||
|
||||
realmWrapper.realm.commitTransaction();
|
||||
realmWrapper.realm.close();
|
||||
} catch(Exception e) {
|
||||
realmWrapper.realm.cancelTransaction();
|
||||
realmWrapper.realm.close();
|
||||
Log.e("REALM_TRANSACTION", "Error: " + e.getMessage());
|
||||
}
|
||||
|
||||
return Observable.from(items)
|
||||
.doOnSubscribe(() -> {
|
||||
realmWrapper.realm = mRealmProvider.provide();
|
||||
realmWrapper.realm.beginTransaction();
|
||||
})
|
||||
|
||||
.map(mToRealmMapper::from)
|
||||
.doOnNext(newGroup -> {
|
||||
realmSpecification.setGroupId(newGroup.getId());
|
||||
GroupRealm groupToUpdate =
|
||||
realmSpecification.toRealmQuery(realmWrapper.realm).findFirst();
|
||||
GroupRealm groupToUpdate = realmSpecification.toRealmQuery(realmWrapper.realm).findFirst();
|
||||
|
||||
if (groupToUpdate != null) {
|
||||
if (newGroup.getMessage() != null && groupToUpdate.getMessage() != null &&
|
||||
newGroup.getMessage().getCreatedAt() < groupToUpdate.getMessage().getCreatedAt()) {
|
||||
if (newGroup.getMessage() != null && groupToUpdate.getMessage() != null && newGroup.getMessage().getCreatedAt() < groupToUpdate.getMessage().getCreatedAt()) {
|
||||
return;
|
||||
}
|
||||
|
||||
newGroup.setUniqueId(groupToUpdate.getUniqueId());
|
||||
if (groupToUpdate.getMessage() != null
|
||||
&& newGroup.getMessage() != null
|
||||
&& groupToUpdate.getMessage().getUniqueId().equals(newGroup.getMessage().getUniqueId())
|
||||
&& groupToUpdate.getMessage().isRead()) {
|
||||
|
||||
if (groupToUpdate.getMessage() != null && newGroup.getMessage() != null && groupToUpdate.getMessage().getUniqueId().equals(newGroup.getMessage().getUniqueId()) && groupToUpdate.getMessage().isRead()) {
|
||||
newGroup.getMessage().setRead(true);
|
||||
}
|
||||
}
|
||||
|
||||
realmWrapper.realm.copyToRealmOrUpdate(newGroup);
|
||||
})
|
||||
.doOnError(throwable -> {
|
||||
@@ -136,12 +192,10 @@ public class GroupRealmRepository implements Repository<Group> {
|
||||
@Override
|
||||
public Completable removeAll() {
|
||||
RealmWrapper realmWrapper = new RealmWrapper();
|
||||
return Observable.fromCallable(new Callable<Void>() {
|
||||
@Override
|
||||
public Void call() throws Exception {
|
||||
realmWrapper.realm.delete(GroupRealm.class);
|
||||
return null;
|
||||
}
|
||||
|
||||
return Observable.fromCallable((Callable<Void>) () -> {
|
||||
realmWrapper.realm.delete(GroupRealm.class);
|
||||
return null;
|
||||
})
|
||||
.doOnSubscribe(() -> {
|
||||
realmWrapper.realm = mRealmProvider.provide();
|
||||
@@ -183,15 +237,11 @@ public class GroupRealmRepository implements Repository<Group> {
|
||||
public Observable<List<Group>> queryForAll(Specification specification) {
|
||||
RealmWrapper realmWrapper = new RealmWrapper();
|
||||
RealmSpecification<GroupRealm> realmSpecification = (RealmSpecification) specification;
|
||||
|
||||
return Observable.fromCallable(() -> realmSpecification.toRealmQuery(realmWrapper.realm)
|
||||
.findAllSorted(GroupRealm.CREATED_AT, Sort.DESCENDING))
|
||||
.flatMapIterable(groupsRealms -> groupsRealms)
|
||||
.flatMap(new Func1<GroupRealm, Observable<Group>>() {
|
||||
@Override
|
||||
public Observable<Group> call(GroupRealm groupRealm) {
|
||||
return Observable.just(mToGroupMapper.from(groupRealm));
|
||||
}
|
||||
})
|
||||
.flatMap(groupRealm -> Observable.just(mToGroupMapper.from(groupRealm)))
|
||||
.toList()
|
||||
.doOnSubscribe(() -> {
|
||||
realmWrapper.realm = mRealmProvider.provide();
|
||||
|
||||
@@ -21,6 +21,10 @@ public class GroupByIdSpecification implements RealmSpecification<GroupRealm> {
|
||||
mGroupId = groupId;
|
||||
}
|
||||
|
||||
public long getUserId() {
|
||||
return mUserId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RealmQuery<GroupRealm> toRealmQuery(Realm realm) {
|
||||
return realm.where(GroupRealm.class)
|
||||
|
||||
@@ -52,6 +52,7 @@ import android.support.v7.widget.RecyclerView;
|
||||
import android.support.v7.widget.SimpleItemAnimator;
|
||||
import android.text.Editable;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.view.Gravity;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
@@ -522,12 +523,15 @@ abstract class BaseChatActivity extends RuntimePermissionActivity implements Cha
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
dialog.dismiss();
|
||||
e.printStackTrace();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
dialog.show();
|
||||
|
||||
@@ -330,7 +330,7 @@ abstract class BaseChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol
|
||||
viewHolder.fileImageView.setVisibility(View.VISIBLE);
|
||||
viewHolder.filenameTextView.setVisibility(View.GONE);
|
||||
viewHolder.fileImageView.setImageDrawable(null);
|
||||
Log.d("FILE_PATH", message.toString());
|
||||
|
||||
Glide.with(viewHolder.fileImageView.getContext())
|
||||
.load(TextUtils.isEmpty(message.smallerImage) ? message.filePath : message.smallerImage)
|
||||
.placeholder(R.drawable.document)
|
||||
|
||||
@@ -159,6 +159,7 @@ public class ContactsFragment extends BaseFragment implements ContactsView, Cont
|
||||
mSnackbar = Snackbar.make(mCoordinatorLayout, message, Snackbar.LENGTH_LONG);
|
||||
mSnackbar.setActionTextColor(ContextCompat.getColor(getActivity(), R.color.colorDefault));
|
||||
}
|
||||
|
||||
mSnackbar.setText(message);
|
||||
mSnackbar.show();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user