This repository has been archived by the owner on Oct 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
[BM-667] Feedback - baby device side #263
Open
gabrysgab
wants to merge
1
commit into
BM-667_User_feedback
Choose a base branch
from
BM-667_feedback_request_preparation
base: BM-667_User_feedback
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
7 changes: 7 additions & 0 deletions
7
app/src/main/kotlin/co/netguru/baby/monitor/client/common/TimestampProvider.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,7 @@ | ||
package co.netguru.baby.monitor.client.common | ||
|
||
import javax.inject.Inject | ||
|
||
class TimestampProvider @Inject constructor() { | ||
fun timestamp() = System.currentTimeMillis() | ||
} |
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
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -1,6 +1,8 @@ | ||||
package co.netguru.baby.monitor.client.feature.babynotification | ||||
|
||||
import co.netguru.baby.monitor.client.feature.feedback.SavedRecordingDetails | ||||
import co.netguru.baby.monitor.client.feature.firebasenotification.FirebaseNotificationSender | ||||
import co.netguru.baby.monitor.client.feature.firebasenotification.NotificationData | ||||
import co.netguru.baby.monitor.client.feature.firebasenotification.NotificationType | ||||
import io.reactivex.Completable | ||||
import io.reactivex.schedulers.Schedulers | ||||
|
@@ -15,14 +17,30 @@ class NotifyBabyEventUseCase( | |||
private val babyEvents: PublishSubject<BabyEvent> = PublishSubject.create() | ||||
|
||||
private fun fetchClientsAndPostNotification(babyEvent: BabyEvent): Completable { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||
val (notificationTitle, notificationType) = when (babyEvent) { | ||||
BabyEvent.BabyCrying -> notificationTexts[CRY_TITLE_KEY] to NotificationType.CRY_NOTIFICATION | ||||
BabyEvent.NoiseDetected -> notificationTexts[NOISE_TITLE_KEY] to NotificationType.NOISE_NOTIFICATION | ||||
val notificationTitle = when (babyEvent) { | ||||
BabyEvent.BabyCrying, is BabyEvent.BabyCryingFeedback | ||||
-> notificationTexts[CRY_TITLE_KEY] | ||||
BabyEvent.NoiseDetected, is BabyEvent.NoiseDetectedFeedback | ||||
-> notificationTexts[NOISE_TITLE_KEY] | ||||
} | ||||
val notificationType = when (babyEvent) { | ||||
BabyEvent.BabyCrying -> NotificationType.CRY_NOTIFICATION | ||||
BabyEvent.NoiseDetected -> NotificationType.NOISE_NOTIFICATION | ||||
is BabyEvent.BabyCryingFeedback -> NotificationType.CRY_NOTIFICATION_WITH_FEEDBACK_REQUEST | ||||
is BabyEvent.NoiseDetectedFeedback -> NotificationType.NOISE_NOTIFICATION_WITH_FEEDBACK_REQUEST | ||||
} | ||||
val feedbackRecordingFile = when (babyEvent) { | ||||
is BabyEvent.BabyCryingFeedback -> babyEvent.recordingName | ||||
is BabyEvent.NoiseDetectedFeedback -> babyEvent.recordingName | ||||
else -> "" | ||||
} | ||||
return notificationSender.broadcastNotificationToFcm( | ||||
notificationTitle ?: error("Notification title missing"), | ||||
notificationTexts[NOTIFICATION_TEXT_KEY] ?: error("Notification text missing"), | ||||
notificationType | ||||
NotificationData( | ||||
notificationTitle ?: error("Notification title missing"), | ||||
notificationTexts[NOTIFICATION_TEXT_KEY] ?: error("Notification text missing"), | ||||
notificationType, | ||||
feedbackRecordingFile | ||||
) | ||||
) | ||||
} | ||||
|
||||
|
@@ -37,15 +55,33 @@ class NotifyBabyEventUseCase( | |||
.retry() | ||||
} | ||||
|
||||
fun notifyBabyCrying() = | ||||
babyEvents.onNext(BabyEvent.BabyCrying) | ||||
fun notifyBabyCrying(savedRecordingDetails: SavedRecordingDetails? = null) { | ||||
val babyEvent = savedRecordingDetails?.let { | ||||
if (it.shouldAskForFeedback) { | ||||
BabyEvent.BabyCryingFeedback(it.fileName) | ||||
} else { | ||||
null | ||||
} | ||||
} ?: BabyEvent.BabyCrying | ||||
babyEvents.onNext(babyEvent) | ||||
} | ||||
|
||||
fun notifyNoiseDetected() = | ||||
babyEvents.onNext(BabyEvent.NoiseDetected) | ||||
fun notifyNoiseDetected(savedRecordingDetails: SavedRecordingDetails? = null) { | ||||
val noiseEvent = savedRecordingDetails?.let { | ||||
if (it.shouldAskForFeedback) { | ||||
BabyEvent.NoiseDetectedFeedback(it.fileName) | ||||
} else { | ||||
null | ||||
} | ||||
} ?: BabyEvent.NoiseDetected | ||||
babyEvents.onNext(noiseEvent) | ||||
} | ||||
|
||||
private sealed class BabyEvent { | ||||
object BabyCrying : BabyEvent() | ||||
object NoiseDetected : BabyEvent() | ||||
data class BabyCryingFeedback(val recordingName: String) : BabyEvent() | ||||
data class NoiseDetectedFeedback(val recordingName: String) : BabyEvent() | ||||
} | ||||
|
||||
companion object { | ||||
|
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
43 changes: 43 additions & 0 deletions
43
app/src/main/kotlin/co/netguru/baby/monitor/client/feature/feedback/FeedbackController.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,43 @@ | ||
package co.netguru.baby.monitor.client.feature.feedback | ||
|
||
import co.netguru.baby.monitor.client.application.firebase.FirebaseSharedPreferencesWrapper | ||
import co.netguru.baby.monitor.client.feature.machinelearning.MachineLearning | ||
import co.netguru.baby.monitor.client.feature.recording.RecordingFileController | ||
import io.reactivex.Single | ||
import javax.inject.Inject | ||
|
||
class FeedbackController @Inject constructor( | ||
private val feedbackFrequencyUseCase: FeedbackFrequencyUseCase, | ||
private val firebaseSharedPreferencesWrapper: FirebaseSharedPreferencesWrapper, | ||
private val recordingFileController: RecordingFileController | ||
) { | ||
fun handleRecording( | ||
recordingData: ByteArray, | ||
fromMachineLearning: Boolean | ||
): Single<SavedRecordingDetails> { | ||
return if (userEnabledRecordingUpload()) { | ||
when { | ||
feedbackFrequencyUseCase.shouldAskForFeedback() -> { | ||
recordingFileController.saveRecording(recordingData) | ||
.map { SavedRecordingDetails(it, true) } | ||
} | ||
fromMachineLearning -> { | ||
recordingFileController.saveRecording(recordingData) | ||
.map { SavedRecordingDetails(it) } | ||
} | ||
else -> { | ||
Single.just(SavedRecordingDetails(RECORDING_NOT_SAVED)) | ||
} | ||
} | ||
} else { | ||
Single.just(SavedRecordingDetails(RECORDING_NOT_SAVED)) | ||
} | ||
} | ||
|
||
private fun userEnabledRecordingUpload() = firebaseSharedPreferencesWrapper.isUploadEnabled() | ||
|
||
companion object { | ||
const val DATA_SIZE = MachineLearning.DATA_SIZE | ||
const val RECORDING_NOT_SAVED = "" | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...c/main/kotlin/co/netguru/baby/monitor/client/feature/feedback/FeedbackFrequencyUseCase.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,61 @@ | ||
package co.netguru.baby.monitor.client.feature.feedback | ||
|
||
import android.content.SharedPreferences | ||
import co.netguru.baby.monitor.client.application.di.FeedbackPreferencesQualifier | ||
import co.netguru.baby.monitor.client.common.TimestampProvider | ||
import co.netguru.baby.monitor.client.common.extensions.edit | ||
import java.util.concurrent.TimeUnit | ||
import javax.inject.Inject | ||
|
||
class FeedbackFrequencyUseCase @Inject constructor( | ||
@FeedbackPreferencesQualifier | ||
private val sharedPreferences: SharedPreferences, | ||
private val timestampProvider: TimestampProvider | ||
) { | ||
fun shouldAskForFeedback(): Boolean { | ||
val currentCounter = sharedPreferences.getInt(NOTIFICATION_COUNTER_KEY, 0) | ||
val lastFeedbackTimestamp = sharedPreferences.getLong(FEEDBACK_TIMESTAMP_KEY, 0) | ||
return enoughTimePassed(lastFeedbackTimestamp) && currentCounter > NO_FEEDBACK_COUNTER_LIMIT | ||
} | ||
Comment on lines
+15
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if it could be modeled with rx. Something like this, perhaps?
|
||
|
||
fun notificationSent() { | ||
incrementCounter() | ||
} | ||
|
||
fun feedbackRequestSent() { | ||
updateTimestamp() | ||
clearNotificationCounter() | ||
} | ||
|
||
private fun enoughTimePassed(lastFeedbackTimestamp: Long) = | ||
timestampProvider.timestamp() > lastFeedbackTimestamp + TimeUnit.HOURS.toMillis( | ||
NO_FEEDBACK_REQUEST_HOURS | ||
) | ||
|
||
private fun updateTimestamp() { | ||
sharedPreferences.edit { | ||
putLong(FEEDBACK_TIMESTAMP_KEY, timestampProvider.timestamp()) | ||
} | ||
} | ||
|
||
private fun clearNotificationCounter() { | ||
sharedPreferences.edit { | ||
putInt(NOTIFICATION_COUNTER_KEY, CLEAR_COUNTER_VALUE) | ||
} | ||
} | ||
|
||
private fun incrementCounter() { | ||
val currentCounter = sharedPreferences.getInt(NOTIFICATION_COUNTER_KEY, 0) | ||
sharedPreferences.edit { | ||
putInt(NOTIFICATION_COUNTER_KEY, currentCounter + 1) | ||
} | ||
} | ||
|
||
companion object { | ||
internal const val FEEDBACK_TIMESTAMP_KEY = "feedback_timestamp" | ||
internal const val NOTIFICATION_COUNTER_KEY = "notification_counter" | ||
internal const val CLEAR_COUNTER_VALUE = 0 | ||
internal const val NO_FEEDBACK_REQUEST_HOURS = 3L | ||
internal const val NO_FEEDBACK_COUNTER_LIMIT = 3 | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
app/src/main/kotlin/co/netguru/baby/monitor/client/feature/feedback/SavedRecordingDetails.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,6 @@ | ||
package co.netguru.baby.monitor.client.feature.feedback | ||
|
||
data class SavedRecordingDetails( | ||
val fileName: String, | ||
val shouldAskForFeedback: Boolean = false | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.