diff --git a/app/src/main/java/com/example/nemophila/MyPageEditorActivity.java b/app/src/main/java/com/example/nemophila/MyPageEditorActivity.java index a7527d2..a52e03b 100644 --- a/app/src/main/java/com/example/nemophila/MyPageEditorActivity.java +++ b/app/src/main/java/com/example/nemophila/MyPageEditorActivity.java @@ -37,13 +37,11 @@ private ActivityResultLauncher launcher = registerForActivityResult(new ActivityResultContracts.OpenDocument(), new ActivityResultCallback() { @Override public void onActivityResult(Uri uri) { - if(uri == null)return; icon = new String(Base64.getEncoder().encode(uri.toString().getBytes())); - ImageButton changeIconButton = (ImageButton)findViewById(R.id.changeIconButton); + ImageButton changeIconButton = findViewById(R.id.changeIconButton); changeIconButton.setImageURI(uri); } - }); @Override @@ -51,44 +49,17 @@ super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_page_editor); + // フィールド AccountViewModel accountViewModel = new ViewModelProvider(this).get(AccountViewModel.class); nemophila = (Nemophila) getApplication(); uid = nemophila.getUid(); token = nemophila.getToken(); //MyPageに戻るボタン - ImageButton backButton = (ImageButton) findViewById(R.id.backMyPage); + ImageButton backButton = findViewById(R.id.backMyPage); backButton.setOnClickListener(v -> finish()); - //LiveDataへの購読 - accountViewModel.getNameLiveData().observe(this, new Observer() { - @Override - public void onChanged(String name) { - TextView myName = (TextView) findViewById(R.id.editName); - myName.setText(name); - nemophila.setName(name); - } - }); - - //LiveDataへの購読 - accountViewModel.getPwLiveData().observe(this, new Observer() { - @Override - public void onChanged(String Pw) { - finish(); - } - - }); - //LiveDataへの購読 - accountViewModel.getPwErrorLiveData().observe(this, new Observer() { - @Override - public void onChanged(String name) { - Toast ts = Toast.makeText(MyPageEditorActivity.this, "パスワードが間違っています", Toast.LENGTH_SHORT); - ts.setGravity(Gravity.CENTER, 0, 0); - ts.show(); - } - - }); - ImageButton changeIconButton = (ImageButton)findViewById(R.id.changeIconButton); + ImageButton changeIconButton = findViewById(R.id.changeIconButton); changeIconButton.setOnClickListener(new View.OnClickListener(){ public void onClick(View v) { launcher.launch(new String[] {"image/*"}); @@ -96,26 +67,28 @@ }); //変更確定ボタンを押した場合の動き - Button changeNameButton = (Button) findViewById(R.id.changeConfirm); + Button changeNameButton = findViewById(R.id.changeConfirm); changeNameButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //名前とパスワードを取り込む - EditText editName = (EditText) findViewById(R.id.editName); + EditText editName = findViewById(R.id.editName); String newName = editName.getText().toString(); - EditText editOldPw = (EditText) findViewById(R.id.editOldPw); + EditText editOldPw = findViewById(R.id.editOldPw); String oldPw = editOldPw.getText().toString(); - EditText editNewPw = (EditText) findViewById(R.id.editNewPw); + EditText editNewPw = findViewById(R.id.editNewPw); String newPw = editNewPw.getText().toString(); - //Nemophilaから(id)(token)をgetしてaccountViewModelに送る + // 名前が入力されているとき通信を行う if(newName.length() > 0) { System.out.println(newName); accountViewModel.changeName(uid, newName, token); } + // パスワードが入力されているとき通信を行う if(oldPw.length() > 0 && newPw.length() > 0) { accountViewModel.changePw(uid, oldPw, newPw, token); } - if(icon!=null){ + // アイコンが入力されているとき通信を行う + if(icon != null){ accountViewModel.changeIcon(uid, icon, token); } @@ -123,7 +96,7 @@ }); // ログアウトボタンを押したときの処理 - Button logoutButton = (Button) findViewById(R.id.logoutButton); + Button logoutButton = findViewById(R.id.logoutButton); logoutButton.setOnClickListener(v -> { new AlertDialog.Builder(this) .setTitle("本当にログアウトしますか?") @@ -142,7 +115,7 @@ }); // 削除ボタンを押したときの処理 - Button deleteAccountButton = (Button) findViewById(R.id.deleteAccount); + Button deleteAccountButton = findViewById(R.id.deleteAccount); deleteAccountButton.setOnClickListener(v -> { new AlertDialog.Builder(this) .setTitle("本当に削除しますか?") @@ -150,14 +123,59 @@ @Override public void onClick(DialogInterface dialogInterface, int i) { // accountViewModel.deleteAccount(uid, token); -// Intent intent = new Intent(MyPageEditorActivity.this, SignUpActivity.class); -// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); -// startActivity(intent); } }) .setNeutralButton("キャンセル", null) .create() .show(); }); + + // ------------------------------ LiveDataへの購読 ------------------------------------------- // + // アカウントの名前を取得した際のLiveDataの購読 + accountViewModel.getNameLiveData().observe(this, new Observer() { + @Override + public void onChanged(String name) { + TextView myName = findViewById(R.id.editName); + myName.setText(name); + nemophila.setName(name); + } + }); + + // アカウントのパスワードを取得した際のLiveDataの購読 + accountViewModel.getPwLiveData().observe(this, new Observer() { + @Override + public void onChanged(String Pw) { + finish(); + } + }); + + // パスワードのエラーが発生した際のLiveDataの購読 + accountViewModel.getPwErrorLiveData().observe(this, new Observer() { + @Override + public void onChanged(String name) { + Toast ts = Toast.makeText(MyPageEditorActivity.this, "パスワードが間違っています", Toast.LENGTH_SHORT); + ts.setGravity(Gravity.CENTER, 0, 0); + ts.show(); + } + }); + + // アカウントの削除をした際のLiveDataの購読 + accountViewModel.getSuccessDeleteAccountLiveData().observe(this, new Observer() { + @Override + public void onChanged(Boolean isSuccess) { + Intent intent = new Intent(MyPageEditorActivity.this, SignUpActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);startActivity(intent); + } + }); + + // アカウントの削除をした際のエラーのLiveDataの購読 + accountViewModel.getDelAcErrorLiveData().observe(this, new Observer() { + @Override + public void onChanged(String s) { + Toast ts = Toast.makeText(MyPageEditorActivity.this, s, Toast.LENGTH_SHORT); + ts.setGravity(Gravity.CENTER, 0, 0); + ts.show(); + } + }); } } diff --git a/app/src/main/java/com/example/nemophila/viewmodels/AccountViewModel.java b/app/src/main/java/com/example/nemophila/viewmodels/AccountViewModel.java index a4f9f2a..5f3d05b 100644 --- a/app/src/main/java/com/example/nemophila/viewmodels/AccountViewModel.java +++ b/app/src/main/java/com/example/nemophila/viewmodels/AccountViewModel.java @@ -37,7 +37,9 @@ private final MutableLiveData pwErrorLiveData; private final MutableLiveData accountLiveData; private final MutableLiveData errorLiveData; - private final MutableLiveData finishChangeIconLiveData; + private final MutableLiveData successChangeIconLiveData; + private final MutableLiveData successDeleteAccountLiveData; + private final MutableLiveData delAcErrorLiveData; // コンストラクタ public AccountViewModel() { @@ -52,7 +54,9 @@ this.pwErrorLiveData = new MutableLiveData<>(); this.accountLiveData = new MutableLiveData<>(); this.errorLiveData = new MutableLiveData<>(); - this.finishChangeIconLiveData = new MutableLiveData<>(); + this.successChangeIconLiveData = new MutableLiveData<>(); + this.successDeleteAccountLiveData = new MutableLiveData<>(); + this.delAcErrorLiveData = new MutableLiveData<>(); } // ライブデータの取得(ゲッター) @@ -72,27 +76,9 @@ public MutableLiveData getErrorLiveData() { return errorLiveData; } - public MutableLiveData getFinishChangeIconLiveData() {return finishChangeIconLiveData;} - - // 対象のアカウント情報の削除 - public void deleteAccount(String uid, String token) { - Call call = accountsRest.deleteAccount(uid, token); - - call.enqueue(new Callback() { - @Override - public void onResponse(Call call, Response response) { - if (response.isSuccessful()) { - System.out.println("DeleteAccount Successful"); - } else { - System.out.println("DeleteAccount ResponseError"); - } - } - @Override - public void onFailure(Call call, Throwable t) { - System.out.println("DeleteAccount NetworkError" + t); - } - }); - } + public MutableLiveData getSuccessChangeIconLiveData() { return successChangeIconLiveData; } + public MutableLiveData getSuccessDeleteAccountLiveData() { return successDeleteAccountLiveData; } + public MutableLiveData getDelAcErrorLiveData() { return delAcErrorLiveData; } // 対象のアカウントパスワードの変更 public void changePw(String uid, String oldPw, String newPw, String token) { @@ -149,16 +135,16 @@ @Override public void onResponse(Call call, Response response) { if (response.isSuccessful()) { - finishChangeIconLiveData.setValue(true); + successChangeIconLiveData.setValue(true); } else { - finishChangeIconLiveData.setValue(false); + successChangeIconLiveData.setValue(false); System.out.println("response error"); } } @Override public void onFailure(Call call, Throwable t) { - finishChangeIconLiveData.setValue(false); + successChangeIconLiveData.setValue(false); System.out.println("ChangeIcon NetworkError :" + t); } }); @@ -275,6 +261,26 @@ accountPostsLiveData.setValue(preData); } + // 対象のアカウント情報の削除 + public void deleteAccount(String uid, String token) { + Call call = accountsRest.deleteAccount(uid, token); + + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if (response.isSuccessful()) { + successDeleteAccountLiveData.setValue(true); + } else { + delAcErrorLiveData.setValue(parseStatusCode(response.code())); + } + } + @Override + public void onFailure(Call call, Throwable t) { + delAcErrorLiveData.setValue(ErrorType.NetworkError.getText()); + } + }); + } + private String parseStatusCode(Integer stats) { switch (stats) { case 404: