Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/markdown-navigator-enh.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions app/src/main/java/org/piwigo/data/model/ImageVariant.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import androidx.room.Relation;

import java.io.Serializable;
import java.util.Objects;

import static androidx.room.ForeignKey.CASCADE;

Expand Down Expand Up @@ -61,5 +62,24 @@ public ImageVariant(int imageId, int width, int height, String storageLocation,
public String storageLocation;
public String lastModified;
public String url;

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ImageVariant that = (ImageVariant) o;
return id == that.id &&
imageId == that.imageId &&
height == that.height &&
width == that.width &&
Objects.equals(storageLocation, that.storageLocation) &&
Objects.equals(lastModified, that.lastModified) &&
Objects.equals(url, that.url);
}

@Override
public int hashCode() {
return Objects.hash(id, imageId, height, width, storageLocation, lastModified, url);
}
}

16 changes: 16 additions & 0 deletions app/src/main/java/org/piwigo/data/model/PositionedItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.piwigo.data.model;

import java.io.Serializable;
import java.util.Objects;

/**
* represents an item (photo, album, video, ...) in the gallery, at a defined position in the current result
Expand All @@ -45,4 +46,19 @@ public T getItem() {
public boolean isUpdateNeeded(){
return updateNeeded;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PositionedItem<?> that = (PositionedItem<?>) o;
return position == that.position &&
updateNeeded == that.updateNeeded &&
Objects.equals(item, that.item);
}

@Override
public int hashCode() {
return Objects.hash(position, item, updateNeeded);
}
}
15 changes: 15 additions & 0 deletions app/src/main/java/org/piwigo/data/model/VariantWithImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import androidx.room.Relation;

import java.io.Serializable;
import java.util.Objects;

public class VariantWithImage implements Serializable {
@Embedded
Expand All @@ -31,4 +32,18 @@ public class VariantWithImage implements Serializable {
entityColumn = "id"
)
public Image image;

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
VariantWithImage that = (VariantWithImage) o;
return Objects.equals(variant, that.variant) &&
Objects.equals(image, that.image);
}

@Override
public int hashCode() {
return Objects.hash(variant, image);
}
}
157 changes: 74 additions & 83 deletions app/src/main/java/org/piwigo/data/repository/CategoriesRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,52 +80,64 @@ public Observable<PositionedItem<Category>> getCategories(@Nullable Integer cate
db = mCache; // this will keep the database if the account is switched. As the old DB will be closed this thread will be reporting an exception but we accept that for now
}

// Flowable<PositionedItem<Category>> remotes = mRestCategoryRepo.getCategories(
// categoryId,
// mPreferences.getString(PreferencesRepository.KEY_PREF_DOWNLOAD_SIZE))
// .subscribeOn(ioScheduler)
// .observeOn(ioScheduler)
// .toFlowable(BackpressureStrategy.BUFFER)
//
// .zipWith(Flowable.range(0, Integer.MAX_VALUE), (restCat, counter) -> {
//
// Category c = new Category();
// c.name = restCat.name;
// c.id = restCat.id;
// if (restCat.idUppercat != 0) {
// c.parentCatId = restCat.idUppercat;
// }
// c.nbImages = restCat.nbImages;
// c.thumbnailUrl = restCat.thumbnailUrl;
// c.globalRank = restCat.globalRank;
// c.comment = restCat.comment;
// c.nbCategories = restCat.nbCategories;
// c.representativePictureId = restCat.representativePictureId;
// c.totalNbImages = restCat.totalNbImages;
//// db.categoryDao().upsert(c);
//
// return new PositionedItem<Category>(counter, c, true);
// })
// // TODO: delete categories in database after they have been deleted on the server
// // TODO: #90 generalize sorting
// .sorted((categoryItem1, categoryItem2) -> NaturalOrderComparator.compare(categoryItem1.getItem().globalRank, categoryItem2.getItem().globalRank));
//
// if(db == null){
// return remotes.toObservable();
// }else {
Flowable<Integer> remoteIDs = mRestCategoryRepo.getCategories(
categoryId,
mPreferences.getString(PreferencesRepository.KEY_PREF_DOWNLOAD_SIZE))
.subscribeOn(ioScheduler)
.observeOn(ioScheduler)
.toFlowable(BackpressureStrategy.BUFFER)
.zipWith(Flowable.range(0, Integer.MAX_VALUE), (restCat, counter) -> restCat.id);

Flowable<PositionedItem<Category>> remotes = mRestCategoryRepo.getCategories(
categoryId,
mPreferences.getString(PreferencesRepository.KEY_PREF_DOWNLOAD_SIZE))
.subscribeOn(ioScheduler)
.observeOn(ioScheduler)
.toFlowable(BackpressureStrategy.BUFFER)
.zipWith(Flowable.range(0, Integer.MAX_VALUE), (restCat, counter) -> {

Category c = new Category();
c.name = restCat.name;
c.id = restCat.id;
if (restCat.idUppercat != 0) {
c.parentCatId = restCat.idUppercat;
}
c.nbImages = restCat.nbImages;
c.thumbnailUrl = restCat.thumbnailUrl;
c.globalRank = restCat.globalRank;
c.comment = restCat.comment;
c.nbCategories = restCat.nbCategories;
c.representativePictureId = restCat.representativePictureId;
c.totalNbImages = restCat.totalNbImages;
db.categoryDao().upsert(c);

return new PositionedItem<Category>(counter, c, true);
})
// TODO: #90 generalize sorting
.sorted((categoryItem1, categoryItem2) -> NaturalOrderComparator.compare(categoryItem1.getItem().globalRank, categoryItem2.getItem().globalRank));

// remotes.subscribe();
if(db == null){
return remotes.toObservable();
}else {
return db.categoryDao().getCategoriesIn(categoryId)
.subscribeOn(ioScheduler)
.observeOn(ioScheduler)
.flattenAsFlowable(s -> s)
.zipWith(Flowable.range(0, Integer.MAX_VALUE),
(item, counter) -> {
Log.d("m_cache_sync","Read "+item.name);
return new PositionedItem<Category>(counter, item, true);
.subscribeOn(ioScheduler)
.observeOn(ioScheduler)
.flattenAsFlowable(s -> s)
/* .filter(category -> {
Log.d("m_cache_sync_cat","Read "+category.name);
if(!remoteIDs.contains(category.id).blockingGet()) {
Log.d("m_cache_sync_cat","Deleted "+category.name);
return false;
}
)
// .concatWith(remotes)
.toObservable();
// }
return true;
})*/
.zipWith(Flowable.range(0, Integer.MAX_VALUE), (item, counter) -> {
return new PositionedItem<Category>(counter, item, true);
})
.concatWith(remotes)
.toObservable();
}
}
/**
* Called when the account is changed.
Expand All @@ -139,56 +151,35 @@ public void onChanged(Account account) {
}
}

public void updateCategories(Integer categoryId) {
Log.d("CategoriesRepository", "getCategories");
public void updateCategoryCache(@Nullable Integer categoryId) {
CacheDatabase db;
synchronized (dbAccountLock) {
db = mCache; // this will keep the database if the account is switched. As the old DB will be closed this thread will be reporting an exception but we accept that for now
}

Flowable<Category> restCategories = mRestCategoryRepo.getCategories(
Flowable<Integer> remoteIDs = mRestCategoryRepo.getCategories(
categoryId,
mPreferences.getString(PreferencesRepository.KEY_PREF_DOWNLOAD_SIZE))
.subscribeOn(ioScheduler)
.observeOn(ioScheduler)
.toFlowable(BackpressureStrategy.BUFFER)
.zipWith(Flowable.range(0, Integer.MAX_VALUE), (restCat, counter) -> restCat.id);

.zipWith(Flowable.range(0, Integer.MAX_VALUE), (restCat, counter) -> {
Log.d("m_cache_sync","Found "+restCat.name);
Category c = new Category();
c.name = restCat.name;
c.id = restCat.id;
if (restCat.idUppercat != 0) {
c.parentCatId = restCat.idUppercat;
}
c.nbImages = restCat.nbImages;
c.thumbnailUrl = restCat.thumbnailUrl;
c.globalRank = restCat.globalRank;
c.comment = restCat.comment;
c.nbCategories = restCat.nbCategories;
c.representativePictureId = restCat.representativePictureId;
c.totalNbImages = restCat.totalNbImages;
db.categoryDao().upsert(c);

return c;
});
restCategories.subscribe();

Flowable<Category> dbCategories = db.categoryDao().getCategoriesIn(categoryId)
.observeOn(ioScheduler)
.subscribeOn(ioScheduler)
.flattenAsFlowable(s -> s)
.zipWith(Flowable.range(0, Integer.MAX_VALUE),
(item, counter) -> {
if(!restCategories.contains(item).blockingGet()) {
Log.d("m_cache_sync","Deleted "+item.name);
db.imageCategoryMapDao().deleteFromCategory(item.id);
db.categoryDao().delete(item);
}
return item;
if(db != null) {
db.categoryDao().getCategoriesIn(categoryId)
.subscribeOn(ioScheduler)
.observeOn(ioScheduler)
.flattenAsFlowable(s -> s)
.zipWith(Flowable.range(0, Integer.MAX_VALUE), (item, counter) -> {
if(!remoteIDs.contains(item.id).blockingGet()) {
Log.d("m_cache_sync_cat","Deleted "+item.name);
db.imageCategoryMapDao().deleteFromCategory(item.id);
db.categoryDao().delete(item);
}
);

dbCategories.subscribe();
return new PositionedItem<Category>(counter, item, true);
})
.toObservable()
.subscribe();
}
}
}
Loading