diff --git a/app/src/main/java/org/ntlab/acanthus_client/Acanthus.java b/app/src/main/java/org/ntlab/acanthus_client/Acanthus.java index c4c6944..a570877 100644 --- a/app/src/main/java/org/ntlab/acanthus_client/Acanthus.java +++ b/app/src/main/java/org/ntlab/acanthus_client/Acanthus.java @@ -1,9 +1,82 @@ package org.ntlab.acanthus_client; import android.app.Application; +import android.content.SharedPreferences; +import org.ntlab.acanthus_client.entities.Account; + +//----------------------------------------------------------------- +// ユーザーアカウント public class Acanthus extends Application { - private String password; + private Integer uid; + private String name; private String email; + private String password; + private String token; + //----------------------------------------------------------------- + // setter + public String getName() { + if(name == null){ + SharedPreferences preferences = getSharedPreferences("prefData",MODE_PRIVATE); + name = preferences.getString("name", ""); + } + return name; + } + + public String getEmail() { + if(email == null){ + SharedPreferences preferences = getSharedPreferences("prefData",MODE_PRIVATE); + email = preferences.getString("name", ""); + } + return email; + } + + public String getPassword() { + if(password == null){ + SharedPreferences preferences = getSharedPreferences("prefData",MODE_PRIVATE); + password = preferences.getString("name", ""); + } + return password; + } + + public String getToken() { + if(token == null){ + SharedPreferences preferences = getSharedPreferences("prefData",MODE_PRIVATE); + token = preferences.getString("name", ""); + } + return token; + } + + //----------------------------------------------------------------- + // setter + public void setName(String name){ + SharedPreferences preferences = getSharedPreferences("prefData", MODE_PRIVATE); + SharedPreferences.Editor editor = preferences.edit(); + editor.putString("name", this.name); + editor.commit(); + } + + public void setEmail(String email) { + SharedPreferences preferences = getSharedPreferences("prefData", MODE_PRIVATE); + SharedPreferences.Editor editor = preferences.edit(); + editor.putString("email", this.email); + editor.commit(); + } + + public void setPassword(String password){ + SharedPreferences preferences = getSharedPreferences("prefData", MODE_PRIVATE); + SharedPreferences.Editor editor = preferences.edit(); + editor.putString("password", this.password); + editor.commit(); + } + + public void setToken(String token){ + SharedPreferences preferences = getSharedPreferences("prefData", MODE_PRIVATE); + SharedPreferences.Editor editor = preferences.edit(); + editor.putString("token", this.token); + editor.commit(); + } + + //----------------------------------------------------------------- } diff --git a/app/src/main/java/org/ntlab/acanthus_client/entities/Account.java b/app/src/main/java/org/ntlab/acanthus_client/entities/Account.java index 7bec5e5..944e309 100644 --- a/app/src/main/java/org/ntlab/acanthus_client/entities/Account.java +++ b/app/src/main/java/org/ntlab/acanthus_client/entities/Account.java @@ -1,12 +1,17 @@ package org.ntlab.acanthus_client.entities; +import android.os.Build; +import android.preference.PreferenceDataStore; + +import androidx.annotation.RequiresApi; + import java.time.LocalDateTime; import java.util.HashMap; import java.util.UUID; //----------------------------------------------------------------- // ユーザーアカウント -public class Account { +public class Account{ private Integer uid; private String name; private String email; diff --git a/app/src/main/java/org/ntlab/acanthus_client/views/Login/LoginScreenActivity.java b/app/src/main/java/org/ntlab/acanthus_client/views/Login/LoginScreenActivity.java index 6154b1d..e2407e4 100644 --- a/app/src/main/java/org/ntlab/acanthus_client/views/Login/LoginScreenActivity.java +++ b/app/src/main/java/org/ntlab/acanthus_client/views/Login/LoginScreenActivity.java @@ -1,14 +1,20 @@ package org.ntlab.acanthus_client.views.Login; import androidx.appcompat.app.AppCompatActivity; +import androidx.lifecycle.Observer; +import androidx.lifecycle.ViewModelProvider; +import androidx.navigation.ActivityNavigator; 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 org.ntlab.acanthus_client.Acanthus; import org.ntlab.acanthus_client.R; +import org.ntlab.acanthus_client.databinding.ActivityLoginScreenBinding; import org.ntlab.acanthus_client.entities.AccountTokenJson; import org.ntlab.acanthus_client.resources.accounts.AccountsRest; import org.ntlab.acanthus_client.views.MainActivity; @@ -28,93 +34,56 @@ public class LoginScreenActivity extends AppCompatActivity { //----------------------------------------------------------------- - // 各Viewの列挙型 - public enum EditTexts { - emailForm(0), passwordForm(1), loginButton(2), redoSignUpButton(3); + private LoginScreenViewModel loginScreenViewModel; + private ActivityLoginScreenBinding binding; - private int textId; - //----------------------------------------------------------------- - private EditTexts(int textId) { - this.textId = textId; - } - - //----------------------------------------------------------------- - // getter - public int getTextId() { - return this.textId; - } + //----------------------------------------------------------------- + // ログインボタン押下時の処理 + public void onClickLogin(View view) { + Acanthus acanthus = (Acanthus) getApplication(); + loginScreenViewModel.issueLoginToken( + acanthus, binding.editTextTextLoginEmail, binding.editTextTextLoginPassword); } //----------------------------------------------------------------- - private ArrayList views = new ArrayList<>(); + // サインアップを押下 + public void onClickSignUp(View view) { + transitionActivity(new SignUpActivity()); + } //----------------------------------------------------------------- + //----------------------------------------------------------------- // @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + init(); + } + + //----------------------------------------------------------------- + //----------------------------------------------------------------- + // 初期化 + private void init() { setContentView(R.layout.activity_login_screen); - initView(); + binding = ActivityLoginScreenBinding.inflate(getLayoutInflater()); - Retrofit retrofit = new Retrofit.Builder() - .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/acanthus/") - .addConverterFactory(JacksonConverterFactory.create()) - .build(); - final AccountsRest accountsRest = retrofit.create(AccountsRest.class); - - onClickLogin(accountsRest); - onClickSignUp(); - + startObserve(); } //----------------------------------------------------------------- - // ログインボタン押下時の処理 - private void onClickLogin(AccountsRest accountsRest) { - Button loginButton = (Button) views.get(EditTexts.loginButton.getTextId()); - - loginButton.setOnClickListener(v -> { - String emailAddress = views.get(EditTexts.emailForm.getTextId()).getText().toString(); - String password = views.get(EditTexts.passwordForm.getTextId()).getText().toString(); - - // ログイン - Call call = accountsRest.issueLoginToken(emailAddress, password); - call.enqueue(new Callback() { - @Override - public void onResponse(Call call, Response response) { - if (response.isSuccessful()) succeedLogin(); - else loginButton.setText("Faild"); - } - - @Override - public void onFailure(Call call, Throwable t) { - loginButton.setText("failure"); - } - }); + // VMの監視開始 + private void startObserve() { + loginScreenViewModel = new ViewModelProvider(this).get(LoginScreenViewModel.class); + loginScreenViewModel.getAcanthus().observe(this, new Observer() { + @Override + public void onChanged(Acanthus acanthus) { + succeedLogin(); + } }); } //----------------------------------------------------------------- - // サインアップ画面に遷移 - private void onClickSignUp() { - Button signUpButton = (Button) views.get(EditTexts.redoSignUpButton.getTextId()); - - signUpButton.setOnClickListener(v -> { - transitionActivity(new SignUpActivity()); - }); - } - - //----------------------------------------------------------------- - //----------------------------------------------------------------- - // - private void initView() { - views.add(findViewById(R.id.editTextTextLoginEmail)); - views.add(findViewById(R.id.editTextTextLoginPassword)); - views.add(findViewById(R.id.buttonLoginLogin)); - views.add(findViewById(R.id.buttonRedoSignUp)); - } - - //----------------------------------------------------------------- // ログイン成功時の処理 private void succeedLogin() { transitionActivity(new MainActivity()); diff --git a/app/src/main/java/org/ntlab/acanthus_client/views/Login/LoginScreenViewModel.java b/app/src/main/java/org/ntlab/acanthus_client/views/Login/LoginScreenViewModel.java new file mode 100644 index 0000000..31885b6 --- /dev/null +++ b/app/src/main/java/org/ntlab/acanthus_client/views/Login/LoginScreenViewModel.java @@ -0,0 +1,77 @@ +package org.ntlab.acanthus_client.views.Login; + +import android.accounts.AccountAuthenticatorActivity; +import android.widget.EditText; + +import androidx.lifecycle.LiveData; +import androidx.lifecycle.MutableLiveData; +import androidx.lifecycle.ViewModel; + +import org.ntlab.acanthus_client.Acanthus; +import org.ntlab.acanthus_client.entities.AccountTokenJson; +import org.ntlab.acanthus_client.resources.accounts.AccountsRest; +import org.ntlab.acanthus_client.views.MainActivity; + +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; +import retrofit2.converter.jackson.JacksonConverterFactory; + +//----------------------------------------------------------------- +// +public class LoginScreenViewModel extends ViewModel { + + private MutableLiveData acanthusMutableLiveData; + + //----------------------------------------------------------------- + //----------------------------------------------------------------- + // + public LoginScreenViewModel() { + this.acanthusMutableLiveData = new MutableLiveData<>(); + } + + //----------------------------------------------------------------- + // getter + public LiveData getAcanthus() { + return this.acanthusMutableLiveData; + } + + //----------------------------------------------------------------- + //----------------------------------------------------------------- + // トークンの発行APIの呼び出し + public void issueLoginToken(Acanthus acanthus, EditText emailForm, EditText passwordForm) { + Retrofit retrofit = new Retrofit.Builder() + .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/acanthus/") + .addConverterFactory(JacksonConverterFactory.create()) + .build(); + final AccountsRest accountsRest = retrofit.create(AccountsRest.class); + + String emailAddress = emailForm.getText().toString(); + String password = passwordForm.getText().toString(); + + // ログイン + Call call = accountsRest.issueLoginToken(emailAddress, password); + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if (response.isSuccessful()) + setVariableFromResponse(acanthus, response.body().getToken()); + } + + @Override + public void onFailure(Call call, Throwable t) { + } + }); + } + + //----------------------------------------------------------------- + //----------------------------------------------------------------- + // ログイン成功時に値の変更を行う + private void setVariableFromResponse(Acanthus acanthus, String token) { + acanthus.setToken(token); + acanthusMutableLiveData.setValue(acanthus); + } + //----------------------------------------------------------------- + +} diff --git a/app/src/main/res/layout/activity_login_screen.xml b/app/src/main/res/layout/activity_login_screen.xml index 19edf2b..d7d7375 100644 --- a/app/src/main/res/layout/activity_login_screen.xml +++ b/app/src/main/res/layout/activity_login_screen.xml @@ -22,6 +22,7 @@ android:layout_height="wrap_content" android:layout_marginStart="240dp" android:layout_marginTop="400dp" + android:onClick="onClickLogin" android:text="ログイン" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" @@ -72,6 +73,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="60dp" + android:onClick="onClickSignUp" android:text="新規登録する" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent"