Newer
Older
CosmosClient / app / src / main / java / com / example / cosmosclient / views / CreateGroupActivity.java
k-morimoto on 4 Jul 2019 5 KB ボタン連打防止処理を追加
package com.example.cosmosclient.views;

import android.media.Image;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.example.cosmosclient.R;
import com.example.cosmosclient.app.Cosmos;
import com.example.cosmosclient.entities.Group;
import com.example.cosmosclient.resources.GroupsRest;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

public class CreateGroupActivity extends AppCompatActivity {
    Button createGroupButton;

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

        //各種IDを取得
        final EditText groupNameText = findViewById(R.id.groupNameText);
        createGroupButton = findViewById(R.id.createGroupButton);
        final String uId,token;

        //ボタン有効化&監視
        createGroupButton.setEnabled(false);
        createGroupButton.setBackgroundColor(0xaa808080);
        groupNameText.addTextChangedListener(new CreateGroupActivity.GenericTextWatcher(groupNameText));

        //グループ作成に必要な情報の取得
        Cosmos app = (Cosmos) getApplication();
        uId = app.getuId();
        ////サインアップでトークンが返ってきてないため一時コメントアウト
        //token = app.getToken();
        token="abcdef";

        //retrofitの処理
        final Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/cosmos/")
                .addConverterFactory(JacksonConverterFactory.create())
                .build();
        //interfaceから実装を取得
        final GroupsRest createGroupService = retrofit.create(GroupsRest.class);

        //グループ作成ボタン処理
        createGroupButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                //ボタン連打防止
                createGroupButton.setEnabled(false);
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        createGroupButton.setEnabled(true);
                    }
                }, 1000L);

                try {
                    Call<Group> createGroup = createGroupService.createGroup(groupNameText.getText().toString(), uId, token);

                    createGroup.enqueue(new Callback<Group>() {
                        @Override
                        public void onResponse(Call<Group> call, Response<Group> response) {
                            if (response.isSuccessful()) {
                                //成功時
                                Group result = response.body();

//                                //確認用
//                                System.out.println(result.getgId());
//                                System.out.println(result.getName());
//                                System.out.println(result.getRequests());
//                                System.out.println(result.getUri());

                                Toast.makeText(CreateGroupActivity.this,
                                        "グループを作成しました", Toast.LENGTH_SHORT).show();
                                finish();
                            } else {
                                //onFailureでキャッチできない用のエラー
                                System.out.println("");
                                Toast.makeText(CreateGroupActivity.this,
                                        "通信エラー",Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<Group> call, Throwable t) {
                            //失敗時
                            t.printStackTrace();
                            Toast.makeText(CreateGroupActivity.this,
                                    "グループ作成失敗",Toast.LENGTH_SHORT);
                        }
                    });
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        });
    }
    //テキスト欄監視
    private class GenericTextWatcher implements TextWatcher {
        private View view;

        private GenericTextWatcher(View view){
            this.view = view;
        }

        @Override
        public  void beforeTextChanged(CharSequence s, int start, int count,int after){/*記述不要*/};
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count){/*記述不要*/};

        @Override
        public void afterTextChanged(Editable s){
            //ボタン有効&無効
            if(s.length()>0){
                createGroupButton.setEnabled(true);
                createGroupButton.setBackgroundColor(0xaa009688);
            }else{
                createGroupButton.setEnabled(false);
                createGroupButton.setBackgroundColor(0xaa808080);
            }
        }
    }
}