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


import android.content.Intent;
import android.net.Uri;
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.ImageView;
import android.widget.Toast;

import com.example.cosmosclient.R;
import com.example.cosmosclient.app.Cosmos;
import com.example.cosmosclient.entities.SignupResponse;
import com.example.cosmosclient.resources.UsersRest;
import com.theartofdev.edmodo.cropper.CropImage;
import com.theartofdev.edmodo.cropper.CropImageView;


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

public class SignupActivity extends AppCompatActivity{
    private boolean nameEnable;
    private boolean pwEnable;
    private boolean cpwEnable;
    private Button MakeAccountButton;
    private ImageView iconChoice;

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

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

        //各種idを取得
        Button LoginButton = findViewById(R.id.LoginButton);
        MakeAccountButton = findViewById(R.id.MakeAccountButton);
        final EditText NameText = findViewById(R.id.NameText);
        final EditText PasswordText = findViewById(R.id.PasswordText);
        final EditText ConfirmPasswordText = findViewById(R.id.ConfirmPasswordText);
        iconChoice = findViewById(R.id.choiceImage);

//
//        //入力欄監視
//        NameText.addTextChangedListener(new GenericTextWatcher(NameText));
//        PasswordText.addTextChangedListener(new GenericTextWatcher(PasswordText));
//        ConfirmPasswordText.addTextChangedListener(new GenericTextWatcher(ConfirmPasswordText));
//
//        //アカウント登録ボタン無効化
//        MakeAccountButton.setEnabled(false);

        //Make Accountボタンの処理
        MakeAccountButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //ボタン連打防止
                MakeAccountButton.setEnabled(false);
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        MakeAccountButton.setEnabled(true);
                    }
                }, 1000L);

                //パスワード文字列比較のための変数宣言
                String pw = PasswordText.getText().toString();
                String confirmPw = ConfirmPasswordText.getText().toString();

                //パスワードと確認パスワードが一致していない際の処理
                if (!pw.equals(confirmPw)) {
                    PasswordText.setError("入力されたパスワードが一致しません");
                    ConfirmPasswordText.setError("入力されたパスワードが一致しません");
                    return;
                }

                //API呼び出しのための値入力
                Call<SignupResponse> call = signupService.createUser(NameText.getText().toString(),
                        PasswordText.getText().toString(), "https://yumenavi.info/live/2018/photo/k/s015980019.jpg");

                //サーバからデータ受け取り
                call.enqueue(new Callback<SignupResponse>() {
                    //成功時
                    @Override
                    public void onResponse(Call<SignupResponse> call, Response<SignupResponse> response) {
                        if (response.isSuccessful()) {
                            SignupResponse result = response.body();

                            //app/Cosmosに情報保存
                            Cosmos app = (Cosmos) getApplication();
                            app.setToken(result.token);
                            app.setuId(result.uId);

                            Intent intent = new Intent(getApplication(), GroupListActivity.class);

                            //test
//                            System.out.println(result.uId);
//                            System.out.println(result.name);
//                            System.out.println(result.uri);
//                            System.out.println(result.token);
//                            System.out.println(result.iconUri);
//                            System.out.println(result.pw);

                            startActivity(intent);
                            Toast.makeText(SignupActivity.this,
                                    "アカウントを作成しました", Toast.LENGTH_SHORT).show();
                            finish();

                        } else {
                            //onFailureでキャッチできないエラー用
                            Toast.makeText(SignupActivity.this,
                                    "通信エラー", Toast.LENGTH_SHORT).show();
                        }
                    }

                    //失敗時
                    @Override
                    public void onFailure(Call<SignupResponse> call, Throwable t) {
                        t.printStackTrace();
                        Toast.makeText(SignupActivity.this,
                                "アカウント作成失敗", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

        //サインイン画面への遷移処理
        LoginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplication(), SigninActivity.class);
                startActivity(intent);
                finish();
            }
        });

        //アイコン選択
        iconChoice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CropImage.activity()
                        .setAspectRatio(1,1)
                        .setGuidelines(CropImageView.Guidelines.ON)
                        .start(SignupActivity.this);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //トリミング後の処理
        if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
            CropImage.ActivityResult result = CropImage.getActivityResult(data);
            if (resultCode == RESULT_OK) {
                Uri resultUri = result.getUri();
                iconChoice.setImageURI(resultUri);
            } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                Exception error = result.getError();
            }
        }
    }

    //入力欄監視処理内容
    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){
            switch(view.getId()) {
                case R.id.NameText:
                    if (s.length() > 0) {
                        nameEnable = true;
                    } else {
                        nameEnable = false;
                    }
                    break;
                case R.id.PasswordText:
                    if(s.length()>0){
                        pwEnable=true;
                    }else{
                        pwEnable=false;
                    }
                    break;
                case R.id.ConfirmPasswordText:
                    if(s.length()>0){
                        cpwEnable=true;
                    }else{
                        cpwEnable=false;
                    }
                    break;
            }
            //ボタン有効&無効
            if(nameEnable && pwEnable && cpwEnable){
                MakeAccountButton.setEnabled(true);
            }else{
                MakeAccountButton.setEnabled(false);
            }
        }
    }

}