Skip to content

Commit

Permalink
Add read flag to alerts (#140)
Browse files Browse the repository at this point in the history
* Allow to mark alerts as read

* Rename field

* Rename variable

* Update C headers

* Typo

* Correct name
  • Loading branch information
ivnsch authored Jul 29, 2020
1 parent 295a074 commit d474cc9
Show file tree
Hide file tree
Showing 11 changed files with 286 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ class JNIInterfaceBootstrappedTests {
assertEquals(JniVoidResult(6, "Not found"), value)
}

@Test
fun updateAlertIsReadWithTrue() {
val value = JniApi().updateAlertIsRead("1", 1)
assertEquals(JniVoidResult(6, "Not found"), value)
}

@Test
fun updateAlertIsReadWithFalse() {
val value = JniApi().updateAlertIsRead("1", 0)
assertEquals(JniVoidResult(6, "Not found"), value)
}

@Test
fun recordTcn() {
val value = JniApi().recordTcn("2485a64b57addcaea3ed1b538d07dbce", 34.03f)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class JNIInterfaceTests {
runnyNose = true,
other = false,
noSymptoms = true
), 1592567315, 1592567335, 1.2f, 2.1f
), 1592567315, 1592567335, 1.2f, 2.1f, false
)
),
value
Expand All @@ -75,7 +75,7 @@ class JNIInterfaceTests {
runnyNose = true,
other = false,
noSymptoms = true
), 1592567315, 1592567335, 1.2f, 2.1f
), 1592567315, 1592567335, 1.2f, 2.1f, false
),
JniAlert(
"343356", JniPublicSymptoms(
Expand All @@ -90,7 +90,7 @@ class JNIInterfaceTests {
runnyNose = true,
other = false,
noSymptoms = true
), 1592567315, 1592567335, 1.2f, 2.1f
), 1592567315, 1592567335, 1.2f, 2.1f, false
)
)
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ data class Alert(
var contactStart: UnixTime,
var contactEnd: UnixTime,
var minDistance: LengthMeasurement,
var avgDistance: LengthMeasurement
var avgDistance: LengthMeasurement,
var isRead: Boolean
) : Parcelable

enum class FeverSeverity {
Expand Down
7 changes: 5 additions & 2 deletions android/core/core/src/main/java/org/coepi/core/jni/JniApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class JniApi {

external fun deleteAlert(id: String): JniVoidResult

external fun updateAlertIsRead(id: String, isRead: Int): JniVoidResult

external fun generateTcn(): String

external fun recordTcn(tcn: String, distance: Float): JniVoidResult
Expand Down Expand Up @@ -134,11 +136,12 @@ data class JniAlertsArrayResult(

data class JniAlert(
var id: String,
var report: JniPublicSymptoms,
var symptoms: JniPublicSymptoms,
var contactStart: Long,
var contactEnd: Long,
var minDistance: Float,
var avgDistance: Float
var avgDistance: Float,
var isRead: Boolean
)

data class JniPublicSymptoms(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,26 @@ class AlertsFetcherImpl(private val api: JniApi) :
else -> Meters(avgDistance)
},
reportTime = when {
report.reportTime < 0 -> error("Invalid report time: ${report.reportTime}")
else -> UnixTime.fromValue(report.reportTime)
symptoms.reportTime < 0 -> error("Invalid report time: ${symptoms.reportTime}")
else -> UnixTime.fromValue(symptoms.reportTime)
},
earliestSymptomTime = when {
report.earliestSymptomTime == -1L ->
symptoms.earliestSymptomTime == -1L ->
None
report.earliestSymptomTime < -1L ->
error("Invalid earliestSymptomTime: ${report.earliestSymptomTime}")
symptoms.earliestSymptomTime < -1L ->
error("Invalid earliestSymptomTime: ${symptoms.earliestSymptomTime}")
else ->
Some(UnixTime.fromValue(report.earliestSymptomTime))
Some(UnixTime.fromValue(symptoms.earliestSymptomTime))
},
feverSeverity = toFeverSeverity(report.feverSeverity),
coughSeverity = toCoughSeverity(report.coughSeverity),
breathlessness = report.breathlessness,
muscleAches = report.muscleAches,
lossSmellOrTaste = report.lossSmellOrTaste,
diarrhea = report.diarrhea,
runnyNose = report.runnyNose,
other = report.other,
noSymptoms = report.noSymptoms
feverSeverity = toFeverSeverity(symptoms.feverSeverity),
coughSeverity = toCoughSeverity(symptoms.coughSeverity),
breathlessness = symptoms.breathlessness,
muscleAches = symptoms.muscleAches,
lossSmellOrTaste = symptoms.lossSmellOrTaste,
diarrhea = symptoms.diarrhea,
runnyNose = symptoms.runnyNose,
other = symptoms.other,
noSymptoms = symptoms.noSymptoms,
isRead = isRead
)
}
24 changes: 23 additions & 1 deletion src/android/android_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ pub unsafe extern "C" fn Java_org_coepi_core_jni_JniApi_deleteAlert(
delete_alert(&env, id).to_void_jni(&env)
}

#[no_mangle]
pub unsafe extern "C" fn Java_org_coepi_core_jni_JniApi_updateAlertIsRead(
env: JNIEnv,
_: JClass,
id: JString,
is_read: jint,
) -> jobject {
update_alert_is_read(&env, id, is_read).to_void_jni(&env)
}

#[no_mangle]
pub unsafe extern "C" fn Java_org_coepi_core_jni_JniApi_recordTcn(
env: JNIEnv,
Expand Down Expand Up @@ -279,6 +289,15 @@ fn delete_alert(env: &JNIEnv, id: JString) -> Result<(), ServicesError> {
dependencies().alert_dao.delete(id_str.to_owned())
}

fn update_alert_is_read(env: &JNIEnv, id: JString, is_read: jint) -> Result<(), ServicesError> {
let id_java_str = env.get_string(id)?;
let id_str = id_java_str.to_str()?;

dependencies()
.alert_dao
.update_is_read(id_str.to_owned(), is_read == 1)
}

fn record_tcn(env: &JNIEnv, tcn: JString, distance: jfloat) -> Result<(), ServicesError> {
let tcn_java_str = env.get_string(tcn)?;
let tcn_str = tcn_java_str.to_str()?;
Expand Down Expand Up @@ -587,6 +606,7 @@ fn placeholder_alert() -> Alert {
contact_end: 0,
min_distance: 0.0,
avg_distance: 0.0,
is_read: false,
}
}

Expand Down Expand Up @@ -651,18 +671,20 @@ pub fn alert_to_jobject(alert: Alert, env: &JNIEnv) -> Result<jobject, ServicesE
let contact_end_j_value = JValue::from(alert.contact_end as i64);
let min_distance_j_value = JValue::from(alert.min_distance);
let avg_distance_j_value = JValue::from(alert.avg_distance);
let is_read_j_value = JValue::from(alert.is_read);

let result: Result<jobject, jni::errors::Error> = env
.new_object(
jni_alert_class,
"(Ljava/lang/String;Lorg/coepi/core/jni/JniPublicSymptoms;JJFF)V",
"(Ljava/lang/String;Lorg/coepi/core/jni/JniPublicSymptoms;JJFFZ)V",
&[
id_j_value,
JValue::from(jni_public_symptoms_obj),
contact_start_j_value,
contact_end_j_value,
min_distance_j_value,
avg_distance_j_value,
is_read_j_value,
],
)
.map(|o| o.into_inner());
Expand Down
1 change: 1 addition & 0 deletions src/android/jni_domain_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,6 @@ fn create_test_alert(id: &str, report_time: u64) -> Alert {
contact_end: 1592567335,
min_distance: 1.2,
avg_distance: 2.1,
is_read: false,
}
}
Loading

0 comments on commit d474cc9

Please sign in to comment.