From 634547622a36a571ac2ff25f53d78f601519b4e1 Mon Sep 17 00:00:00 2001 From: nemukemo Date: Tue, 9 Jul 2024 19:49:58 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=E3=81=82=E3=81=A8=E3=81=A7=E3=82=8D?= =?UTF-8?q?=E3=82=8B=E3=81=B0=E3=81=99=E3=82=8A=E3=82=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/oplogy/FirestoreReception.java | 7 ++- .../com/example/oplogy/SetUpActivity.java | 53 ++++++++----------- 2 files changed, 26 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/com/example/oplogy/FirestoreReception.java b/app/src/main/java/com/example/oplogy/FirestoreReception.java index 612f6c6..03bd633 100644 --- a/app/src/main/java/com/example/oplogy/FirestoreReception.java +++ b/app/src/main/java/com/example/oplogy/FirestoreReception.java @@ -16,19 +16,18 @@ import java.util.Map; public class FirestoreReception { + //firestoreから受け取ったデータを束ねるためのマップ + public List myDataList = new ArrayList<>(); private FirebaseFirestore db; public FirestoreReception() { db = FirebaseFirestore.getInstance(); } - //firestoreから受け取ったデータを束ねるためのマップ - public List myDataList = new ArrayList<>(); - //ClassIdを引数にデータの作成を行う public void getDocumentsByClassId(int classId) { myDataList.clear(); - CollectionReference collectionRef = db.collection("QuestionnaireForms"); + CollectionReference collectionRef = db.collection("testTimeStamp"); // classIdが引数のものを取得する collectionRef.whereEqualTo("classId", classId).get() diff --git a/app/src/main/java/com/example/oplogy/SetUpActivity.java b/app/src/main/java/com/example/oplogy/SetUpActivity.java index 4144f93..ac275a0 100644 --- a/app/src/main/java/com/example/oplogy/SetUpActivity.java +++ b/app/src/main/java/com/example/oplogy/SetUpActivity.java @@ -2,7 +2,6 @@ package com.example.oplogy; import static android.content.ContentValues.TAG; - import android.annotation.SuppressLint; import android.app.DatePickerDialog; import android.app.TimePickerDialog; @@ -42,6 +41,16 @@ public class SetUpActivity extends FragmentActivity String startBreakTimeString; String endBreakTimeString; int totalStudentString; + String stringYearString; + String stringMonthString; + String stringDayOfMonthString; + String stringHourOfDayString; + String stringMinuteString; + Button setFirstDay; + Button setSecondDay; + Button setThirdDay; + Button setStartTimeButton; + Button setEndTimeButton; private TextView setTeacherName; private TextView setStartPoint; private TextView setStartTime; @@ -52,29 +61,13 @@ public class SetUpActivity extends FragmentActivity private int isDateSelectedInt; private int isStartTimeSelectedInt; - String stringYearString; - String stringMonthString; - String stringDayOfMonthString; - - - String stringHourOfDayString; - String stringMinuteString; - - Button setFirstDay; - Button setSecondDay; - Button setThirdDay; - Button setStartTimeButton; - Button setEndTimeButton; - - - @SuppressLint("MissingInflatedId") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_set_up); - int classIdInt= getIntent().getIntExtra("classId", 100000); + int classIdInt = getIntent().getIntExtra("classId", 100000); setTeacherName = findViewById(R.id.teacherName); //先生の名前 setStartPoint = findViewById(R.id.startPoint); //開始地点 @@ -104,7 +97,7 @@ public class SetUpActivity extends FragmentActivity Button reset = findViewById(R.id.resetButton); toMain.setOnClickListener(view -> { - Intent intent = new Intent(SetUpActivity.this,MainActivity.class); //main画面へ戻る処理 + Intent intent = new Intent(SetUpActivity.this, MainActivity.class); //main画面へ戻る処理 startActivity(intent); }); @@ -119,7 +112,7 @@ public class SetUpActivity extends FragmentActivity Log.d(TAG, "Third Day:" + thirdDayString); Log.d(TAG, "Start Time" + startTimeString); Log.d(TAG, "End Time" + endTimeString); - if (setTenMinute.isChecked()){ //ラジオボタンの状態を取得 + if (setTenMinute.isChecked()) { //ラジオボタンの状態を取得 intervalTimeString = "10"; } else if (setFifteenMinute.isChecked()) { intervalTimeString = "15"; @@ -172,12 +165,12 @@ public class SetUpActivity extends FragmentActivity runOnUiThread(() -> Toast.makeText(SetUpActivity.this, "登録しました", Toast.LENGTH_SHORT).show()); } //家庭訪問日を保存する共有プリファレンス - SharedPreferences sharedPreferences=getSharedPreferences("visitingDate",MODE_PRIVATE); - SharedPreferences.Editor editor= sharedPreferences.edit(); + SharedPreferences sharedPreferences = getSharedPreferences("visitingDate", MODE_PRIVATE); + SharedPreferences.Editor editor = sharedPreferences.edit(); - editor.putString("day1",firstDayString); - editor.putString("day2",secondDayString); - editor.putString("day3",thirdDayString); + editor.putString("day1", firstDayString); + editor.putString("day2", secondDayString); + editor.putString("day3", thirdDayString); editor.apply(); @@ -186,17 +179,17 @@ public class SetUpActivity extends FragmentActivity }); - setFirstDay.setOnClickListener(v ->{ + setFirstDay.setOnClickListener(v -> { isDateSelectedInt = 1; showDatePickerDialog(); //DatePickerの表示 }); - setSecondDay.setOnClickListener(v ->{ + setSecondDay.setOnClickListener(v -> { isDateSelectedInt = 2; showDatePickerDialog(); }); - setThirdDay.setOnClickListener(v ->{ + setThirdDay.setOnClickListener(v -> { isDateSelectedInt = 3; showDatePickerDialog(); }); @@ -245,7 +238,7 @@ public class SetUpActivity extends FragmentActivity @Override public void onDateSet(DatePicker datePicker, int year, int month, int dayOfMonth) { //Dateを成形する // DatePickerDialogで選択された日付を処理する - String str = String.format(Locale.JAPAN, "%02d/%02d", month + 1, dayOfMonth); // TextViewに表示する日付の形式を設定 + String str = String.format(Locale.JAPAN, "%02d/%02d", month + 1, dayOfMonth); // TextViewに表示する日付の形式を設定 if (isDateSelectedInt == 1) { stringYearString = String.valueOf(year); //年 @@ -291,7 +284,7 @@ public class SetUpActivity extends FragmentActivity } else if (isStartTimeSelectedInt == 3) { stringHourOfDayString = String.format("%02d", hourOfDay); stringMinuteString = String.format("%02d", minute); - startBreakTimeString =stringHourOfDayString + stringMinuteString; + startBreakTimeString = stringHourOfDayString + stringMinuteString; setStartBreakTime.setText(" " + str + " "); } else if (isStartTimeSelectedInt == 4) { From 356021beae33019980e41e54b0829a5146348838 Mon Sep 17 00:00:00 2001 From: nemukemo Date: Wed, 10 Jul 2024 16:07:02 +0900 Subject: [PATCH 2/2] =?UTF-8?q?ID,=E3=82=BB=E3=83=83=E3=83=88=E3=82=A2?= =?UTF-8?q?=E3=83=83=E3=83=97=E3=81=8C=E7=B5=82=E3=82=8F=E3=81=A3=E3=81=A6?= =?UTF-8?q?=E3=81=84=E3=81=AA=E3=81=84=E5=A0=B4=E5=90=88=E3=81=ABtoast?= =?UTF-8?q?=E8=A1=A8=E7=A4=BA=E3=82=92=E8=A1=8C=E3=81=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/example/oplogy/CreateUUID.java | 12 +- .../example/oplogy/FirestoreReception.java | 7 +- .../java/com/example/oplogy/MainActivity.java | 140 ++++++++++++++---- 3 files changed, 123 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/com/example/oplogy/CreateUUID.java b/app/src/main/java/com/example/oplogy/CreateUUID.java index 84904b0..08686e6 100644 --- a/app/src/main/java/com/example/oplogy/CreateUUID.java +++ b/app/src/main/java/com/example/oplogy/CreateUUID.java @@ -5,12 +5,12 @@ import java.util.List; public class CreateUUID { - public static int generateUUID(List classIdList ){ - while (true){ - int uuidInt = (int)(Math.random() * 1000000); + public static int generateUUID(List classIdList) { + while (true) { + int uuidInt = (int) (Math.random() * 1000000); boolean isDuplicate = false; - for(int classIdInt : classIdList){ - if(classIdInt==uuidInt){ + for (int classIdInt : classIdList) { + if (classIdInt == uuidInt) { //重複があればフラグを立て、ループを抜ける isDuplicate = true; break; @@ -22,7 +22,7 @@ public class CreateUUID { InsertClassIdforFirebase insertClassIdforFirebase = new InsertClassIdforFirebase(); insertClassIdforFirebase.insertClassId(uuidInt); //テスト用 - uuidInt=100; + uuidInt = 100; return uuidInt; } } diff --git a/app/src/main/java/com/example/oplogy/FirestoreReception.java b/app/src/main/java/com/example/oplogy/FirestoreReception.java index aa0cf68..0d69f20 100644 --- a/app/src/main/java/com/example/oplogy/FirestoreReception.java +++ b/app/src/main/java/com/example/oplogy/FirestoreReception.java @@ -16,19 +16,18 @@ import java.util.Map; public class FirestoreReception { + //firestoreから受け取ったデータを束ねるためのマップ + public List myDataList = new ArrayList<>(); private FirebaseFirestore db; public FirestoreReception() { db = FirebaseFirestore.getInstance(); } - //firestoreから受け取ったデータを束ねるためのマップ - public List myDataList = new ArrayList<>(); - //ClassIdを引数にデータの作成を行う public void getDocumentsByClassId(int classId) { myDataList.clear(); - CollectionReference collectionRef = db.collection("testAddressArray"); + CollectionReference collectionRef = db.collection("testDistinct"); // classIdが引数のものを取得する collectionRef.whereEqualTo("classId", classId).get() diff --git a/app/src/main/java/com/example/oplogy/MainActivity.java b/app/src/main/java/com/example/oplogy/MainActivity.java index f32da49..fa0f5d6 100644 --- a/app/src/main/java/com/example/oplogy/MainActivity.java +++ b/app/src/main/java/com/example/oplogy/MainActivity.java @@ -19,6 +19,7 @@ import com.google.gson.Gson; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -85,7 +86,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe // firestoreの受信関連 db = FirebaseFirestore.getInstance(); firestoreReception = new FirestoreReception(); - Log.d("MainActivity","geocodeAddress"); + Log.d("MainActivity", "geocodeAddress"); ExecutorService executor = Executors.newSingleThreadExecutor(); @@ -105,7 +106,6 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe } - // クリック処理 @Override public void onClick(View view) { @@ -136,51 +136,65 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe // ルート作成のクリック処理 if (view == root) { imageRoot.setImageResource(R.drawable.pin); - fetchDataAndCreateRoute(); - + checkSetupAndCreateRoute(this::fetchDataAndCreateRoute); } if (view == imageRoot) { imageRoot.setImageResource(R.drawable.pin); - fetchDataAndCreateRoute(); + checkSetupAndCreateRoute(this::fetchDataAndCreateRoute); } // 提出状況のクリック処理 if (view == submission) { - ArrayList submissionStudents = getSubmissionStudents(); - Intent toSubmission = new Intent(MainActivity.this, SubmissionActivity.class); - toSubmission.putParcelableArrayListExtra("submissionStudents", submissionStudents); - startActivity(toSubmission); + checkSetupAndCreateRoute(() -> { + ArrayList submissionStudents = getSubmissionStudents(); + Intent toSubmission = new Intent(MainActivity.this, SubmissionActivity.class); + toSubmission.putParcelableArrayListExtra("submissionStudents", submissionStudents); + startActivity(toSubmission); + }); } if (view == imageSubmission) { - ArrayList submissionStudents = getSubmissionStudents(); - Intent toSubmission = new Intent(MainActivity.this, SubmissionActivity.class); - toSubmission.putParcelableArrayListExtra("submissionStudents", submissionStudents); - startActivity(toSubmission); + checkSetupAndCreateRoute(() -> { + ArrayList submissionStudents = getSubmissionStudents(); + Intent toSubmission = new Intent(MainActivity.this, SubmissionActivity.class); + toSubmission.putParcelableArrayListExtra("submissionStudents", submissionStudents); + startActivity(toSubmission); + }); } } - //UUIDを表示するかのダイアログ + //ID作成、表示に関する処理 private void showUUIDYesNoDialog() { firestoreReception_classIdDatabase = new FirestoreReception_classIdDatabase(); List classIdList = firestoreReception_classIdDatabase.getAllDocumentsFromClassIdDatabase(); AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setTitle("クラスID"); - builder.setMessage("あなたのクラスIDを表示しますか?"); + builder.setTitle("ID"); + builder.setMessage("あなたのIDを表示/もしくは新規で作成しますか?"); - builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { + //作成処理 + builder.setPositiveButton("作成", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { classId = CreateUUID.generateUUID(classIdList); - Toast.makeText(MainActivity.this, "クラスID: " + classId, Toast.LENGTH_SHORT).show(); - imageUuid.setImageResource(R.drawable.checked_image); + // 生成されたIDを表示するメソッド + showClassIdDialog("生成されたID", classId); } }); - builder.setNegativeButton("No", new DialogInterface.OnClickListener() { + //表示処理 + builder.setNegativeButton("表示", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - Log.d("DialogNO", "DialogでNoが選ばれました"); - imageUuid.setImageResource(R.drawable.checked_image); + //roomを扱うため非同期処理 + ExecutorService executor = Executors.newSingleThreadExecutor(); + executor.execute(() -> { + // 現在のクラスIDを取得 + int currentClassId = getCurrentClassIdFromRoom(); + runOnUiThread(() -> { + // 現在のクラスIDを表示するダイアログ + showClassIdDialog("現在のID", currentClassId); + }); + }); + executor.shutdown(); } }); @@ -189,12 +203,86 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe } + private int getCurrentClassIdFromRoom() { + AppDatabase db = getDatabaseInstance(); + SetUpTableDao setUpTableDao = db.setUpTableDao(); + + // 現在のクラスIDを取得 + return setUpTableDao.getClassId(); + } + + //クラスIDを表示するダイアログ + private void showClassIdDialog(String title, int classId) { + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle(title); + builder.setMessage("ID: " + classId); + builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + } + }); + AlertDialog dialog = builder.create(); + dialog.show(); + } + + + //ルート作成、提出状況の遷移を行う前のチェックを行う処理 + private void checkSetupAndCreateRoute(Runnable onSetupComplete) { + if (isClassIdSet()) { + isSetupExists(classId).thenAccept(setupExists -> { + if (setupExists) { + runOnUiThread(onSetupComplete); + } else { + runOnUiThread(() -> { + Toast.makeText(this, "先にセットアップを済ませてください", Toast.LENGTH_LONG).show(); + }); + } + }).exceptionally(ex -> { + ex.printStackTrace(); + runOnUiThread(() -> { + Toast.makeText(this, "エラーが発生しました", Toast.LENGTH_LONG).show(); + }); + return null; + }); + } else { + Toast.makeText(this, "先にIDの作成を行ってください", Toast.LENGTH_LONG).show(); + } + } + + // クラスIDが設定されているかどうかを判定 + private boolean isClassIdSet() { + // classIdが0より大きい場合、trueを返す + return classId > 0; + } + + // セットアップが存在するかどうかを判定 + private CompletableFuture isSetupExists(int classId) { + final ExecutorService executorService = Executors.newSingleThreadExecutor(); + return CompletableFuture.supplyAsync(() -> { + AppDatabase db = getDatabaseInstance(); + SetUpTableDao setUpTableDao = db.setUpTableDao(); + //データベースの値を全取得 + List checkData = setUpTableDao.getAll(); + for (SetUpTable setUpTable : checkData) { + //SetUpTableのclassIdと引数のclassIdが一致する場合、trueを返す + if (setUpTable.classId == classId) { + return true; + } + } + return false; + //処理完了時にexecutorServiceをシャットダウン + }, executorService).whenComplete((result, throwable) -> executorService.shutdown()); + } + + //ルート作成の非同期処理 private void fetchDataAndCreateRoute() { ExecutorService executor = Executors.newSingleThreadExecutor(); executor.execute(() -> { - AppDatabase db = getDatabaseInstance(); SetUpTableDao setUpTableDao = db.setUpTableDao(); + AppDatabase db = getDatabaseInstance(); + SetUpTableDao setUpTableDao = db.setUpTableDao(); int totalStudentInt = setUpTableDao.getTotalStudent(); int myDataListSizeInt = firestoreReception.getMyDataListSize(); @@ -245,10 +333,10 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe //final宣言することによって、スレッドセーフになる(ラムダ式内で使えるようにする) final List finalMyDataList = myDataList; CreateSchedule createSchedule = new CreateSchedule(MainActivity.this); - String startPointLatLngString = createSchedule.receiveData(myDataList, getApplicationContext()); + String startPointLatLngString = createSchedule.receiveData(myDataList, getApplicationContext()); runOnUiThread(() -> { - if ( !startPointLatLngString.equals("")) { + if (!startPointLatLngString.equals("")) { Log.d("MainActivity", "スケジュール作成成功"); saveMyDataList(finalMyDataList); Intent toRoot = new Intent(MainActivity.this, Maps.class); @@ -264,6 +352,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe executor.shutdown(); }); } + private void saveMyDataList(List myDataList) { // 共有プリファレンスのインスタンスを取得 SharedPreferences sharedPreferences = getSharedPreferences("MyDataList", MODE_PRIVATE); @@ -307,7 +396,6 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe } - //提出状況の取得 private ArrayList getSubmissionStudents() { ArrayList submissionStudents = new ArrayList<>();