Skip to content

Commit

Permalink
Merge pull request #282 from SDPepe/fix/local-db-file-iteration
Browse files Browse the repository at this point in the history
Fix NullPointerException in local db
  • Loading branch information
ADGLY authored Jun 4, 2021
2 parents d4a9679 + 50a8e9f commit e80b382
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 40 deletions.
11 changes: 0 additions & 11 deletions app/src/main/java/ch/epfl/sdp/appart/AdActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -422,17 +422,6 @@ private void updateBookmarkIconState() {
}

private void updateBookmarkLocalDatabase(User currentUser) {
/*localdb.getCards().thenAcceptAsync(cards -> {
cards.forEach(card -> {
if (card.getAdId().equals(adId)) {
setBookmarkIcon(card.getAdId().equals(adId));
return;
}
});
});*/
/*localdb.getCurrentUser().thenAcceptAsync(user -> {
setBookmarkIcon(user.getFavoritesIds().contains(adId));
});*/
setBookmarkIcon(currentUser.getFavoritesIds().contains(adId));

}
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/ch/epfl/sdp/appart/FavoriteActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,11 @@ private void writeAdsToDisk() {
Adding a clean favorites as a preparation for the possibility to
remove favorites. However, this should not clean the currentUser
data, which it currently does.
Update : Now, this is fixed. We actually have this clean for
safety. In case one part of the synchronization does not work.
*/
//localdb.cleanFavorites();
localdb.cleanFavoritesWithoutCurrentUser();
List<Card> favs = mViewModel.getFavorites().getValue().first;
for (int i = 0; i < favs.size(); i++) {
Card card = favs.get(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ public FirestoreDatabaseService() {
Do we use this or not ?
If we do, this "overrides" or local DB system. However, we know that the data will
have the same version that our local db. So maybe, we can think of the local db
as a backup for data that can't be cached by firestore.*/
as a backup for data that can't be cached by firestore.
db.clearPersistence();
FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
.setPersistenceEnabled(false).build();
db.setFirestoreSettings(settings);
//*/
*/
storage = FirebaseStorage.getInstance();
adHelper = new FirestoreAdHelper();
imageHelper = new FirestoreImageHelper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ private static void readAdFolder(File folder, List<Card> cards,
* or not
*/
static CompletableFuture<Void> readAdDataForAUser(String currentUserID,
List<Card> cards, Map<String, Ad> idsToAd,
Map<String, List<String>> adIdsToPanoramas) {
List<Card> cards,
Map<String, Ad> idsToAd,
Map<String,
List<String>> adIdsToPanoramas) {
String currentUserFolderPath =
LocalDatabasePaths.currentUserFolder(currentUserID);

Expand All @@ -116,17 +118,21 @@ static CompletableFuture<Void> readAdDataForAUser(String currentUserID,
return CompletableFuture.runAsync(() -> {

File[] folders = favFolder.listFiles(fileFilter);
if (folders == null)
throw new CompletionException(new LocalDatabaseException("The" +

/*throw new CompletionException(new LocalDatabaseException
("The" +
" ad folder : " + favFolder.getPath() + " doesn't " +
"contain any folders !"));
for (File folder : folders) {
try {
readAdFolder(folder, cards, idsToAd,
adIdsToPanoramas);
} catch (LocalDatabaseException e) {
e.printStackTrace();
throw new CompletionException(e);
"contain any folders !"));*/

if (folders != null) {
for (File folder : folders) {
try {
readAdFolder(folder, cards, idsToAd,
adIdsToPanoramas);
} catch (LocalDatabaseException e) {
e.printStackTrace();
throw new CompletionException(e);
}
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,21 @@ private void clearMemory() {
* contains. Useful for testing or if we reached illegal state.
*/
public void cleanFavorites() {
File favoritesDir =
new File(LocalDatabasePaths.favoritesFolder());
FileIO.deleteDirectory(favoritesDir);
cleanFolder(new File(LocalDatabasePaths.favoritesFolder()));
}

/**
* This completely removes the favorites folder except the current user
* data.
* It is useful when loading the favorites page.
*/
@Override
public void cleanFavoritesWithoutCurrentUser() {
cleanFolder(new File(LocalDatabasePaths.currentUserFolder(getCurrentUser().getUserId())));
}

private void cleanFolder(File folder) {
FileIO.deleteDirectory(folder);
clearMemory();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ CompletableFuture<Void> writeCompleteAd(String adId,
*/
void cleanFavorites();

/**
* This completely removes the favorites folder except the current user
* data.
* It is useful when loading the favorites page.
*/
void cleanFavoritesWithoutCurrentUser();

/**
* Removes a card with id cardId. The user associated with this card is
* removed only if it isn't referenced by any other card.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.File;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
Expand All @@ -22,7 +21,7 @@ public class LocalUserReader {
* @param userFile a file pointing to the user's directory
*/
private static void readUserFolder(File userFile,
Map<String, User> idsToUser
Map<String, User> idsToUser
, Set<String> userIds) throws LocalDatabaseException {

String dataPath =
Expand All @@ -46,8 +45,8 @@ private static void readUserFolder(File userFile,
* folders in the users folder. The reading on disk happens asynchronously.
*
* @param currentUserID the current user id
* @param idsToUser a map mapping user ids to users
* @param userIds a set of user ids
* @param idsToUser a map mapping user ids to users
* @param userIds a set of user ids
* @return a completable future that indicates if the operation succeeded
* or not
*/
Expand All @@ -58,15 +57,19 @@ static CompletableFuture<Void> readUsers(String currentUserID,
LocalDatabasePaths.usersFolder(currentUserID);
File userFolder = new File(userPath);
return CompletableFuture.runAsync(() -> {
for (File folder :
Objects.requireNonNull(userFolder.listFiles(File::isDirectory))) {
try {
readUserFolder(folder, idsToUser, userIds);
} catch (LocalDatabaseException e) {
e.printStackTrace();
throw new CompletionException(e);
File[] files = userFolder.listFiles(File::isDirectory);
if (files != null) {
for (File folder :
files) {
try {
readUserFolder(folder, idsToUser, userIds);
} catch (LocalDatabaseException e) {
e.printStackTrace();
throw new CompletionException(e);
}
}
}

});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ public void cleanFavorites() {
throw new NotImplementedError();
}

@Override
public void cleanFavoritesWithoutCurrentUser() {
throw new NotImplementedError();
}

@Override
public void removeCard(String cardId) {
throw new NotImplementedError();
Expand Down

0 comments on commit e80b382

Please sign in to comment.