Skip to content

Commit

Permalink
fix: handle hotspot disabled during sync (#85)
Browse files Browse the repository at this point in the history
Closes #79
  • Loading branch information
sdsantos authored May 25, 2020
1 parent 1f123ce commit 4cefc09
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package tech.relaycorp.courier.ui.sync.people
import kotlinx.coroutines.channels.sendBlocking
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.take
import kotlinx.coroutines.launch
import tech.relaycorp.courier.background.WifiHotspotState
import tech.relaycorp.courier.background.WifiHotspotStateReceiver
import tech.relaycorp.courier.common.BehaviorChannel
Expand Down Expand Up @@ -50,15 +50,30 @@ class PeopleSyncViewModel
private var hadFirstClient = false

init {
ioScope.launch {
when (getHotspotState()) {
WifiHotspotState.Enabled -> privateSync.startSync()
WifiHotspotState.Disabled -> {
openHotspotInstructions.send(Unit)
finish.send(Finish)
wifiHotspotStateReceiver
.state()
.take(1)
.onEach {
when (it) {
WifiHotspotState.Enabled -> privateSync.startSync()
WifiHotspotState.Disabled -> {
openHotspotInstructions.send(Unit)
finish.send(Finish)
}
}
}
}
.launchIn(ioScope)

wifiHotspotStateReceiver
.state()
.drop(1)
.filter { it == WifiHotspotState.Disabled }
.onEach {
privateSync.stopSync()
openHotspotInstructions.send(Unit)
finish.send(Finish)
}
.launchIn(ioScope)

privateSync
.clientsConnected()
Expand Down
17 changes: 13 additions & 4 deletions app/src/main/res/layout/activity_hotspot_instructions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="40dp"
android:gravity="center"
android:padding="32dp"
android:layout_marginBottom="48dp"
android:orientation="vertical"
android:padding="32dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<ImageView
Expand All @@ -33,8 +33,8 @@
android:id="@+id/titleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginBottom="32dp"
android:gravity="center"
android:textAppearance="@style/TextAppearance.Courier.Title"
android:textIsSelectable="true"
tools:text="@string/hotspot_instructions_disabled" />
Expand All @@ -43,12 +43,21 @@
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:layout_marginBottom="16dp"
android:gravity="center"
android:textAppearance="@style/TextAppearance.Courier.Body2"
android:textIsSelectable="true"
tools:text="@string/hotspot_instructions_disabled_text" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:gravity="center"
android:text="@string/hotspot_instructions_stay_enabled"
android:textAppearance="@style/TextAppearance.Courier.Body2"
android:textIsSelectable="true" />

<com.google.android.material.button.MaterialButton
android:id="@+id/openSettings"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@
Your Wi-Fi hotspot is now enabled.\nYou can start the Sync with People.
</string>
<string name="hotspot_instructions_start_sync">Start to Sync</string>
<string name="hotspot_instructions_stay_enabled">
The Wi-Fi hotspot should stay enabled throughout the synchronization with people.
</string>

<!-- Settings -->
<string name="settings">Settings</string>
Expand Down

0 comments on commit 4cefc09

Please sign in to comment.