バグ修正

This commit is contained in:
nemukemo 2024-06-21 10:51:26 +09:00
commit 13120b9856
9 changed files with 188 additions and 76 deletions

View File

@ -1,9 +1,9 @@
package com.example.oplogy; package com.example.oplogy;
import androidx.room.Database; import androidx.room.Database;
import androidx.room.RoomDatabase; import androidx.room.RoomDatabase;
import androidx.room.TypeConverters;
@Database(entities = {SetUpTable.class}, version = 1) @Database(entities = {SetUpTable.class}, version = 2)
public abstract class AppDatabase extends RoomDatabase { public abstract class AppDatabase extends RoomDatabase {
// データベースにアクセスするためのメソッドを提供する // データベースにアクセスするためのメソッドを提供する
public abstract SetUpTableDao setUpTableDao(); public abstract SetUpTableDao setUpTableDao();

View File

@ -1,18 +1,82 @@
package com.example.oplogy; package com.example.oplogy;
import android.content.Context;
import android.util.Log; import android.util.Log;
import androidx.room.Room;
import com.google.firebase.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CreateRoot { public class CreateRoot {
public void receiveData(List<MyDataClass> myDataList) { private Context context;
private AppDatabase db;
// デバッグ用ログ
for(MyDataClass data : myDataList){
Log.d("CreateRoot", "data: "+ data.toString());
}
Log.d("CreateRoot", "myDataList[0]: " + myDataList.get(0).toString());
public CreateRoot(MainActivity activity) {
this.context = activity;
this.db = Room.databaseBuilder(activity.getApplicationContext(), AppDatabase.class, "SetUpTable").build();
} }
}
public void receiveData(List<MyDataClass> myDataList) {
for (int i = 0; i < myDataList.size(); i++) {
//希望時間帯の終了時刻から開始時刻を引いて希望時間帯の長さ(timezone)に入れる
MyDataClass data = myDataList.get(i);
List<Timestamp> firstDay = data.getFirstDay();
Timestamp startTime = firstDay.get(0);
Timestamp endTime = firstDay.get(1);
Long timezone = endTime.getSeconds() - startTime.getSeconds();
data.setTimezone(timezone);
//TimeStampを日付に変換
Date startDate = new Date(startTime.getSeconds() * 1000);
Date endDate = new Date(endTime.getSeconds() * 1000);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String startDateString = sdf.format(startDate);
String endDateString = sdf.format(endDate);
// myDataList の中の data に追加する処理
myDataList.get(i).setTimezone(timezone);
myDataList.get(i).setStartDateString(startDateString);
myDataList.get(i).setEndDateString(endDateString);
// ログ出力
Log.d("CreateRoot", "(index: " + i + ") timezone: " + myDataList.get(i).getTimezone());
Log.d("CreateRoot", "(index: " + i + ") startDate: " + myDataList.get(i).getStartDateString());
Log.d("CreateRoot", "(index: " + i + ") data: " + myDataList.get(i));
}
// timezoneを比較するComparatortimezoneが短い順に並べる
Comparator<MyDataClass> comparator = new Comparator<MyDataClass>() {
@Override
public int compare(MyDataClass data1, MyDataClass data2) {
return data1.getTimezone().compareTo(data2.getTimezone());
}
};
// myDataListをtimezoneの値でソート
Collections.sort(myDataList, comparator);
// ソート後のmyDataListをログ出力
for (int i = 0; i < myDataList.size(); i++) {
Log.d("CreateRoot", "(index: " + i + ") timezone: " + myDataList.get(i).getTimezone());
Log.d("CreateRoot", "(index: " + i + ") startDate: " + myDataList.get(i).getStartDateString());
Log.d("CreateRoot", "(index: " + i + ") data: " + myDataList.get(i));
}
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(() -> {
SetUpTableDao setUpTableDao = db.setUpTableDao();
String startTime=setUpTableDao.getStartTime();
String endTime=setUpTableDao.getEndTime();
Log.d("CreateRoot", "開始時間" + startTime);
Log.d("CreateRoot", "終了時刻" + endTime);
});
}
}

View File

@ -64,8 +64,6 @@ public class FirestoreReception {
for(MyDataClass data :myDataList){ for(MyDataClass data :myDataList){
Log.i("FirestoreReceptiond", "data: " + data.toString()); Log.i("FirestoreReceptiond", "data: " + data.toString());
} }
CreateRoot createRoot=new CreateRoot();
createRoot.receiveData(myDataList);
} else { } else {
Log.w("FirestoreReceptiond", "Error getting documents.", task.getException()); Log.w("FirestoreReceptiond", "Error getting documents.", task.getException());
} }

View File

@ -15,6 +15,8 @@ import androidx.room.Room;
import com.google.firebase.firestore.FirebaseFirestore; import com.google.firebase.firestore.FirebaseFirestore;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -93,27 +95,48 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
// ルート作成のクリック処理 // ルート作成のクリック処理
if(view == root){ if(view == root){
imageRoot.setImageResource(R.drawable.pin); imageRoot.setImageResource(R.drawable.pin);
ExecutorService executor = Executors.newSingleThreadExecutor(); ExecutorService executor = Executors.newSingleThreadExecutor();
CountDownLatch latch = new CountDownLatch(2);
executor.execute(() -> { executor.execute(() -> {
AppDatabase db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "SetUpTable").build(); AppDatabase db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "SetUpTable").build();
SetUpTableDao setUpTableDao = db.setUpTableDao(); SetUpTableDao setUpTableDao = db.setUpTableDao();
// データベースに登録されている生徒の数formにデータを送信した生徒の合計数をを取得 Log.d("MainActivity", "db" + setUpTableDao.getAll());
int totalStudent = setUpTableDao.getTotalStudent(); int totalStudent = setUpTableDao.getTotalStudent();
int myDataListSize = firestoreReception.myDataList.size(); int myDataListSize = firestoreReception.getMyDataListSize();
runOnUiThread(() -> { runOnUiThread(() -> {
if (totalStudent != myDataListSize) { if (totalStudent != myDataListSize) {
// 値が一致しない場合ダイアログを表示 showRouteCreationDialog(latch);
showRouteCreationDialog();
} else { } else {
Intent toRoot = new Intent(MainActivity.this,Maps.class); latch.countDown();
startActivity(toRoot);
} }
}); });
}); });
executor.execute(() -> {
List<MyDataClass> myDataList = firestoreReception.getMyDataList();
CreateRoot createRoot = new CreateRoot(MainActivity.this);
createRoot.receiveData(myDataList);
latch.countDown();
});
new Thread(() -> {
try {
latch.await(); // Both tasks must call countDown() before this returns
runOnUiThread(() -> {
Intent toRoot = new Intent(MainActivity.this, Maps.class);
startActivity(toRoot);
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
executor.shutdown();
} }
// 提出状況のクリック処理 // 提出状況のクリック処理
if(view == submission){ if(view == submission){
@ -145,15 +168,14 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
builder.show(); builder.show();
} }
//ルート作成のダイアログ //ルート作成のダイアログ
private void showRouteCreationDialog() { private void showRouteCreationDialog(CountDownLatch latch) {
new AlertDialog.Builder(MainActivity.this) new AlertDialog.Builder(MainActivity.this)
.setTitle("警告") .setTitle("警告")
.setMessage("人数が足りてませんがそれでもルート作成を行いますか?") .setMessage("人数が足りてませんがそれでもルート作成を行いますか?")
.setPositiveButton("OK", new DialogInterface.OnClickListener() { .setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
Intent toRoot = new Intent(MainActivity.this,Maps.class); latch.countDown();
startActivity(toRoot);
} }
}) })
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

View File

@ -14,6 +14,9 @@ public class MyDataClass {
List<Timestamp> thirdDay; List<Timestamp> thirdDay;
List<Timestamp> secondDay; List<Timestamp> secondDay;
double latitude; double latitude;
private Long Timezone;
private String startDateString;
private String endDateString;
public MyDataClass(String patronName, int classId, List<String> address, List<Timestamp> firstDay, int studentNumber, String childName, List<Timestamp> thirdDay, List<Timestamp> secondDay) { public MyDataClass(String patronName, int classId, List<String> address, List<Timestamp> firstDay, int studentNumber, String childName, List<Timestamp> thirdDay, List<Timestamp> secondDay) {
this.patronName = patronName; this.patronName = patronName;
@ -39,60 +42,97 @@ public class MyDataClass {
", secondDay=" + secondDay + ", secondDay=" + secondDay +
'}'; '}';
} }
//getter //getter
public String getPatronName() { public String getPatronName() {
return patronName; return patronName;
} }
public int getClassId() {
return classId;
}
public List<String> getAddress() {
return address;
}
public List<Timestamp> getFirstDay() {
return firstDay;
}
public int getStudentNumber() {
return studentNumber;
}
public String getChildName() {
return childName;
}
public List<Timestamp> getThirdDay() {
return thirdDay;
}
public List<Timestamp> getSecondDay() {
return secondDay;
}
public double getLatitude() {
return latitude;
}
//setter //setter
public void setPatronName(String patronName) { public void setPatronName(String patronName) {
this.patronName = patronName; this.patronName = patronName;
} }
public int getClassId() {
return classId;
}
public void setClassId(int classId) { public void setClassId(int classId) {
this.classId = classId; this.classId = classId;
} }
public List<String> getAddress() {
return address;
}
public void setAddress(List<String> address) { public void setAddress(List<String> address) {
this.address = address; this.address = address;
} }
public List<Timestamp> getFirstDay() {
return firstDay;
}
public void setFirstDay(List<Timestamp> firstDay) { public void setFirstDay(List<Timestamp> firstDay) {
this.firstDay = firstDay; this.firstDay = firstDay;
} }
public int getStudentNumber() {
return studentNumber;
}
public void setStudentNumber(int studentNumber) { public void setStudentNumber(int studentNumber) {
this.studentNumber = studentNumber; this.studentNumber = studentNumber;
} }
public String getChildName() {
return childName;
}
public void setChildName(String childName) { public void setChildName(String childName) {
this.childName = childName; this.childName = childName;
} }
public List<Timestamp> getThirdDay() {
return thirdDay;
}
public void setThirdDay(List<Timestamp> thirdDay) { public void setThirdDay(List<Timestamp> thirdDay) {
this.thirdDay = thirdDay; this.thirdDay = thirdDay;
} }
public List<Timestamp> getSecondDay() {
return secondDay;
}
public void setSecondDay(List<Timestamp> secondDay) { public void setSecondDay(List<Timestamp> secondDay) {
this.secondDay = secondDay; this.secondDay = secondDay;
} }
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) { public void setLatitude(double latitude) {
this.latitude = latitude; this.latitude = latitude;
} }
public void setEndDateString(String endDateString) {
this.endDateString = endDateString;
}
public Long getTimezone() {
return Timezone;
}
public void setTimezone(Long Timezone) {
this.Timezone = Timezone;
}
public String getStartDateString() {
return startDateString;
}
public void setStartDateString(String startDateString) {
this.startDateString = startDateString;
}
} }

View File

@ -1,9 +1,9 @@
package com.example.oplogy; package com.example.oplogy;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class RootSearchActivity extends AppCompatActivity { public class RootSearchActivity extends AppCompatActivity {
@Override @Override

View File

@ -19,7 +19,6 @@ import androidx.fragment.app.FragmentActivity;
import androidx.room.Room; import androidx.room.Room;
import java.util.Locale; import java.util.Locale;
import java.util.Set;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -125,7 +124,9 @@ public class SetUpActivity extends FragmentActivity
getApplicationContext(), getApplicationContext(),
AppDatabase.class, AppDatabase.class,
"SetUpTable" "SetUpTable"
).build(); )
.fallbackToDestructiveMigration()
.build();
SetUpTableDao setUpTableDao = db.setUpTableDao(); SetUpTableDao setUpTableDao = db.setUpTableDao();
// Roomの操作を行う // Roomの操作を行う
SetUpTable setUpTable = new SetUpTable( SetUpTable setUpTable = new SetUpTable(

View File

@ -9,22 +9,24 @@ public class SetUpTable {
public int id; public int id;
public String teacherName; public String teacherName;
public String startPoint; public String startPoint;
public String endPoint;
public String startTime; public String startTime;
public String endTime; public String endTime;
public String breakStartTime; public String intervalTime;
public String breakEndTime; public String startBreakTime;
public String endBreakTime;
public int totalStudent; public int totalStudent;
//コンストラクタ //コンストラクタ
public SetUpTable(String teacherName, String startPoint, String endPoint, String startTime, String endTime, String breakStartTime,String breakEndTime, int totalStudent) { public SetUpTable(String teacherName, String startPoint, String startTime, String endTime,
String intervalTime, String startBreakTime, String endBreakTime, int totalStudent) {
this.teacherName = teacherName; this.teacherName = teacherName;
this.startPoint = startPoint; this.startPoint = startPoint;
this.endPoint = endPoint;
this.startTime = startTime; this.startTime = startTime;
this.endTime = endTime; this.endTime = endTime;
this.breakStartTime = breakStartTime; this.intervalTime = intervalTime;
this.breakEndTime = breakEndTime; this.startBreakTime = startBreakTime;
this.endBreakTime = endBreakTime;
this.totalStudent = totalStudent; this.totalStudent = totalStudent;
} }
//getter //getter
@ -37,21 +39,12 @@ public class SetUpTable {
public String getStartPoint() { public String getStartPoint() {
return startPoint; return startPoint;
} }
public String getEndPoint() {
return endPoint;
}
public String getStartTime() { public String getStartTime() {
return startTime; return startTime;
} }
public String getEndTime() { public String getEndTime() {
return endTime; return endTime;
} }
public String getBreakStartTime() {
return breakStartTime;
}
public String getBreakEndTime() {
return breakEndTime;
}
public int getTotalStudent() { public int getTotalStudent() {
return totalStudent; return totalStudent;
} }
@ -65,21 +58,12 @@ public class SetUpTable {
public void setStartPoint(String startPoint) { public void setStartPoint(String startPoint) {
this.startPoint = startPoint; this.startPoint = startPoint;
} }
public void setEndPoint(String endPoint) {
this.endPoint = endPoint;
}
public void setStartTime(String startTime) { public void setStartTime(String startTime) {
this.startTime = startTime; this.startTime = startTime;
} }
public void setEndTime(String endTime) { public void setEndTime(String endTime) {
this.endTime = endTime; this.endTime = endTime;
} }
public void setBreakStartTime(String breakStartTime) {
this.breakStartTime = breakStartTime;
}
public void setBreakEndTime(String breakEndTime) {
this.breakEndTime = breakEndTime;
}
public void setTotalStudent(int totalStudent) { public void setTotalStudent(int totalStudent) {
this.totalStudent = totalStudent; this.totalStudent = totalStudent;
} }

View File

@ -18,6 +18,9 @@ public interface SetUpTableDao {
//削除処理 //削除処理
@Query("DELETE FROM SetUpTable") @Query("DELETE FROM SetUpTable")
void deleteAll(); void deleteAll();
//全件取得
@Query("SELECT * FROM SetUpTable")
List<SetUpTable> getAll();
@Query("SELECT * FROM SetUpTable WHERE teacherName = :name LIMIT 1") @Query("SELECT * FROM SetUpTable WHERE teacherName = :name LIMIT 1")
SetUpTable findByName(String name); SetUpTable findByName(String name);