iT邦幫忙

2024 iThome 鐵人賽

DAY 5
0

這篇我們實作get方法


DatabasePresenter

  • getData:
@Override
    public void getData(String roomNumber){
        new Thread(() -> {
            database.collection("RoomData")
                    .whereEqualTo("RoomNumber", roomNumber)
                    .get()
                    .addOnCompleteListener(task -> {
                        if(task.isSuccessful() && task.getResult() != null && task.getResult().getDocuments().size() > 0){
                            DocumentSnapshot documentSnapshot = task.getResult().getDocuments().get(0);

                            String getId = documentSnapshot.getId();
                            String getRoomNumber = documentSnapshot.getString("RoomNumber");
                            Boolean getPlayer1OnlineState = documentSnapshot.getBoolean("Player1OnlineState");
                            Boolean getPlayer2OnlineState = documentSnapshot.getBoolean("Player2OnlineState");
                            String getMoveState = documentSnapshot.getString("MoveState");
                            HashMap<String,Object> getChessboardData = (HashMap<String, Object>) documentSnapshot.get("ChessboardData");
                            HashMap<String,Object> getChessmanMoveData = (HashMap<String, Object>) documentSnapshot.get("ChessmanMoveData");
                            String getTurn = documentSnapshot.getString("Turn");
                            HashMap<String,Object> getPawnMove1 = (HashMap<String, Object>) documentSnapshot.get("PawnMove1");
                            HashMap<String,Object> getPawnMove2 = (HashMap<String, Object>) documentSnapshot.get("PawnMove2");
                            HashMap<String,Object> getCastlingMove1 = (HashMap<String, Object>) documentSnapshot.get("CastlingMove1");
                            HashMap<String,Object> getCastlingMove2 = (HashMap<String, Object>) documentSnapshot.get("CastlingMove2");
                            String getDate = documentSnapshot.getString("Date");
                            String getPlayer1message = documentSnapshot.getString("Player1message");
                            String getPlayer2message = documentSnapshot.getString("Player2message");
                            String getWinPlayer = documentSnapshot.getString("WinPlayer");

                            callback.getSuccess(getId,
                                    getRoomNumber,
                                    getPlayer1OnlineState,
                                    getPlayer2OnlineState,
                                    getMoveState,
                                    getChessboardData,
                                    getChessmanMoveData,
                                    getTurn,
                                    getPawnMove1,
                                    getPawnMove2,
                                    getCastlingMove1,
                                    getCastlingMove2,
                                    getDate,
                                    getPlayer1message,
                                    getPlayer2message,
                                    getWinPlayer);
                        }
                        else{
                            callback.getError();
                        }
                    });
        }).start();
    }

這裡我們取得資料中各端點的快照,以獲得各項資料


下一篇來實作upload方法


上一篇
【DAY 04】firebase - DatabasePresenter & DatabaseContract - addData
下一篇
【DAY 06】firebase - DatabasePresenter & DatabaseContract - uploadData
系列文
基於Firebase整合生成式AI研究開發雙人國際象棋系統(Based on Firebase and AI to research chess system)30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言