docking #36

Merged
Utahshi merged 4 commits from docking into master 2024-07-05 03:04:51 +00:00
Showing only changes of commit 06fdb9a312 - Show all commits

View File

@ -52,7 +52,6 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
//取得するためのクラスID //取得するためのクラスID
private int classId; private int classId;
private String address; private String address;
private Object finalMyDataList;
@Override @Override
@ -94,8 +93,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
ExecutorService executor = Executors.newSingleThreadExecutor(); ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(() -> { executor.execute(() -> {
try { try {
AppDatabase db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "SetUpTable") AppDatabase db = getDatabaseInstance();
.build();
SetUpTableDao setUpTableDao = db.setUpTableDao(); SetUpTableDao setUpTableDao = db.setUpTableDao();
classId = setUpTableDao.getClassId(); classId = setUpTableDao.getClassId();
firestoreReception.getDocumentsByClassId(classId); firestoreReception.getDocumentsByClassId(classId);
@ -109,6 +107,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
} }
// クリック処理 // クリック処理
@Override @Override
public void onClick(View view) { public void onClick(View view) {
@ -161,7 +160,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
} }
} }
//UUIDを表示するかのダイアログ //IDに関する処理
private void showUUIDYesNoDialog() { private void showUUIDYesNoDialog() {
firestoreReception_classIdDatabase = new FirestoreReception_classIdDatabase(); firestoreReception_classIdDatabase = new FirestoreReception_classIdDatabase();
List<Integer> classIdList = firestoreReception_classIdDatabase.getAllDocumentsFromClassIdDatabase(); List<Integer> classIdList = firestoreReception_classIdDatabase.getAllDocumentsFromClassIdDatabase();
@ -169,19 +168,30 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("クラスID"); builder.setTitle("クラスID");
builder.setMessage("あなたのクラスIDを表示しますか?"); builder.setMessage("あなたのクラスIDを表示/もしくは新規で作成しますか?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { builder.setPositiveButton("作成", new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
classId = CreateUUID.generateUUID(classIdList); classId = CreateUUID.generateUUID(classIdList);
Toast.makeText(MainActivity.this, "クラスID: " + classId, Toast.LENGTH_SHORT).show(); // 生成されたクラスIDを表示するメソッド
showClassIdDialog("生成されたクラスID",classId);
} }
}); });
builder.setNegativeButton("No", new DialogInterface.OnClickListener() { builder.setNegativeButton("表示", new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
Log.d("DialogNO", "DialogでNoが選ばれました"); //roomを扱うため非同期処理
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(() -> {
// 現在のクラスIDを取得
int currentClassId = getCurrentClassIdFromRoom();
runOnUiThread(() -> {
// 現在のクラスIDを表示するダイアログ
showClassIdDialog("現在のクラスID",currentClassId);
});
});
executor.shutdown();
} }
}); });
@ -189,81 +199,103 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
alertDialog.show(); alertDialog.show();
} }
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 fetchDataAndCreateRoute() { private void fetchDataAndCreateRoute() {
//非同期処理の開始
ExecutorService executor = Executors.newSingleThreadExecutor(); ExecutorService executor = Executors.newSingleThreadExecutor();
CountDownLatch latch = new CountDownLatch(2);
// タスク1: ローカルDBから生徒数を取得してtotalStudentと比較
executor.execute(() -> { executor.execute(() -> {
AppDatabase db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "SetUpTable").build(); AppDatabase db = getDatabaseInstance(); SetUpTableDao setUpTableDao = db.setUpTableDao();
SetUpTableDao setUpTableDao = db.setUpTableDao();
Log.d("MainActivity", "db" + setUpTableDao.getAll());
int totalStudent = setUpTableDao.getTotalStudent(); int totalStudent = setUpTableDao.getTotalStudent();
int myDataListSize = firestoreReception.getMyDataListSize(); int myDataListSize = firestoreReception.getMyDataListSize();
//総生徒数と提出済みになっている生徒の数が一致するかの確認
runOnUiThread(() -> { runOnUiThread(() -> {
if (totalStudent != myDataListSize) { if (totalStudent != myDataListSize) {
showRouteCreationDialog(latch); //未提出者がいることの警告ダイアログ
showRouteCreationDialog();
} else { } else {
latch.countDown(); //ルート作成
createRoute(executor);
} }
}); });
}); });
// タスク2: ルート作成を行う // `fetchDataAndCreateRoute`メソッド内ではshutdownを呼び出さない
}
private void showRouteCreationDialog() {
new AlertDialog.Builder(MainActivity.this)
.setTitle("警告")
.setMessage("人数が足りてませんがそれでもルート作成を行いますか?")
.setPositiveButton("OK", (dialog, which) -> {
// 新しいExecutorServiceを作成してタスクを実行
ExecutorService dialogExecutor = Executors.newSingleThreadExecutor();
createRoute(dialogExecutor);
dialogExecutor.shutdown();
})
.setNegativeButton("Cancel", (dialog, which) -> {
dialog.dismiss();
})
.show();
}
private void createRoute(ExecutorService executor) {
executor.execute(() -> { executor.execute(() -> {
List<MyDataClass> myDataList = null; List<MyDataClass> myDataList = null;
while (myDataList == null) { while (myDataList == null) {
myDataList = firestoreReception.getMyDataList(); myDataList = firestoreReception.getMyDataList();
try { try {
Thread.sleep(3000); Thread.sleep(3000);
Log.d("MainActivity", "myDataList" + myDataList.size());
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new RuntimeException(e); Thread.currentThread().interrupt();
return;
} }
} }
Log.d("MainActivity", "myDataList" + myDataList.size());
//final宣言することによってスレッドセーフになる(ラムダ式内で使えるようにする)
final List<MyDataClass> finalMyDataList = myDataList;
CreateSchedule createSchedule = new CreateSchedule(MainActivity.this); CreateSchedule createSchedule = new CreateSchedule(MainActivity.this);
Boolean notDuplicates = createSchedule.receiveData(myDataList, getApplicationContext()); Boolean notDuplicates = createSchedule.receiveData(myDataList, getApplicationContext());
latch.countDown();
if (notDuplicates) { runOnUiThread(() -> {
Log.d("MainActivity", "スケジュール作成成功"); if (notDuplicates) {
saveMyDataList(myDataList); Log.d("MainActivity", "スケジュール作成成功");
Intent toRoot = new Intent(MainActivity.this, Maps.class); saveMyDataList(myDataList);
startActivity(toRoot); Intent toRoot = new Intent(MainActivity.this, Maps.class);
} else { startActivity(toRoot);
showErrorDialog(latch, myDataList); } else {
} //保護者の重複による警告ダイアログ
showErrorDialog(finalMyDataList);
}
});
// createRouteの最後にexecutorをシャットダウン
executor.shutdown();
}); });
} }
//ルート作成のダイアログ
private void showRouteCreationDialog(CountDownLatch latch) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("警告")
.setMessage("人数が足りてませんがそれでもルート作成を行いますか?")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
latch.countDown();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.show();
}
// MyDataListを共有プリファレンスに保存するメソッド
private void saveMyDataList(List<MyDataClass> myDataList) { private void saveMyDataList(List<MyDataClass> myDataList) {
// 共有プリファレンスのインスタンスを取得 // 共有プリファレンスのインスタンスを取得
SharedPreferences sharedPreferences = getSharedPreferences("MyDataList", MODE_PRIVATE); SharedPreferences sharedPreferences = getSharedPreferences("MyDataList", MODE_PRIVATE);
@ -278,11 +310,11 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
editor.apply(); editor.apply();
} }
public void showErrorDialog(CountDownLatch latch, List<MyDataClass> myDataList) { private void showErrorDialog(List<MyDataClass> myDataList) {
List<Integer> studentNumbers = new ArrayList<>(); List<Integer> studentNumbers = new ArrayList<>();
for (int i = 0; i < myDataList.size(); i++) { for (MyDataClass data : myDataList) {
if (myDataList.get(i).getSchedule() == 0) { if (data.getSchedule() == 0) {
studentNumbers.add(myDataList.get(i).getStudentNumber()); studentNumbers.add(data.getStudentNumber());
} }
} }
StringBuilder message = new StringBuilder("保護者の重複が重大でルート作成ができません。調整してください。\n出席番号: "); StringBuilder message = new StringBuilder("保護者の重複が重大でルート作成ができません。調整してください。\n出席番号: ");
@ -295,15 +327,19 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
new AlertDialog.Builder(MainActivity.this) new AlertDialog.Builder(MainActivity.this)
.setTitle("警告") .setTitle("警告")
.setMessage(message.toString()) .setMessage(message.toString())
.setPositiveButton("OK", new DialogInterface.OnClickListener() { .setPositiveButton("OK", (dialog, which) -> {
@Override dialog.dismiss();
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}) })
.show(); .show();
} }
private AppDatabase getDatabaseInstance() {
return Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "SetUpTable").build();
}
//提出状況の取得 //提出状況の取得
private ArrayList<SubmissionStudent> getSubmissionStudents() { private ArrayList<SubmissionStudent> getSubmissionStudents() {
ArrayList<SubmissionStudent> submissionStudents = new ArrayList<>(); ArrayList<SubmissionStudent> submissionStudents = new ArrayList<>();
@ -314,7 +350,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
executor.execute(() -> { executor.execute(() -> {
// 1. Roomデータベースから全生徒数を取得 // 1. Roomデータベースから全生徒数を取得
AppDatabase db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "SetUpTable").build(); AppDatabase db = getDatabaseInstance();
SetUpTableDao setUpTableDao = db.setUpTableDao(); SetUpTableDao setUpTableDao = db.setUpTableDao();
int totalStudent = setUpTableDao.getTotalStudent(); int totalStudent = setUpTableDao.getTotalStudent();
// 2. Firestoreから生徒番号のリストを取得 // 2. Firestoreから生徒番号のリストを取得