murakumo_ルート作成する前の警告 #45

Open
murakumo wants to merge 4 commits from murakumo_ルート作成する前の警告 into master

View File

@ -141,65 +141,53 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
// ルート作成のクリック処理 // ルート作成のクリック処理
if (view == root) { if (view == root) {
imageRoot.setImageResource(R.drawable.pin); imageRoot.setImageResource(R.drawable.pin);
if (isClassIdSet()) { checkSetupAndCreateRoute(this::fetchDataAndCreateRoute);
isSetupExists(classId).thenAccept(setupExists -> {
if (setupExists) {
fetchDataAndCreateRoute();
} else {
runOnUiThread(() -> {
Toast.makeText(this, "セットアップが設定されていません", Toast.LENGTH_SHORT).show();
});
}
}).exceptionally(ex -> {
ex.printStackTrace();
runOnUiThread(() -> {
Toast.makeText(this, "エラーが発生しました", Toast.LENGTH_SHORT).show();
});
return null;
});
} else {
Toast.makeText(this, "クラスIDが設定されていません", Toast.LENGTH_SHORT).show();
}
} }
if (view == imageRoot) { if (view == imageRoot) {
imageRoot.setImageResource(R.drawable.pin); imageRoot.setImageResource(R.drawable.pin);
fetchDataAndCreateRoute(); checkSetupAndCreateRoute(this::fetchDataAndCreateRoute);
} }
// 提出状況のクリック処理 // 提出状況のクリック処理
if (view == submission) { if (view == submission) {
checkSetupAndCreateRoute(() -> {
ArrayList<SubmissionStudent> submissionStudents = getSubmissionStudents(); ArrayList<SubmissionStudent> submissionStudents = getSubmissionStudents();
Intent toSubmission = new Intent(MainActivity.this, SubmissionActivity.class); Intent toSubmission = new Intent(MainActivity.this, SubmissionActivity.class);
toSubmission.putParcelableArrayListExtra("submissionStudents", submissionStudents); toSubmission.putParcelableArrayListExtra("submissionStudents", submissionStudents);
startActivity(toSubmission); startActivity(toSubmission);
});
} }
if (view == imageSubmission) { if (view == imageSubmission) {
checkSetupAndCreateRoute(() -> {
ArrayList<SubmissionStudent> submissionStudents = getSubmissionStudents(); ArrayList<SubmissionStudent> submissionStudents = getSubmissionStudents();
Intent toSubmission = new Intent(MainActivity.this, SubmissionActivity.class); Intent toSubmission = new Intent(MainActivity.this, SubmissionActivity.class);
toSubmission.putParcelableArrayListExtra("submissionStudents", submissionStudents); toSubmission.putParcelableArrayListExtra("submissionStudents", submissionStudents);
startActivity(toSubmission); startActivity(toSubmission);
});
} }
} }
//IDに関する処理 //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();
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("作成", 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);
// 生成されたクラスIDを表示するメソッド // 生成されたIDを表示するメソッド
showClassIdDialog("生成されたクラスID",classId); showClassIdDialog("生成されたID",classId);
} }
}); });
//表示処理
builder.setNegativeButton("表示", new DialogInterface.OnClickListener() { builder.setNegativeButton("表示", new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
@ -210,7 +198,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
int currentClassId = getCurrentClassIdFromRoom(); int currentClassId = getCurrentClassIdFromRoom();
runOnUiThread(() -> { runOnUiThread(() -> {
// 現在のクラスIDを表示するダイアログ // 現在のクラスIDを表示するダイアログ
showClassIdDialog("現在のクラスID",currentClassId); showClassIdDialog("現在のID",currentClassId);
}); });
}); });
executor.shutdown(); executor.shutdown();
@ -232,7 +220,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
private void showClassIdDialog(String title, int classId) { private void showClassIdDialog(String title, int classId) {
AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(title); builder.setTitle(title);
builder.setMessage("クラスID: " + classId); builder.setMessage("ID: " + classId);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
@ -243,26 +231,61 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
dialog.show(); 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() { private boolean isClassIdSet() {
// classIdが0より大きい場合trueを返す // classIdが0より大きい場合trueを返す
return classId > 0; return classId > 0;
} }
// セットアップが存在するかどうかを判定
private CompletableFuture<Boolean> isSetupExists(int classId) { private CompletableFuture<Boolean> isSetupExists(int classId) {
final ExecutorService executorService = Executors.newSingleThreadExecutor(); final ExecutorService executorService = Executors.newSingleThreadExecutor();
return CompletableFuture.supplyAsync(() -> { return CompletableFuture.supplyAsync(() -> {
AppDatabase db = getDatabaseInstance(); AppDatabase db = getDatabaseInstance();
SetUpTableDao setUpTableDao = db.setUpTableDao(); SetUpTableDao setUpTableDao = db.setUpTableDao();
//データベースの値を全取得
List<SetUpTable> checkData = setUpTableDao.getAll(); List<SetUpTable> checkData = setUpTableDao.getAll();
for (SetUpTable setUpTable : checkData) { for (SetUpTable setUpTable : checkData) {
//SetUpTableのclassIdと引数のclassIdが一致する場合trueを返す
if (setUpTable.classId == classId) { if (setUpTable.classId == classId) {
return true; return true;
} }
} }
return false; return false;
//処理完了時にexecutorServiceをシャットダウン
}, executorService).whenComplete((result, throwable) -> executorService.shutdown()); }, executorService).whenComplete((result, throwable) -> executorService.shutdown());
} }
//ルート作成の非同期処理 //ルート作成の非同期処理
private void fetchDataAndCreateRoute() { private void fetchDataAndCreateRoute() {
ExecutorService executor = Executors.newSingleThreadExecutor(); ExecutorService executor = Executors.newSingleThreadExecutor();
@ -287,6 +310,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
// `fetchDataAndCreateRoute`メソッド内ではshutdownを呼び出さない // `fetchDataAndCreateRoute`メソッド内ではshutdownを呼び出さない
} }
//人数が足りないときに表示するダイアログ
private void showRouteCreationDialog() { private void showRouteCreationDialog() {
new AlertDialog.Builder(MainActivity.this) new AlertDialog.Builder(MainActivity.this)
.setTitle("警告") .setTitle("警告")
@ -303,6 +327,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
.show(); .show();
} }
//ルート作成の非同期処理
private void createRoute(ExecutorService executor) { private void createRoute(ExecutorService executor) {
executor.execute(() -> { executor.execute(() -> {
List<MyDataClass> myDataList = null; List<MyDataClass> myDataList = null;
@ -375,6 +400,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
} }
//Room作成のメソッド
private AppDatabase getDatabaseInstance() { private AppDatabase getDatabaseInstance() {
return Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "SetUpTable").fallbackToDestructiveMigration().build(); return Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "SetUpTable").fallbackToDestructiveMigration().build();
} }