|
|
|
@ -22,6 +22,7 @@ import com.android.volley.VolleyError; |
|
|
|
|
|
|
|
|
|
import org.floens.chan.Chan; |
|
|
|
|
import org.floens.chan.chan.ChanUrls; |
|
|
|
|
import org.floens.chan.core.database.DatabaseManager; |
|
|
|
|
import org.floens.chan.core.model.Board; |
|
|
|
|
import org.floens.chan.core.net.BoardsRequest; |
|
|
|
|
import org.floens.chan.utils.Logger; |
|
|
|
@ -35,135 +36,134 @@ import java.util.Map; |
|
|
|
|
|
|
|
|
|
import de.greenrobot.event.EventBus; |
|
|
|
|
|
|
|
|
|
public class BoardManager { |
|
|
|
|
public class BoardManager implements Response.Listener<List<Board>>, Response.ErrorListener { |
|
|
|
|
private static final String TAG = "BoardManager"; |
|
|
|
|
private static final Comparator<Board> savedOrder = new Comparator<Board>() { |
|
|
|
|
|
|
|
|
|
private static final Comparator<Board> ORDER_SORT = new Comparator<Board>() { |
|
|
|
|
@Override |
|
|
|
|
public int compare(Board lhs, Board rhs) { |
|
|
|
|
return lhs.order < rhs.order ? -1 : 1; |
|
|
|
|
return lhs.order - rhs.order; |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
private List<Board> allBoards; |
|
|
|
|
private Map<String, Board> allBoardsByValue = new HashMap<>(); |
|
|
|
|
|
|
|
|
|
public BoardManager() { |
|
|
|
|
loadBoards(); |
|
|
|
|
loadFromServer(); |
|
|
|
|
private static final Comparator<Board> NAME_SORT = new Comparator<Board>() { |
|
|
|
|
@Override |
|
|
|
|
public int compare(Board lhs, Board rhs) { |
|
|
|
|
return lhs.name.compareTo(rhs.name); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// TODO: synchronize
|
|
|
|
|
public Board getBoardByValue(String value) { |
|
|
|
|
return allBoardsByValue.get(value); |
|
|
|
|
} |
|
|
|
|
private final DatabaseManager databaseManager; |
|
|
|
|
|
|
|
|
|
public List<Board> getAllBoards() { |
|
|
|
|
return allBoards; |
|
|
|
|
} |
|
|
|
|
private final List<Board> boards; |
|
|
|
|
private final List<Board> savedBoards = new ArrayList<>(); |
|
|
|
|
private final Map<String, Board> boardsByCode = new HashMap<>(); |
|
|
|
|
|
|
|
|
|
public List<Board> getSavedBoards() { |
|
|
|
|
List<Board> saved = new ArrayList<>(allBoards.size()); |
|
|
|
|
public BoardManager(DatabaseManager databaseManager) { |
|
|
|
|
this.databaseManager = databaseManager; |
|
|
|
|
boards = databaseManager.getBoards(); |
|
|
|
|
|
|
|
|
|
for (Board b : allBoards) { |
|
|
|
|
if (b.saved) |
|
|
|
|
saved.add(b); |
|
|
|
|
if (boards.isEmpty()) { |
|
|
|
|
Logger.d(TAG, "Loading default boards"); |
|
|
|
|
boards.addAll(getDefaultBoards()); |
|
|
|
|
saveDatabase(); |
|
|
|
|
update(true); |
|
|
|
|
} else { |
|
|
|
|
update(false); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Collections.sort(saved, savedOrder); |
|
|
|
|
|
|
|
|
|
return saved; |
|
|
|
|
Chan.getVolleyRequestQueue().add(new BoardsRequest(ChanUrls.getBoardsUrl(), this, this)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public boolean getBoardExists(String board) { |
|
|
|
|
for (Board e : getAllBoards()) { |
|
|
|
|
if (e.value.equals(board)) { |
|
|
|
|
return true; |
|
|
|
|
@Override |
|
|
|
|
public void onResponse(List<Board> response) { |
|
|
|
|
List<Board> boardsToAddWs = new ArrayList<>(); |
|
|
|
|
List<Board> boardsToAddNws = new ArrayList<>(); |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < response.size(); i++) { |
|
|
|
|
Board serverBoard = response.get(i); |
|
|
|
|
|
|
|
|
|
Board existing = getBoardByCode(serverBoard.code); |
|
|
|
|
if (existing != null) { |
|
|
|
|
serverBoard.id = existing.id; |
|
|
|
|
serverBoard.saved = existing.saved; |
|
|
|
|
serverBoard.order = existing.order; |
|
|
|
|
boards.set(boards.indexOf(existing), serverBoard); |
|
|
|
|
} else { |
|
|
|
|
serverBoard.saved = true; |
|
|
|
|
if (serverBoard.workSafe) { |
|
|
|
|
boardsToAddWs.add(serverBoard); |
|
|
|
|
} else { |
|
|
|
|
boardsToAddNws.add(serverBoard); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void updateSavedBoards() { |
|
|
|
|
Chan.getDatabaseManager().setBoards(allBoards); |
|
|
|
|
Collections.sort(boardsToAddWs, NAME_SORT); |
|
|
|
|
Collections.sort(boardsToAddNws, NAME_SORT); |
|
|
|
|
|
|
|
|
|
notifyChanged(); |
|
|
|
|
for (int i = 0; i < boardsToAddWs.size(); i++) { |
|
|
|
|
Board board = boardsToAddWs.get(i); |
|
|
|
|
board.order = boards.size(); |
|
|
|
|
boards.add(board); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void updateByValueMap() { |
|
|
|
|
allBoardsByValue.clear(); |
|
|
|
|
for (Board test : allBoards) { |
|
|
|
|
allBoardsByValue.put(test.value, test); |
|
|
|
|
} |
|
|
|
|
for (int i = 0; i < boardsToAddNws.size(); i++) { |
|
|
|
|
Board board = boardsToAddNws.get(i); |
|
|
|
|
board.order = boards.size(); |
|
|
|
|
boards.add(board); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void notifyChanged() { |
|
|
|
|
EventBus.getDefault().post(new BoardsChangedMessage()); |
|
|
|
|
saveDatabase(); |
|
|
|
|
update(true); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void storeBoards() { |
|
|
|
|
updateByValueMap(); |
|
|
|
|
|
|
|
|
|
Chan.getDatabaseManager().setBoards(allBoards); |
|
|
|
|
notifyChanged(); |
|
|
|
|
@Override |
|
|
|
|
public void onErrorResponse(VolleyError error) { |
|
|
|
|
Logger.e(TAG, "Failed to get boards from server"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void loadBoards() { |
|
|
|
|
allBoards = Chan.getDatabaseManager().getBoards(); |
|
|
|
|
if (allBoards.size() == 0) { |
|
|
|
|
Logger.d(TAG, "Loading default boards"); |
|
|
|
|
allBoards = getDefaultBoards(); |
|
|
|
|
storeBoards(); |
|
|
|
|
// Thread-safe
|
|
|
|
|
public boolean getBoardExists(String code) { |
|
|
|
|
return getBoardByCode(code) != null; |
|
|
|
|
} |
|
|
|
|
updateByValueMap(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void setBoardsFromServer(List<Board> serverList) { |
|
|
|
|
boolean has; |
|
|
|
|
for (Board serverBoard : serverList) { |
|
|
|
|
has = false; |
|
|
|
|
for (int i = 0; i < allBoards.size(); i++) { |
|
|
|
|
if (allBoards.get(i).value.equals(serverBoard.value)) { |
|
|
|
|
Board old = allBoards.get(i); |
|
|
|
|
serverBoard.id = old.id; |
|
|
|
|
serverBoard.saved = old.saved; |
|
|
|
|
serverBoard.order = old.order; |
|
|
|
|
allBoards.set(i, serverBoard); |
|
|
|
|
|
|
|
|
|
has = true; |
|
|
|
|
break; |
|
|
|
|
// Thread-safe
|
|
|
|
|
public Board getBoardByCode(String code) { |
|
|
|
|
synchronized (boardsByCode) { |
|
|
|
|
return boardsByCode.get(code); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!has) { |
|
|
|
|
/*Logger.d(TAG, "Adding unknown board: " + serverBoard.value); |
|
|
|
|
|
|
|
|
|
if (serverBoard.workSafe) { |
|
|
|
|
serverBoard.saved = true; |
|
|
|
|
serverBoard.order = allBoards.size(); |
|
|
|
|
}*/ |
|
|
|
|
|
|
|
|
|
allBoards.add(serverBoard); |
|
|
|
|
public List<Board> getAllBoards() { |
|
|
|
|
return boards; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public List<Board> getSavedBoards() { |
|
|
|
|
return savedBoards; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
storeBoards(); |
|
|
|
|
public void flushOrderAndSaved() { |
|
|
|
|
saveDatabase(); |
|
|
|
|
update(true); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void loadFromServer() { |
|
|
|
|
Chan.getVolleyRequestQueue().add( |
|
|
|
|
new BoardsRequest(ChanUrls.getBoardsUrl(), new Response.Listener<List<Board>>() { |
|
|
|
|
@Override |
|
|
|
|
public void onResponse(List<Board> data) { |
|
|
|
|
setBoardsFromServer(data); |
|
|
|
|
private void update(boolean notify) { |
|
|
|
|
savedBoards.clear(); |
|
|
|
|
savedBoards.addAll(filterSaved(boards)); |
|
|
|
|
synchronized (boardsByCode) { |
|
|
|
|
boardsByCode.clear(); |
|
|
|
|
for (Board board : boards) { |
|
|
|
|
boardsByCode.put(board.code, board); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if (notify) { |
|
|
|
|
EventBus.getDefault().post(new BoardsChangedMessage()); |
|
|
|
|
} |
|
|
|
|
}, new Response.ErrorListener() { |
|
|
|
|
@Override |
|
|
|
|
public void onErrorResponse(VolleyError error) { |
|
|
|
|
Logger.e(TAG, "Failed to get boards from server"); |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
private void saveDatabase() { |
|
|
|
|
databaseManager.setBoards(boards); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private List<Board> getDefaultBoards() { |
|
|
|
@ -178,6 +178,18 @@ public class BoardManager { |
|
|
|
|
return list; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private List<Board> filterSaved(List<Board> all) { |
|
|
|
|
List<Board> saved = new ArrayList<>(all.size()); |
|
|
|
|
for (int i = 0; i < all.size(); i++) { |
|
|
|
|
Board board = all.get(i); |
|
|
|
|
if (board.saved) { |
|
|
|
|
saved.add(board); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
Collections.sort(saved, ORDER_SORT); |
|
|
|
|
return saved; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static class BoardsChangedMessage { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|