Newer
Older
IrisClient / app / src / main / java / org / ntlab / irisclient / MemberRoomActivity.java
package org.ntlab.irisclient;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.RecyclerView;
import retrofit2.Call;
import retrofit2.Retrofit;

import android.content.ClipData;
import android.content.ClipDescription;
import android.content.ClipboardManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.android.material.snackbar.Snackbar;

import org.ntlab.irisclient.entities.RoomJson;
import org.ntlab.irisclient.models.Member;
import org.ntlab.irisclient.models.Settings;
import org.ntlab.irisclient.resources.RoomsRest;
import org.ntlab.irisclient.viewmodels.RoomViewModel;

import java.util.ArrayList;
import java.util.List;


public class MemberRoomActivity extends AppCompatActivity {

    //フィールド
    ArrayList<String> viewMembers = new ArrayList<>();
    ArrayList<String> oldMembers = new ArrayList<>();
    List<Member> allMembers = new ArrayList<>();
    String roomId = "null";
    String myName = "null";




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_member_room);

        roomId = ((Iris) getApplication()).getRid();
        String nickname = ((Iris) getApplication()).getNickname();

        //--------------------------------------------------------------------------
        //自分の名前
        Intent intentMain = getIntent();
        myName = intentMain.getStringExtra("nickname");

        //--------------------------------------------------------------------------
        //メンバー表示
        Member mn = new Member(myName);
        mn.setBelongs("g");
        mn.setMaster(false);

        viewMembers.add(mn.getNickname());

        //--------------------------------------------------------------------------
        //RoomViewModelへのアクセス
        RoomViewModel roomViewModel = new ViewModelProvider(this).get(RoomViewModel.class);

        roomViewModel.setRid(roomId);

        ListView membersList = (ListView) findViewById(R.id.MembersList);

        BaseAdapter adapter = new MemberListAdapter(this.getApplicationContext(), R.layout.member_list_layout, viewMembers, allMembers);
        membersList.setAdapter(adapter);

        TextView drawTimer = (TextView) findViewById(R.id.drawTimer);
        TextView dTimersec = (TextView) findViewById(R.id.dTimersec);
        TextView gameTimer = (TextView) findViewById(R.id.gameTimer);
        TextView gTimersec = (TextView) findViewById(R.id.gTimersec);

        //LiveData(List<Member>)への購読
        roomViewModel.getMembersLiveData().observe(this, new Observer<List<Member>>() {
            //private List<Member> members;
            @Override
            public void onChanged(List<Member> irisMembers) {

                viewMembers.clear();
                irisMembers.forEach(m -> viewMembers.add(m.getNickname()));
                allMembers = irisMembers;

                Member myInfo = allMembers.get(myNameIndex(allMembers,myName));
                ((Iris) getApplication()).setMaster(myInfo.isMaster());
                ((Iris) getApplication()).setTeam(myInfo.getBelongs());

                BaseAdapter adapter = new MemberListAdapter(getApplicationContext(), R.layout.member_list_layout, viewMembers, allMembers);
                membersList.setAdapter(adapter);

            }

        });

        roomViewModel.getStateLiveData().observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer state) {
                if(state >= 1){
                    Intent intent = new Intent(MemberRoomActivity.this,DrawingActivity.class);
                    startActivity(intent);
                }

                if(state == -1){
                    roomDeleted(roomViewModel);
                }
            }
        });

        roomViewModel.getSettingsLiveData().observe(this, new Observer<Settings>() {
            @Override
            public void onChanged(Settings settings) {
                if(settings.isDrawingTimer()) {
                    drawTimer.setText("お絵描きタイマー:ON");
                    dTimersec.setVisibility(View.VISIBLE);
                    dTimersec.setText(Integer.toString(settings.getDrawingTimerTimes())+"秒");

                }else {
                    drawTimer.setText("お絵描きタイマー:OFF");
                    dTimersec.setVisibility(View.GONE);
                }

                if(settings.isGameTimer()) {
                    gameTimer.setText("ゲームタイマー:ON");
                    gTimersec.setVisibility(View.VISIBLE);
                    gTimersec.setText(Integer.toString(settings.getGameTimerTimes())+"秒");

                }else {
                    gameTimer.setText("ゲームタイマー:OFF");
                    gTimersec.setVisibility(View.GONE);
                }
            }
        });


        //--------------------------------------------------------------------------
        //各ボタン処理
        Button redMasterButton = (Button) findViewById(R.id.beRedMaster);
        Button redSpyButton = (Button) findViewById(R.id.beRedSpy);
        Button blueMasterButton = (Button) findViewById(R.id.beBlueMaster);
        Button blueSpyButton = (Button) findViewById(R.id.beBlueSpy);
        ImageButton copyButton = (ImageButton) findViewById(R.id.copyButton);

        //MemberRoom専用ボタン
        ImageButton homeButton = (ImageButton) findViewById(R.id.homeButton);

        //チームと役職の変更
        //赤チーム・スパイマスター
        redMasterButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roomViewModel.changeBelongsAndMaster(roomId, myName,"r",true);
            }
        });

        //赤チーム・諜報員
        redSpyButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roomViewModel.changeBelongsAndMaster(roomId, myName,"r",false);
            }
        });

        //青チーム・スパイマスター
        blueMasterButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roomViewModel.changeBelongsAndMaster(roomId, myName,"b",true);
            }
        });

        //青チーム・諜報員
        blueSpyButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roomViewModel.changeBelongsAndMaster(roomId, myName,"b",false);
            }
        });

        //roomIDをクリップボードにコピー、他アプリに共有
        copyButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CopyClipBoard();
                CreateCommonIntent();
                Snackbar.make(v, "コピーしました", Snackbar.LENGTH_LONG).show();
            }
        });

        //部屋の退出、最初の画面(ホーム)に戻る
        homeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                homeButton(roomViewModel);
            }
        });

        //---------------------------------------------------------------
        //部屋ID情報
        TextView RoomIdText = findViewById(R.id.roomID);
        RoomIdText.setText(roomId);


        roomViewModel.start(500,(Iris)getApplication());

    }

    //---------------------------------------------------------------
    //roomIDのテキストをコピーする処理
    public void CopyClipBoard() {
        String copyText = roomId; // 生成されたroomIDを取得

        boolean result = SetClipData(copyText);
        if(result) {

        } else {

        }
    }

    private boolean SetClipData(String copyText) {
        try {
            //クリップボードに格納するItemを作成
            ClipData.Item item = new ClipData.Item(copyText);

            //MIMETYPEの作成
            String[] mimeType = new String[1];
            mimeType[0] = ClipDescription.MIMETYPE_TEXT_URILIST;

            //クリップボードに格納するClipDataオブジェクトの作成
            ClipData cd = new ClipData(new ClipDescription("text_data", mimeType), item);

            //クリップボードにデータを格納
            ClipboardManager cm = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
            cm.setPrimaryClip(cd);
            return true;
        }
        catch(Exception e) {
            return false;
        }
    }

    private void CreateCommonIntent() {
        //システムのクリップボードを取得
        ClipboardManager cm = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);

        //クリップボードからClipDataを取得
        ClipData cd = cm.getPrimaryClip();

        if(cd != null) {  // クリップボードにコピーがある場合
            ClipData.Item item = cd.getItemAt(0);
            Intent sendIntent =new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.setType("text/plain");
            sendIntent.putExtra(sendIntent.EXTRA_TEXT, item.getText());   // 他アプリのテキストをセット
            startActivity(sendIntent);                                    // 共有メニューを開く
        }
    }
    //---------------------------------------------------------------

    //---------------------------------------------------------------
    //ホームボタンを押したときの処理
    private void homeButton(RoomViewModel roomViewModel){
        String strTitle = "ホーム画面に戻ってよろしいですか?";
        String strMessage = "退出することになりますよ";

        AlertDialog.Builder builder;
        builder = new AlertDialog.Builder(this);
        builder.setMessage(strMessage);
        builder.setTitle(strTitle);
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                roomViewModel.deleteMember(roomId , myName);
                Intent intent = new Intent(MemberRoomActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });
        builder.setNegativeButton("キャンセル", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.create();
        builder.show();
    }

    //---------------------------------------------------------------
    //オーナーが部屋を退出し、強制的に部屋が削除される処理
    private void roomDeleted(RoomViewModel roomViewModel){
        String strTitle = "オーナーが退出しました";
        String strMessage = "強制的に部屋を削除します";

        AlertDialog.Builder builder;
        builder = new AlertDialog.Builder(this);
        builder.setMessage(strMessage);
        builder.setTitle(strTitle);

        builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                System.out.println("ホームへ戻される");
            }
        });
        builder.create();
        builder.show();
    }

    private int myNameIndex(List<Member> list, String name){
        for(int i = 0; i < list.size(); i++){
            if(list.get(i).getNickname().equals(name)) {
                return i;
            }
        }
        return 0;
    }

}