Newer
Older
NemophilaClient / app / src / main / java / com / example / nemophila / MyPageEditorActivity.java
package com.example.nemophila;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
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.entities.Account;
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);
        Nemophila nemophila = (Nemophila) getApplication();
        String name = nemophila.getName();
        EditText myName = (EditText) findViewById(R.id.editName);
        myName.setText(name);

        //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);
            }
        });

        accountViewModel.getNameLiveData().observe(this, new Observer<String>() {
            @Override
            public void onChanged(String name) {
                TextView myName = (TextView) findViewById(R.id.editName);
                myName.setText(name);
                nemophila.setName(name);
                Intent intent = new Intent(MyPageEditorActivity.this, MyPageActivity.class);
                startActivity(intent);
            }

        });
        accountViewModel.getPwLiveData().observe(this, new Observer<String>() {
            @Override
            public void onChanged(String Pw) {
                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に送る
                String uid = nemophila.getUid();
                String token  = nemophila.getToken();
                if(newName!=null) {
                    System.out.println(newName);
                    accountViewModel.changeName(uid, newName, token);
                }
                if(oldPw!=null && newPw!=null) {
                    accountViewModel.changePw(uid, oldPw, newPw, token);
                }
            }
        });

        //削除ボタンを押したときの処理
        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();
                Intent intent = new Intent(MyPageEditorActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        });
    }
}