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 a13a392..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,6 +1,9 @@ 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; @@ -9,6 +12,7 @@ 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; @@ -30,40 +34,16 @@ public class LoginScreenActivity extends AppCompatActivity { //----------------------------------------------------------------- - ActivityLoginScreenBinding binding; + private LoginScreenViewModel loginScreenViewModel; + private ActivityLoginScreenBinding binding; - //----------------------------------------------------------------- - // - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - init(); - } //----------------------------------------------------------------- // ログインボタン押下時の処理 public void onClickLogin(View view) { - 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 = binding.editTextTextLoginEmail.getText().toString(); - String password = binding.editTextTextLoginPassword.getText().toString(); - - // ログイン - Call call = accountsRest.issueLoginToken(emailAddress, password); - call.enqueue(new Callback() { - @Override - public void onResponse(Call call, Response response) { - if (response.isSuccessful()) succeedLogin(); - } - - @Override - public void onFailure(Call call, Throwable t) { - } - }); + Acanthus acanthus = (Acanthus) getApplication(); + loginScreenViewModel.issueLoginToken( + acanthus, binding.editTextTextLoginEmail, binding.editTextTextLoginPassword); } //----------------------------------------------------------------- @@ -75,10 +55,34 @@ //----------------------------------------------------------------- //----------------------------------------------------------------- // + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + init(); + } + + //----------------------------------------------------------------- + //----------------------------------------------------------------- + // 初期化 private void init() { setContentView(R.layout.activity_login_screen); binding = ActivityLoginScreenBinding.inflate(getLayoutInflater()); + + startObserve(); } + + //----------------------------------------------------------------- + // 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 succeedLogin() { 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 index bb632ad..31885b6 100644 --- 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 @@ -1,13 +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 void issueLoginToken(){ - + 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); + } + //----------------------------------------------------------------- + }