Newer
Older
NemophilaClient / app / src / main / java / com / example / nemophila / MyPageEditorActivity.java
k-suzuki on 20 Jun 2023 3 KB MyPageActivityを作った
package com.example.nemophila;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.nemophila.viewmodels.AccountViewModel;

public class MyPageEditorActivity extends AppCompatActivity {
//マイページ編集画面の作成
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_page_editor);
//AccountViewModelとの通信
        AccountViewModel accountViewModel = new ViewModelProvider(this).get(AccountViewModel.class);
        EditText name = (EditText) findViewById(R.id.editName);
//MyPageに戻るボタン
        Button backButton = (Button) findViewById(R.id.backMyPage);
        backButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(MyPageEditorActivity.this, MyPageActivity.class);
                startActivity(intent);

            }
        });
//変更確定ボタンを押した場合の動き
        Button changeNameButton = (Button) findViewById(R.id.changeConfirm);
        changeNameButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
//名前とパスワードを取り込む
                EditText editName = (EditText) findViewById(R.id.editName);
                String newName = editName.getText().toString();
                EditText editOldPw = (EditText) findViewById(R.id.editOldPw);
                String oldPw = editOldPw.getText().toString();
                EditText editNewPw = (EditText) findViewById(R.id.editNewPw);
                String newPw = editNewPw.getText().toString();
//Nemophilaから(id)(token)をgetしてaccountViewModelに送る
                Nemophila nemophila = (Nemophila) getApplication();
                String uid = nemophila.getUid();
                String token  = nemophila.getToken();
                if(newName!=null) {
                    accountViewModel.changeName(uid, newName, token);
                }
                if(oldPw!=null && newPw!=null) {
                    accountViewModel.changePw(uid, oldPw, newPw, token);
                }
//変更確定ボタンを押した場合もMyPageに戻る
                Intent intent = new Intent(MyPageEditorActivity.this, MyPageActivity.class);
                startActivity(intent);
            }
        });
//        削除ボタンを押したときの処理
        Button deleteAccountButton = (Button) findViewById(R.id.deleteAccount);
        deleteAccountButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Nemophila nemophila = (Nemophila) getApplication();
                String uid = nemophila.getUid();
                String token = nemophila.getToken();
                Intent intent = new Intent(MyPageEditorActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        });
    }
}