Newer
Older
CosmosClient / app / src / main / java / com / example / cosmosclient / views / SignupActivity.java
k-morimoto on 13 Jun 2019 6 KB resourcesファイル作成
package com.example.cosmosclient.views;

import android.content.Intent;
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.Toast;

import com.example.cosmosclient.R;
import com.example.cosmosclient.resources.UsersRest;


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;

    @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);

        //入力欄監視
        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){
                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();
                            Intent intent = new Intent(getApplication(), GroupList.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);
                            finish();

                        }else{
                            //onFailureでキャッチできないエラー用
                            Toast.makeText(SignupActivity.this,
                                    "アカウント作成失敗",Toast.LENGTH_SHORT);
                        }
                    }

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

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

    //入力欄監視処理内容
    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);
            }
        }
    }

}