Skip to content

Commit

Permalink
Simplify recent boards updates using Streams
Browse files Browse the repository at this point in the history
This makes the code a lot more compact.

Also, call rebuildRecentBoardsList unconditionally, if recent.num_boards
is 0, then the recent boards list will be empty anyway, so it is a noop.
  • Loading branch information
matthijskooijman committed Oct 26, 2020
1 parent a3fb8f3 commit 818c3fe
Showing 1 changed file with 12 additions and 28 deletions.
40 changes: 12 additions & 28 deletions app/src/processing/app/Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -1366,37 +1366,21 @@ public void onBoardOrPortChange() {
}
}

// Update recent boards list in preferences
List<String> newRecentBoardIds = new ArrayList<String>();
String currentBoard = PreferencesData.get("board");
for (String recentBoard : PreferencesData.getCollection("recent.boards")){
if (!recentBoard.equals(currentBoard)) {
newRecentBoardIds.add(recentBoard);
}
}
newRecentBoardIds.add(0, currentBoard);

int numBoards = 0;

if (PreferencesData.has("recent.num_boards")) {
numBoards = PreferencesData.getInteger("recent.num_boards");
}
// Prepend current board to recent boards and limit to max size
String currentBoard = PreferencesData.get("board");
Stream newRecentBoardIds =
Stream.concat(
Stream.of(currentBoard),
PreferencesData.getCollection("recent.boards").stream()
).distinct().limit(PreferencesData.getInteger("recent.num_boards"));

while (newRecentBoardIds.size() > numBoards) {
newRecentBoardIds.remove(newRecentBoardIds.size() - 1);
}
PreferencesData.setCollection("recent.boards", newRecentBoardIds);

// If recent.num_boards is 0, interpret this as the feature
// being turned off. There's no need to rebuild the menu
// because it will be hidden
if (numBoards > 0) {
try {
rebuildRecentBoardsList();
} catch (Exception e) {
//TODO show error
e.printStackTrace();
}
try {
rebuildRecentBoardsList();
} catch (Exception e) {
//TODO show error
e.printStackTrace();
}

// Update editors status bar
Expand Down

0 comments on commit 818c3fe

Please sign in to comment.