Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Auto-refresh pings state #32

Merged
merged 2 commits into from
Apr 5, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions app/src/main/java/tech/relaycorp/ping/common/FlowUtils.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package tech.relaycorp.ping.common

import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import java.util.*
import kotlin.time.Duration

fun <T> PublishFlow() = BroadcastChannel<T>(1)

Expand All @@ -14,3 +13,10 @@ fun <T> Flow<Optional<T>>.element(): Flow<T> =
if (it.isPresent) flowOf(it.get()!!) else emptyFlow()
}

fun flowInterval(repeatMillis: Duration) =
flow {
while (true) {
emit(Unit)
delay(repeatMillis)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tech.relaycorp.ping.common.di

import dagger.Subcomponent
import tech.relaycorp.gateway.common.di.PerActivity
import tech.relaycorp.ping.ui.main.MainActivity
import tech.relaycorp.ping.ui.peers.AddPublicPeerActivity
import tech.relaycorp.ping.ui.peers.PeerActivity
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tech.relaycorp.gateway.common.di
package tech.relaycorp.ping.common.di

import javax.inject.Scope

Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/tech/relaycorp/ping/domain/GetPing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ class GetPing
private val pingDao: PingDao
) {
fun get(pingId: String) =
pingDao
.getPublic(pingId)
pingDao.getPublic(pingId)
.filterNotNull()
.map { it.toModel() }
}
8 changes: 5 additions & 3 deletions app/src/main/java/tech/relaycorp/ping/domain/ObservePings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ class ObservePings
@Inject constructor(
private val pingDao: PingDao
) {
fun observe() = pingDao.listPublic().map { list ->
list.map { it.toModel() }
}
fun observe() =
pingDao.listPublic()
.map { list ->
list.map { it.toModel() }
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/tech/relaycorp/ping/ui/BaseActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import dev.chrisbanes.insetter.applyInsetter
import kotlinx.android.synthetic.main.common_app_bar.*
import kotlinx.coroutines.channels.sendBlocking
import kotlinx.coroutines.flow.asFlow
import tech.relaycorp.gateway.ui.common.MessageManager
import tech.relaycorp.ping.ui.common.MessageManager
import tech.relaycorp.ping.App
import tech.relaycorp.ping.R
import tech.relaycorp.ping.common.PublishFlow
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/tech/relaycorp/ping/ui/BaseView.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package tech.relaycorp.ping.ui

import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.widget.FrameLayout
import androidx.annotation.AttrRes
import androidx.annotation.StyleRes
import tech.relaycorp.gateway.ui.common.MessageManager
import tech.relaycorp.ping.ui.common.MessageManager

open class BaseView
@JvmOverloads
Expand All @@ -17,5 +16,6 @@ constructor(
@StyleRes defStyleRes: Int = 0
) : FrameLayout(context, attrs, defStyleAttr, defStyleRes) {

protected val messageManager by lazy { MessageManager(context as Activity) }
protected val activity get() = context as BaseActivity
protected val messageManager by lazy { MessageManager(activity) }
}
2 changes: 1 addition & 1 deletion app/src/main/java/tech/relaycorp/ping/ui/BaseViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tech.relaycorp.gateway.ui
package tech.relaycorp.ping.ui

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tech.relaycorp.gateway.ui.common
package tech.relaycorp.ping.ui.common

import android.app.Activity
import android.view.View
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package tech.relaycorp.ping.ui.common.loading
import android.app.Activity
import android.app.AlertDialog
import android.view.WindowManager
import tech.relaycorp.gateway.common.di.PerActivity
import tech.relaycorp.ping.R
import tech.relaycorp.ping.common.di.PerActivity
import javax.inject.Inject

@PerActivity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import tech.relaycorp.gateway.ui.BaseViewModel
import tech.relaycorp.ping.ui.BaseViewModel
import tech.relaycorp.ping.domain.ObservePings
import tech.relaycorp.ping.domain.model.Ping
import javax.inject.Inject
Expand Down
30 changes: 26 additions & 4 deletions app/src/main/java/tech/relaycorp/ping/ui/main/PingItemView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ package tech.relaycorp.ping.ui.main

import android.content.Context
import android.util.AttributeSet
import androidx.lifecycle.lifecycleScope
import com.airbnb.epoxy.ModelProp
import com.airbnb.epoxy.ModelView
import kotlinx.android.synthetic.main.item_ping.view.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import tech.relaycorp.ping.R
import tech.relaycorp.ping.common.flowInterval
import tech.relaycorp.ping.domain.model.Ping
import tech.relaycorp.ping.ui.BaseView
import tech.relaycorp.ping.ui.common.DateTimeFormat
import tech.relaycorp.ping.ui.ping.PingActivity
import kotlin.time.seconds

@ModelView(autoLayout = ModelView.Size.MATCH_WIDTH_WRAP_HEIGHT)
class PingItemView
Expand All @@ -24,9 +30,22 @@ constructor(
inflate(context, R.layout.item_ping, this)
}

private var refreshScope: CoroutineScope? = null

@ModelProp
fun setItem(item: Ping) {
recipient.text = item.peer.alias

refresh { setRefreshableState(item) }

setOnClickListener {
context.startActivity(
PingActivity.getIntent(context, pingId = item.pingId)
)
}
}

private fun setRefreshableState(item: Ping) {
sentAt.text = DateTimeFormat.format(item.sentAt)

state.setImageResource(
Expand All @@ -43,11 +62,14 @@ constructor(
Ping.State.Expired -> R.string.ping_expired
}
)
}

setOnClickListener {
context.startActivity(
PingActivity.getIntent(context, pingId = item.pingId)
)
private fun refresh(action: () -> Unit) {
refreshScope?.cancel()
refreshScope = (activity.lifecycleScope.plus(SupervisorJob())).also { refreshScope ->
flowInterval(10.seconds)
.onEach { action() }
.launchIn(refreshScope)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package tech.relaycorp.ping.ui.peers

import android.net.Uri
import kotlinx.coroutines.flow.*
import tech.relaycorp.gateway.ui.BaseViewModel
import tech.relaycorp.ping.ui.BaseViewModel
import tech.relaycorp.ping.common.PublishFlow
import tech.relaycorp.ping.common.element
import tech.relaycorp.ping.data.ReadFile
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package tech.relaycorp.ping.ui.peers

import kotlinx.coroutines.flow.*
import tech.relaycorp.gateway.ui.BaseViewModel
import tech.relaycorp.ping.ui.BaseViewModel
import tech.relaycorp.ping.common.PublishFlow
import tech.relaycorp.ping.common.element
import tech.relaycorp.ping.domain.DeletePeer
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package tech.relaycorp.ping.ui.peers

import kotlinx.coroutines.flow.*
import tech.relaycorp.gateway.ui.BaseViewModel
import tech.relaycorp.ping.ui.BaseViewModel
import tech.relaycorp.ping.domain.ObservePeers
import tech.relaycorp.ping.domain.model.Peer
import javax.inject.Inject
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package tech.relaycorp.ping.ui.ping

import kotlinx.coroutines.flow.*
import tech.relaycorp.gateway.ui.BaseViewModel
import tech.relaycorp.ping.ui.BaseViewModel
import tech.relaycorp.ping.common.element
import tech.relaycorp.ping.common.flowInterval
import tech.relaycorp.ping.domain.GetPing
import tech.relaycorp.ping.domain.model.Ping
import java.util.*
import javax.inject.Inject
import kotlin.time.seconds

class PingViewModel
@Inject constructor(
Expand All @@ -24,6 +26,7 @@ class PingViewModel
init {
pingId
.element()
.flatMapLatest { pingId -> flowInterval(10.seconds).map { pingId } } // Refresh display
.flatMapLatest(getPing::get)
.onEach { _ping.value = Optional.of(it) }
.launchIn(backgroundScope)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package tech.relaycorp.ping.ui.ping
import kotlinx.coroutines.channels.sendBlocking
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import tech.relaycorp.gateway.ui.BaseViewModel
import tech.relaycorp.ping.ui.BaseViewModel
import tech.relaycorp.ping.common.Logging.logger
import tech.relaycorp.ping.common.PublishFlow
import tech.relaycorp.ping.domain.GetDefaultPeer
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false"
android:paddingBottom="88dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:listitem="@layout/item_ping" />

Expand Down