Skip to content

Commit 818c3fe

Browse files
Simplify recent boards updates using Streams
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.
1 parent a3fb8f3 commit 818c3fe

File tree

1 file changed

+12
-28
lines changed

1 file changed

+12
-28
lines changed

app/src/processing/app/Base.java

+12-28
Original file line numberDiff line numberDiff line change
@@ -1366,37 +1366,21 @@ public void onBoardOrPortChange() {
13661366
}
13671367
}
13681368

1369-
// Update recent boards list in preferences
1370-
List<String> newRecentBoardIds = new ArrayList<String>();
1371-
String currentBoard = PreferencesData.get("board");
1372-
for (String recentBoard : PreferencesData.getCollection("recent.boards")){
1373-
if (!recentBoard.equals(currentBoard)) {
1374-
newRecentBoardIds.add(recentBoard);
1375-
}
1376-
}
1377-
newRecentBoardIds.add(0, currentBoard);
1378-
1379-
int numBoards = 0;
1380-
1381-
if (PreferencesData.has("recent.num_boards")) {
1382-
numBoards = PreferencesData.getInteger("recent.num_boards");
1383-
}
1369+
// Prepend current board to recent boards and limit to max size
1370+
String currentBoard = PreferencesData.get("board");
1371+
Stream newRecentBoardIds =
1372+
Stream.concat(
1373+
Stream.of(currentBoard),
1374+
PreferencesData.getCollection("recent.boards").stream()
1375+
).distinct().limit(PreferencesData.getInteger("recent.num_boards"));
13841376

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

1390-
// If recent.num_boards is 0, interpret this as the feature
1391-
// being turned off. There's no need to rebuild the menu
1392-
// because it will be hidden
1393-
if (numBoards > 0) {
1394-
try {
1395-
rebuildRecentBoardsList();
1396-
} catch (Exception e) {
1397-
//TODO show error
1398-
e.printStackTrace();
1399-
}
1379+
try {
1380+
rebuildRecentBoardsList();
1381+
} catch (Exception e) {
1382+
//TODO show error
1383+
e.printStackTrace();
14001384
}
14011385

14021386
// Update editors status bar

0 commit comments

Comments
 (0)