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

import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
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;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.nemophila.entities.Account;
import com.example.nemophila.viewmodels.AccountViewModel;

import java.util.Base64;
import java.util.List;

public class MyPageEditorActivity extends AppCompatActivity {
    private String icon;
    private Nemophila nemophila;
    private String uid;
    private String token;

    //マイページ編集画面の作成
    private ActivityResultLauncher<String[]> launcher = registerForActivityResult(new ActivityResultContracts.OpenDocument(), new ActivityResultCallback<Uri>() {
        @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);
            changeIconButton.setImageURI(uri);
        }

    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        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);
        backButton.setOnClickListener(v -> finish());

        //LiveDataへの購読
        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);
            }
        });

        //LiveDataへの購読
        accountViewModel.getPwLiveData().observe(this, new Observer<String>() {
            @Override
            public void onChanged(String Pw) {
                finish();
            }

        });
        //LiveDataへの購読
        accountViewModel.getPwErrorLiveData().observe(this, new Observer<String>() {
            @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);
        changeIconButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                launcher.launch(new String[] {"image/*"});
            }
        });

        //変更確定ボタンを押した場合の動き
        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に送る
                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){
                    accountViewModel.changeIcon(uid, icon, token);
                }

            }
        });

        // ログアウトボタンを押したときの処理
        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(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();
        });
    }
}