diff --git a/app/src/main/java/com/example/citrusclient/viewmodels/BooksViewModel.java b/app/src/main/java/com/example/citrusclient/viewmodels/BooksViewModel.java index e1e54fe..784795f 100644 --- a/app/src/main/java/com/example/citrusclient/viewmodels/BooksViewModel.java +++ b/app/src/main/java/com/example/citrusclient/viewmodels/BooksViewModel.java @@ -18,39 +18,39 @@ public class BooksViewModel extends ViewModel { private final Retrofit retrofit; - private final BooksRest booksRest; - private final MutableLiveData> booksLiveData; - + private final MutableLiveData> booksIdLiveData; private final MutableLiveData titleLiveData; - private final MutableLiveData colorLiveData; - private final MutableLiveData publicityLiveData; - + private final MutableLiveData favoritedLiveData; private final MutableLiveData errorLiveData; - private final MutableLiveData successDeleteBookLiveData; - private final MutableLiveData delBookErrorLivedata; - public BooksViewModel() { + public BooksViewModel(MutableLiveData> booksIdLiveData) { this.retrofit = new Retrofit.Builder() .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/citrus/") .addConverterFactory(JacksonConverterFactory.create()) .build(); this.booksRest = retrofit.create(BooksRest.class); this.booksLiveData = new MutableLiveData<>(); + this.booksIdLiveData = booksIdLiveData; this.titleLiveData = new MutableLiveData<>(); this.colorLiveData = new MutableLiveData<>(); this.publicityLiveData = new MutableLiveData<>(); + this.favoritedLiveData = new MutableLiveData<>(); this.errorLiveData = new MutableLiveData<>(); this.successDeleteBookLiveData = new MutableLiveData<>(); this.delBookErrorLivedata = new MutableLiveData<>(); } - public MutableLiveData> getBookLiveData() { return this.booksLiveData;} + public MutableLiveData> getBookLiveData() { return this.booksLiveData;}//本の一覧を返す + public MutableLiveData> getBooksIdLiveData() {return this.booksIdLiveData;}//本の情報を取得 + public MutableLiveData getTitleLiveData() {return this.titleLiveData;}//本のタイトルを返す + public MutableLiveData getPublicityLiveData() {return this.publicityLiveData;}//本の公開状態を返す + public MutableLiveData getSuccessDeleteBookLiveData() {return successDeleteBookLiveData;} @@ -83,7 +83,6 @@ @Override public void onResponse(Call> call, Response> response) { if (response.isSuccessful()) { - System.out.println(response.code()); HashMap book = response.body(); booksLiveData.setValue(book); System.out.println(response.code()); @@ -96,13 +95,6 @@ } }); } -// private void setBooksLiveData(HashMap bookall){ -// HashMap books = new HashMap<>(); -// for(int i = 0; bookall.get(i).keySet().equals(null); i++){ -// -// } -// booksLiveData.setValue(books); -// } public void deleteBook(String account_id, Integer book_id, String token){ Call call = booksRest.deleteBook(account_id, book_id, token); @@ -111,7 +103,7 @@ @Override public void onResponse(Call call, Response response) { if (response.isSuccessful()) { - deleteBookLiveData(account_id, book_id); + successDeleteBookLiveData.setValue(true); System.out.println("DELETE"); } else { System.out.println("response error"); @@ -125,10 +117,16 @@ }); } - private void deleteBookLiveData(String account_id, Integer book_id) { - ArrayList delData = new ArrayList<>(); - - } + /*private void deleteBookLiveData(String account_id, Integer book_id) { + HashMap delData = new HashMap<>(); + for (Book book: booksLiveData.getValue()) { + if (book.getBookId().equals(book_id)) { + continue; + } + delData.add(book); + } + booksLiveData.setValue(delData); + }*/ public void setTitle(String account_id, Integer book_id, String title, String token){ Call call = booksRest.putTitle(account_id, book_id, title, token); diff --git a/app/src/main/java/com/example/citrusclient/viewmodels/TodosViewModel.java b/app/src/main/java/com/example/citrusclient/viewmodels/TodosViewModel.java index 0d9935a..bfb88d3 100644 --- a/app/src/main/java/com/example/citrusclient/viewmodels/TodosViewModel.java +++ b/app/src/main/java/com/example/citrusclient/viewmodels/TodosViewModel.java @@ -23,9 +23,11 @@ //LiveData private final MutableLiveData>>>> allTodosLiveData; - private final MutableLiveData>> TodosByMonthLiveData; - private final MutableLiveData> TodosByDayLiveData; - private final MutableLiveData TodoByIdLiveData; + private final MutableLiveData>> todosByMonthLiveData; + private final MutableLiveData> todosByDayLiveData; + private final MutableLiveData todoLiveData; + private final MutableLiveData successSetCheckLiveData; + private final MutableLiveData successDeleteTodoLiveData; private final MutableLiveData errorLiveData; //Constructor @@ -37,9 +39,11 @@ .build(); this.todosRest = retrofit.create(TodosRest.class); this.allTodosLiveData = new MutableLiveData<>(); - this.TodosByMonthLiveData = new MutableLiveData<>(); - this.TodosByDayLiveData = new MutableLiveData<>(); - this.TodoByIdLiveData = new MutableLiveData<>(); + this.todosByMonthLiveData = new MutableLiveData<>(); + this.todosByDayLiveData = new MutableLiveData<>(); + this.todoLiveData = new MutableLiveData<>(); + this.successSetCheckLiveData = new MutableLiveData<>(); + this.successDeleteTodoLiveData = new MutableLiveData<>(); this.errorLiveData = new MutableLiveData<>(); } @@ -48,13 +52,16 @@ { return allTodosLiveData; } - public MutableLiveData>> getTodosByMonthLiveData() {return TodosByMonthLiveData;} - public MutableLiveData> getTodosByDayLiveData() {return TodosByDayLiveData;} - public MutableLiveData getTodoByIdLiveData() {return TodoByIdLiveData;} + public MutableLiveData>> getTodosByMonthLiveData() {return todosByMonthLiveData;} + public MutableLiveData> getTodosByDayLiveData() {return todosByDayLiveData;} + public MutableLiveData getTodoLiveData() {return todoLiveData;} + public MutableLiveData getSuccessSetCheckLiveData() {return successSetCheckLiveData;} + public MutableLiveData getSuccessDeleteTodoLiveData() {return successDeleteTodoLiveData;} public MutableLiveData getErrorLiveData() {return errorLiveData;} - public void getAllTodos(String accountId,Integer bookId, String token) + + public void loadAllTodos(String accountId,Integer bookId, String token) { Call>>>> call = todosRest.getAllTodos(accountId, bookId, token); call.enqueue(new Callback>>>>() { @@ -70,14 +77,14 @@ } @Override public void onFailure(Call>>>> call, Throwable t) { - System.out.println("CommunicationError: getAllTodos" + t); + System.out.println("NetworkError : getAllTodos" + t); errorLiveData.setValue(parseStatusCode(-1)); } }); } - public void getTodosByMonth(String accountId,Integer bookId, Integer year, Integer month, + public void loadTodosByMonth(String accountId,Integer bookId, Integer year, Integer month, String token) { Call>> call = todosRest.getTodosByMonth(accountId, bookId, year, month, token); @@ -86,21 +93,21 @@ public void onResponse(Call>> call, Response>> response) { if (response.isSuccessful()) { System.out.println("Success: getTodosByMonth"); - TodosByMonthLiveData.setValue(response.body()); + todosByMonthLiveData.setValue(response.body()); } else { System.out.println("Error: getTodosByMonth" + response.code()); } } @Override public void onFailure(Call>> call, Throwable t) { - System.out.println("CommunicationError: getTodosByMonth" + t); + System.out.println("NetworkError : getTodosByMonth" + t); errorLiveData.setValue(parseStatusCode(-1)); } }); } - public void getTodosByDay(String accountId,Integer bookId, Integer year, Integer month, + public void loadTodosByDay(String accountId,Integer bookId, Integer year, Integer month, Integer day, String token) { Call> call = todosRest.getTodosByDay(accountId, bookId, year, month, day, token); @@ -109,21 +116,21 @@ public void onResponse(Call> call, Response> response) { if (response.isSuccessful()) { System.out.println("Success: getTodosByDay"); - TodosByDayLiveData.setValue(response.body()); + todosByDayLiveData.setValue(response.body()); } else { System.out.println("Error: getTodosByDay" + response.code()); } } @Override public void onFailure(Call> call, Throwable t) { - System.out.println("CommunicationError: getTodosByDay" + t); + System.out.println("NetworkError : getTodosByDay" + t); errorLiveData.setValue(parseStatusCode(-1)); } }); } - public void getTodoById(String accountId,Integer bookId, Integer year, Integer month, + public void loadTodoById(String accountId,Integer bookId, Integer year, Integer month, Integer day, Integer todoId, String token) { Call call = todosRest.getTodoById(accountId, bookId, year, month, day, todoId, token); @@ -132,14 +139,14 @@ public void onResponse(Call call, Response response) { if (response.isSuccessful()) { System.out.println("Success: getTodoById"); - TodoByIdLiveData.setValue(response.body()); + todoLiveData.setValue(response.body()); } else { System.out.println("Error: getTodoById" + response.code()); } } @Override public void onFailure(Call call, Throwable t) { - System.out.println("CommunicationError: getTodoById" + t); + System.out.println("NetworkError : getTodoById" + t); errorLiveData.setValue(parseStatusCode(-1)); } @@ -155,14 +162,13 @@ public void onResponse(Call call, Response response) { if (response.isSuccessful()) { System.out.println("Success: createTodo"); - TodoByIdLiveData.setValue(response.body()); } else { System.out.println("Error: createTodo" + response.code()); } } @Override public void onFailure(Call call, Throwable t) { - System.out.println("CommunicationError: createTodo" + t); + System.out.println("NetworkError : createTodo" + t); errorLiveData.setValue(parseStatusCode(-1)); } @@ -178,14 +184,14 @@ public void onResponse(Call call, Response response) { if (response.isSuccessful()) { System.out.println("Success: setCheck"); - + successSetCheckLiveData.setValue(true); } else { System.out.println("Error: setCheck" + response.code()); } } @Override public void onFailure(Call call, Throwable t) { - System.out.println("CommunicationError: setCheck" + t); + System.out.println("NetworkError : setCheck" + t); errorLiveData.setValue(parseStatusCode(-1)); } }); @@ -200,14 +206,14 @@ public void onResponse(Call call, Response response) { if (response.isSuccessful()) { System.out.println("Success: deleteTodo"); - + successDeleteTodoLiveData.setValue(true); } else { System.out.println("Error: deleteTodo" + response.code()); } } @Override public void onFailure(Call call, Throwable t) { - System.out.println("CommunicationError: deleteTodo" + t); + System.out.println("NetworkError : deleteTodo" + t); errorLiveData.setValue(parseStatusCode(-1)); } }); diff --git a/app/src/main/java/com/example/citrusclient/views/CreateBookFragment.java b/app/src/main/java/com/example/citrusclient/views/CreateBookFragment.java index 9d11a24..1984939 100644 --- a/app/src/main/java/com/example/citrusclient/views/CreateBookFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/CreateBookFragment.java @@ -5,6 +5,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; @@ -13,6 +14,10 @@ import android.widget.Button; import com.example.citrusclient.R; +import android.graphics.Color;//ボタンの色変更 +import android.os.Bundle; +import android.widget.Button; + /** * A simple {@link Fragment} subclass. @@ -71,15 +76,26 @@ @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); + super.onViewCreated(view, savedInstanceState); //親クラスである Fragment の onViewCreated() メソッドを呼び出す - //View.findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener()) - Button saveButton = (Button)view.findViewById(R.id.save_button); -// saveButton.setOnClickListener(new View.OnClickListener(){ -// public void onClick(View v){ -// Intent intent = Intent(CreateBookFragment.this,MainActivity.class); -// startActivity(intent); -// } -// }) + //ここから書く + // MainActivityにこのフラグメントを設定するした二つのどっちか + //((MainActivity) getContext()).setCreateBookFragment(this);//フラグメントとアクティビティ間での情報のやり取りがあるとき + //((MainActivity) getActivity()).showFragment(new MyBookshelfFragment()); + + view.findViewById(R.id.save_button).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + ((MainActivity) getActivity()).showFragment(new MyBookshelfFragment()); + } + }); + + view.findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + ((MainActivity) getActivity()).showFragment(new MyBookshelfFragment()); + } + }); + } } \ No newline at end of file diff --git a/app/src/main/java/com/example/citrusclient/views/SignUpActivity.java b/app/src/main/java/com/example/citrusclient/views/SignUpActivity.java index 9ec5406..e092f40 100644 --- a/app/src/main/java/com/example/citrusclient/views/SignUpActivity.java +++ b/app/src/main/java/com/example/citrusclient/views/SignUpActivity.java @@ -1,19 +1,35 @@ package com.example.citrusclient.views; +import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.EditText; +import android.widget.TextView; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; +import retrofit2.converter.jackson.JacksonConverterFactory; +import com.example.citrusclient.Citrus; import com.example.citrusclient.R; +import com.example.citrusclient.rest.AccountsRest; + +import org.w3c.dom.Text; public class SignUpActivity extends AppCompatActivity { + private Retrofit retrofit; + private AccountsRest accountsRest; + private Citrus citrus; + + //画面起動時 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -28,6 +44,26 @@ }); + //citrusと連携 + citrus = (Citrus) this.getApplication(); + + //端末に情報が登録されているとき + if ((citrus.getToken() != null)&&(citrus.getAccountId() != null)){ + System.out.println("アカウント登録済み"); + //MainActivityに遷移 + Intent intent = new Intent(SignUpActivity.this,MainActivity.class); + startActivity(intent); + } + + //通信の初期化 + this.retrofit = new Retrofit.Builder() + .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/citrus/") + .addConverterFactory(JacksonConverterFactory.create()) + .build(); + this.accountsRest = retrofit.create(AccountsRest.class); + + + findViewById(R.id.button_signup).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -37,11 +73,49 @@ EditText editpw = (EditText) findViewById(R.id.put_pw); String pw = editpw.getText().toString().trim(); - if(!id.isEmpty() && !pw.isEmpty() && !id.trim().isEmpty() && !pw.trim().isEmpty() ){ - //通信 + //入力欄が空欄の時 + if(id.isEmpty() && pw.isEmpty() && id.trim().isEmpty() && pw.trim().isEmpty()){ + System.out.println("不適切入力"); + ((TextView)findViewById(R.id.textView_respons)).setText("適切に入力してください"); + + }else if(!id.isEmpty() && !pw.isEmpty() && !id.trim().isEmpty() && !pw.trim().isEmpty() ){ + //入力欄が全て適切に入力された場合の処理 + Call call = accountsRest.signup(id,pw); + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if (response.isSuccessful()){ + System.out.println("通信成功"); + //端末にtoken.id登録 + String token = response.body(); + citrus.setToken(token); + citrus.setAccountId(id); + + //画面遷移 + Intent intent = new Intent(SignUpActivity.this,MainActivity.class); + startActivity(intent); + + }else{ + //通信可能(ただしエラーが発生した場合) + //404がPW入力されてないときだけど、前で定義しているからいらないよね..? + System.out.println("通信可能"); + if(response.code()==409){ + ((TextView)findViewById(R.id.textView_respons)).setText(("入力したアカウントIDは既に使用されています")); + } + } + } + //通信失敗 + @Override + public void onFailure(Call call, Throwable t) { + System.out.println("通信失敗"); + System.out.println(t); + } + }); } } }); + + } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_sign_up.xml b/app/src/main/res/layout/activity_sign_up.xml index fecfc4b..a6ba2c0 100644 --- a/app/src/main/res/layout/activity_sign_up.xml +++ b/app/src/main/res/layout/activity_sign_up.xml @@ -83,7 +83,8 @@ android:id="@+id/textView_respons" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="error表示" + android:text="" + android:textColor="#B10922" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.501" diff --git a/app/src/main/res/layout/fragment_create_book.xml b/app/src/main/res/layout/fragment_create_book.xml index 2fccf53..b35d8d7 100644 --- a/app/src/main/res/layout/fragment_create_book.xml +++ b/app/src/main/res/layout/fragment_create_book.xml @@ -14,19 +14,26 @@ android:id="@+id/save_button" android:layout_width="wrap_content" android:layout_height="wrap_content" + android:background="#ECFDE3" android:text="保存" + android:textColor="#296DC1" + app:backgroundTint="#EDFDE4" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" - app:layout_constraintHorizontal_bias="0.90" + app:layout_constraintHorizontal_bias="0.9" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" - app:layout_constraintVertical_bias="0.10" /> + app:layout_constraintVertical_bias="0.099" />