-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement saving of completed surveys to RoomD
Signed-off-by: Amr Hossam <[email protected]>
- Loading branch information
1 parent
93fec4a
commit 23361cc
Showing
11 changed files
with
207 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
onebusaway-android/src/main/java/org/onebusaway/android/database/AppDatabase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.onebusaway.android.database | ||
|
||
import androidx.room.Database | ||
import androidx.room.RoomDatabase | ||
import org.onebusaway.android.ui.survey.dao.StudiesDao | ||
import org.onebusaway.android.ui.survey.dao.SurveysDao | ||
import org.onebusaway.android.ui.survey.entity.Study | ||
import org.onebusaway.android.ui.survey.entity.Survey | ||
|
||
@Database(entities = [Study::class, Survey::class], version = 1) | ||
abstract class AppDatabase : RoomDatabase() { | ||
abstract fun studiesDao(): StudiesDao | ||
abstract fun surveysDao(): SurveysDao | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
onebusaway-android/src/main/java/org/onebusaway/android/ui/survey/dao/StudiesDao.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.onebusaway.android.ui.survey.dao | ||
|
||
import androidx.room.* | ||
import org.onebusaway.android.ui.survey.entity.Study | ||
|
||
@Dao | ||
interface StudiesDao { | ||
@Insert(onConflict = OnConflictStrategy.IGNORE) | ||
suspend fun insertStudy(study: Study): Long | ||
|
||
@Update | ||
suspend fun updateStudy(study: Study) | ||
|
||
@Delete | ||
suspend fun deleteStudy(study: Study) | ||
|
||
@Query("SELECT * FROM studies WHERE study_id = :studyId") | ||
suspend fun getStudyById(studyId: Int): Study? | ||
|
||
@Query("SELECT * FROM studies") | ||
suspend fun getAllStudies(): List<Study> | ||
} |
29 changes: 29 additions & 0 deletions
29
onebusaway-android/src/main/java/org/onebusaway/android/ui/survey/dao/SurveysDao.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.onebusaway.android.ui.survey.dao | ||
|
||
import androidx.room.* | ||
import org.onebusaway.android.ui.survey.entity.Survey | ||
|
||
@Dao | ||
interface SurveysDao { | ||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
suspend fun insertSurvey(survey: Survey): Long | ||
|
||
@Update | ||
suspend fun updateSurvey(survey: Survey) | ||
|
||
@Delete | ||
suspend fun deleteSurvey(survey: Survey) | ||
|
||
@Query("SELECT *FROM surveys") | ||
suspend fun getAllSurveys():List<Survey> | ||
|
||
@Query("SELECT * FROM surveys WHERE survey_id = :surveyId") | ||
suspend fun getSurveyById(surveyId: Int): Survey? | ||
|
||
@Query("SELECT * FROM surveys WHERE study_id = :studyId") | ||
suspend fun getSurveysByStudyId(studyId: Int): List<Survey> | ||
|
||
@Query("SELECT COUNT(*) > 0 FROM surveys WHERE survey_id = :surveyId") | ||
suspend fun isSurveyIdExists(surveyId: Int): Boolean | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
onebusaway-android/src/main/java/org/onebusaway/android/ui/survey/entity/Study.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.onebusaway.android.ui.survey.entity | ||
|
||
import androidx.room.Entity | ||
import androidx.room.PrimaryKey | ||
|
||
@Entity(tableName = "studies") | ||
data class Study( | ||
@PrimaryKey val study_id: Int, | ||
val name: String, | ||
val description: String, | ||
val is_subscribed: Boolean | ||
) |
17 changes: 17 additions & 0 deletions
17
onebusaway-android/src/main/java/org/onebusaway/android/ui/survey/entity/Survey.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.onebusaway.android.ui.survey.entity | ||
|
||
import androidx.room.Entity | ||
import androidx.room.ForeignKey | ||
import androidx.room.PrimaryKey | ||
|
||
@Entity( | ||
tableName = "surveys", foreignKeys = [ForeignKey( | ||
entity = Study::class, | ||
parentColumns = ["study_id"], | ||
childColumns = ["study_id"], | ||
onDelete = ForeignKey.CASCADE | ||
)] | ||
) | ||
data class Survey( | ||
@PrimaryKey val survey_id: Int, val study_id: Int, val name: String | ||
) |
46 changes: 46 additions & 0 deletions
46
...way-android/src/main/java/org/onebusaway/android/ui/survey/repository/SurveyRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.onebusaway.android.ui.survey.repository | ||
|
||
import android.content.Context | ||
import androidx.room.Room | ||
import org.onebusaway.android.database.AppDatabase | ||
import org.onebusaway.android.ui.survey.entity.Study | ||
import org.onebusaway.android.ui.survey.entity.Survey | ||
|
||
|
||
class SurveyRepository(context: Context) { | ||
private val db: AppDatabase = Room.databaseBuilder( | ||
context.applicationContext, AppDatabase::class.java, "study-survey-db" | ||
).build() | ||
|
||
private val studiesDao = db.studiesDao() | ||
private val surveysDao = db.surveysDao() | ||
|
||
suspend fun addOrUpdateStudy(study: Study) { | ||
val existingStudy = studiesDao.getStudyById(study.study_id) | ||
if (existingStudy == null) { | ||
studiesDao.insertStudy(study) | ||
} else { | ||
studiesDao.updateStudy(study) | ||
} | ||
} | ||
|
||
suspend fun getAllStudies(): List<Study> { | ||
return studiesDao.getAllStudies() | ||
} | ||
|
||
suspend fun addSurvey(survey: Survey) { | ||
surveysDao.insertSurvey(survey) | ||
} | ||
|
||
suspend fun getSurveysForStudy(studyId: Int): List<Survey> { | ||
return surveysDao.getSurveysByStudyId(studyId) | ||
} | ||
|
||
suspend fun checkSurveyCompleted(surveyId: Int): Boolean { | ||
return surveysDao.isSurveyIdExists(surveyId) | ||
} | ||
|
||
suspend fun getAllSurveys(): List<Survey> { | ||
return surveysDao.getAllSurveys(); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
onebusaway-android/src/main/java/org/onebusaway/android/ui/survey/utils/SurveyDbHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.onebusaway.android.ui.survey.utils | ||
|
||
import android.content.Context | ||
import android.util.Log | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.SupervisorJob | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.runBlocking | ||
import org.onebusaway.android.io.request.survey.model.StudyResponse | ||
import org.onebusaway.android.ui.survey.entity.Study | ||
import org.onebusaway.android.ui.survey.entity.Survey | ||
import org.onebusaway.android.ui.survey.repository.SurveyRepository | ||
|
||
class SurveyDbHelper { | ||
companion object { | ||
private val coroutineScope = CoroutineScope(Dispatchers.IO + SupervisorJob()) | ||
|
||
@JvmStatic | ||
fun markSurveyAsCompleted(context: Context, survey: StudyResponse.Surveys) { | ||
val surveyRepo = SurveyRepository(context) | ||
|
||
val newStudy = Study( | ||
survey.study.id, survey.study.name, survey.study.description, true | ||
) | ||
val newSurvey = Survey(survey.id, survey.study.id, survey.name) | ||
coroutineScope.launch { | ||
try { | ||
surveyRepo.addOrUpdateStudy(newStudy) | ||
surveyRepo.addSurvey(newSurvey) | ||
Log.d("All Saved Surveys", surveyRepo.getAllSurveys().toString()); | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
} | ||
} | ||
} | ||
|
||
@JvmStatic | ||
fun isSurveyCompleted(context: Context, surveyId: Int): Boolean { | ||
val surveyRepo = SurveyRepository(context) | ||
return runBlocking { | ||
try { | ||
surveyRepo.checkSurveyCompleted(surveyId) | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
false | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters