Skip to content

Commit

Permalink
Update selfTestUtils.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
lhwdev committed Mar 5, 2022
1 parent 79672f1 commit 3f85daf
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class AlarmReceiver : BroadcastReceiver() {
val session = selfTestSession(context)

runBlocking { // TODO: is this okay?
context.submitSuspend(session)
context.submitSuspend(session, manual = false)
}

val pref = PreferenceState(context.prefMain())
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/lhwdev/selfTestMacro/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class MainActivity : AppCompatActivity() {
} else {
time.text = "시간 예약 안 됨"
}
updateTime(intent)
updateTime(intent, reset = true)
}

fun pickTime() {
Expand Down Expand Up @@ -257,7 +257,7 @@ class MainActivity : AppCompatActivity() {

submit.setOnClickListener {
lifecycleScope.launch {
submitSuspend(session, false)
submitSuspend(session, false, manual = true)
updateCurrentState()
}
}
Expand Down
28 changes: 20 additions & 8 deletions app/src/main/java/com/lhwdev/selfTestMacro/selfTestUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ suspend fun Context.surveyData(user: User, usersIdentifier: UserIdentifier): Sur
)
}

suspend fun Context.submitSuspend(session: Session, notification: Boolean = true) {
suspend fun Context.submitSuspend(session: Session, notification: Boolean = true, manual: Boolean) {
val pref = preferenceState
selfLog("submitSuspend ${pref.user?.identifier?.mainUserName}")

Expand Down Expand Up @@ -101,7 +101,9 @@ suspend fun Context.submitSuspend(session: Session, notification: Boolean = true
surveyData
)

pref.lastSubmit = Date().time
if(!manual) {
pref.lastSubmit = System.currentTimeMillis()
}

selfLog("submitSuspend success ${pref.user?.identifier?.mainUserName} ${result.registerAt}", force = true)

Expand All @@ -116,15 +118,23 @@ suspend fun Context.submitSuspend(session: Session, notification: Boolean = true
}
}

fun Context.updateTime(intent: PendingIntent) {
fun Context.updateTime(intent: PendingIntent, reset: Boolean = false) {
val preferenceState = preferenceState
val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
if(Build.VERSION.SDK_INT >= 21)
selfLog("updateTime: lastAlarm=${alarmManager.nextAlarmClock}")

alarmManager.cancel(intent)
if(preferenceState.isSchedulingEnabled)
scheduleNextAlarm(intent, preferenceState.hour, preferenceState.min, isRandom = preferenceState.isRandomEnabled)
if(preferenceState.isSchedulingEnabled) {
scheduleNextAlarm(
intent = intent,
hour = preferenceState.hour, min = preferenceState.min,
isRandom = preferenceState.isRandomEnabled,
nextDay = false
)

if(reset) preferenceState.lastSubmit = -1
}
}

private val random = Random
Expand All @@ -139,7 +149,7 @@ fun Context.scheduleNextAlarm(
hour: Int,
min: Int,
isRandom: Boolean,
nextDay: Boolean = false,
nextDay: Boolean
) {
val pref = preferenceState
val currentTime: Long
Expand All @@ -164,7 +174,8 @@ fun Context.scheduleNextAlarm(
return
}

if(nextDay || lastDay == millisToDaysCumulative(new.timeInMillis)) {
val newDay = millisToDaysCumulative(new.timeInMillis)
if(nextDay || lastDay == newDay) {
new.add(Calendar.DAY_OF_YEAR, 1)
}

Expand All @@ -189,11 +200,12 @@ fun Context.scheduleNextAlarm(
new[Calendar.MINUTE] = min
new[Calendar.SECOND] = 0
new[Calendar.MILLISECOND] = 0
selfLog("schedule time selection (nextDay=$nextDay lastDay=$lastDay, current=$newDay)")
new
}.timeInMillis

if(isRandom) {
newTime += 1000 * 60 * (Random.nextFloat() * 5).toInt()
newTime += 1000 * 60 * (Random.nextFloat() * 10 - 5).toInt()
if(newTime - currentTime < 10000) {
selfLog("scheduling: coerced time from $newTime")
newTime = currentTime + 10000
Expand Down
5 changes: 0 additions & 5 deletions app/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,5 @@
android:layout_height="wrap_content"
android:text="지금 자가진단 제출하기"
tools:ignore="HardcodedText" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="이 앱을 통해 자가진단을 이미 제출한 경우, 그 날 예약이 별개로 실행되지 않습니다." />
</LinearLayout>
</ScrollView>

0 comments on commit 3f85daf

Please sign in to comment.