diff --git a/app/src/main/java/com/example/nemophila/MyPageEditorActivity.java b/app/src/main/java/com/example/nemophila/MyPageEditorActivity.java index a92b444..a7527d2 100644 --- a/app/src/main/java/com/example/nemophila/MyPageEditorActivity.java +++ b/app/src/main/java/com/example/nemophila/MyPageEditorActivity.java @@ -7,6 +7,8 @@ import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProvider; +import android.app.AlertDialog; +import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; @@ -27,8 +29,11 @@ public class MyPageEditorActivity extends AppCompatActivity { private String icon; - //マイページ編集画面の作成 + private Nemophila nemophila; + private String uid; + private String token; + //マイページ編集画面の作成 private ActivityResultLauncher launcher = registerForActivityResult(new ActivityResultContracts.OpenDocument(), new ActivityResultCallback() { @Override public void onActivityResult(Uri uri) { @@ -46,21 +51,15 @@ super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_page_editor); - //LiveDataへの購読 AccountViewModel accountViewModel = new ViewModelProvider(this).get(AccountViewModel.class); - Nemophila nemophila = (Nemophila) getApplication(); - String name = nemophila.getName(); - EditText myName = (EditText) findViewById(R.id.editName); - myName.setText(name); + nemophila = (Nemophila) getApplication(); + uid = nemophila.getUid(); + token = nemophila.getToken(); //MyPageに戻るボタン ImageButton backButton = (ImageButton) findViewById(R.id.backMyPage); - backButton.setOnClickListener(new View.OnClickListener() { - public void onClick(View v) { - Intent intent = new Intent(MyPageEditorActivity.this, MyPageActivity.class); - startActivity(intent); - } - }); + backButton.setOnClickListener(v -> finish()); + //LiveDataへの購読 accountViewModel.getNameLiveData().observe(this, new Observer() { @Override @@ -69,23 +68,13 @@ myName.setText(name); nemophila.setName(name); } - }); - 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) { - Intent intent = new Intent(MyPageEditorActivity.this, MyPageActivity.class); - startActivity(intent); + finish(); } }); @@ -119,13 +108,11 @@ String newPw = editNewPw.getText().toString(); //Nemophilaから(id)(token)をgetしてaccountViewModelに送る - String uid = nemophila.getUid(); - String token = nemophila.getToken(); - if(newName!=null) { + if(newName.length() > 0) { System.out.println(newName); accountViewModel.changeName(uid, newName, token); } - if(oldPw!=null && newPw!=null) { + if(oldPw.length() > 0 && newPw.length() > 0) { accountViewModel.changePw(uid, oldPw, newPw, token); } if(icon!=null){ @@ -135,16 +122,42 @@ } }); - //削除ボタンを押したときの処理 + // ログアウトボタンを押したときの処理 + Button logoutButton = (Button) findViewById(R.id.logoutButton); + logoutButton.setOnClickListener(v -> { + new AlertDialog.Builder(this) + .setTitle("本当にログアウトしますか?") + .setPositiveButton("OK", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + nemophila.logout(); + Intent intent = new Intent(MyPageEditorActivity.this, LoginActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); + startActivity(intent); + } + }) + .setNeutralButton("キャンセル", null) + .create() + .show(); + }); + + // 削除ボタンを押したときの処理 Button deleteAccountButton = (Button) findViewById(R.id.deleteAccount); - deleteAccountButton.setOnClickListener(new View.OnClickListener() { - public void onClick(View v) { - String uid = nemophila.getUid(); - String token = nemophila.getToken(); - accountViewModel.deleteAccount(uid,token); - Intent intent = new Intent(MyPageEditorActivity.this, SignUpActivity.class); - startActivity(intent); - } + deleteAccountButton.setOnClickListener(v -> { + new AlertDialog.Builder(this) + .setTitle("本当に削除しますか?") + .setPositiveButton("OK", new DialogInterface.OnClickListener() { + @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(); }); } }