fix: count of favorites is synched when selecting/deselecting a favorite. Favorite count is also present after loading all games.

This commit is contained in:
lantzelot-swe 2021-01-22 22:49:15 +01:00
parent fd12733038
commit 973652c99c
2 changed files with 25 additions and 3 deletions

View File

@ -377,7 +377,7 @@ public class ListPanel extends JPanel
if (!uiModel.isDataChanged())
{
uiModel.toggleFavorite(list.getSelectedValue());
mainPanel.repaintAfterModifications();
}
}
}

View File

@ -34,6 +34,8 @@ public class MainViewModel extends AbstractModel
private GameView selectedGameView;
private int allGamesCount = 0;
private int favoritesCount = 0;
private GameView favoritesView;
private InfoModel infoModel = new InfoModel();
private JoystickModel joy1Model = new JoystickModel(true);
@ -92,11 +94,13 @@ public class MainViewModel extends AbstractModel
selectedGameView = new GameView(GameView.ALL_GAMES_ID);
selectedGameView.setName("All Games");
selectedGameView.setSqlQuery("");
gameViewModel.addElement(selectedGameView);
//Add favorites view
GameView favoritesView = new GameView(GameView.FAVORITES_ID);
favoritesView = new GameView(GameView.FAVORITES_ID);
favoritesView.setName("Favorites");
favoritesView.setSqlQuery(" WHERE Favorite = 1");
gameViewModel.addElement(selectedGameView);
gameViewModel.addElement(favoritesView);
List<GameView> gameViewList = dbConnector.loadGameViews();
@ -238,6 +242,16 @@ public class MainViewModel extends AbstractModel
if (gameView.getGameViewId() == GameView.ALL_GAMES_ID)
{
this.allGamesCount = gamesList.size();
//Update favorites count
favoritesCount = 0;
for (GameListData gameListData : gamesList)
{
if (gameListData.isFavorite())
{
favoritesCount++;
}
}
favoritesView.setGameCount(favoritesCount);
}
this.disableChangeNotification(false);
logger.debug("...done.");
@ -474,6 +488,14 @@ public class MainViewModel extends AbstractModel
{
dbConnector.toggleFavorite(data.getGameId(), data.getFavorite());
data.toggleFavorite();
if (data.isFavorite())
{
favoritesView.setGameCount(++favoritesCount);
}
else
{
favoritesView.setGameCount(--favoritesCount);
}
gameListModel.notifyChange();
}
}