-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f305c15
commit ef3e0bc
Showing
6 changed files
with
144 additions
and
19 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
composeApp/src/commonMain/kotlin/com/clipevery/net/ClientHandlerManager.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 |
---|---|---|
@@ -1,10 +1,16 @@ | ||
package com.clipevery.net | ||
|
||
interface ClientHandlerManager { | ||
|
||
fun start() | ||
|
||
fun addHandler(id: String) | ||
|
||
fun removeHandler(id: String) | ||
|
||
fun stop() | ||
|
||
suspend fun checkConnects(checkAction: CheckAction) | ||
|
||
suspend fun checkConnect(id: String, checkAction: CheckAction): Boolean | ||
} |
7 changes: 6 additions & 1 deletion
7
composeApp/src/commonMain/kotlin/com/clipevery/net/DeviceRefresher.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 |
---|---|---|
@@ -1,5 +1,10 @@ | ||
package com.clipevery.net | ||
|
||
import androidx.compose.runtime.State | ||
|
||
interface DeviceRefresher { | ||
suspend fun refresh(checkAction: CheckAction) | ||
|
||
val isRefreshing: State<Boolean> | ||
|
||
fun refresh(checkAction: CheckAction) | ||
} |
95 changes: 90 additions & 5 deletions
95
composeApp/src/commonMain/kotlin/com/clipevery/ui/devices/DevicesView.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 |
---|---|---|
@@ -1,38 +1,123 @@ | ||
package com.clipevery.ui.devices | ||
|
||
import androidx.compose.animation.core.Animatable | ||
import androidx.compose.animation.core.LinearEasing | ||
import androidx.compose.animation.core.tween | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.size | ||
import androidx.compose.foundation.shape.CircleShape | ||
import androidx.compose.material.Divider | ||
import androidx.compose.material.Icon | ||
import androidx.compose.material.MaterialTheme | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.outlined.Refresh | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.MutableState | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.graphics.graphicsLayer | ||
import androidx.compose.ui.unit.dp | ||
import com.clipevery.LocalKoinApplication | ||
import com.clipevery.dao.sync.SyncRuntimeInfo | ||
import com.clipevery.dao.sync.SyncRuntimeInfoDao | ||
import com.clipevery.net.CheckAction | ||
import com.clipevery.net.DeviceRefresher | ||
import com.clipevery.ui.PageViewContext | ||
import com.clipevery.ui.base.ClipIconButton | ||
import kotlinx.coroutines.flow.asFlow | ||
import kotlinx.coroutines.flow.toList | ||
|
||
@Composable | ||
fun DevicesView(currentPageViewContext: MutableState<PageViewContext>) { | ||
Box(contentAlignment = Alignment.TopCenter) { | ||
DevicesListView(currentPageViewContext) | ||
DevicesRefreshView() | ||
} | ||
} | ||
|
||
@Composable | ||
fun DevicesListView(currentPageViewContext: MutableState<PageViewContext>) { | ||
val current = LocalKoinApplication.current | ||
val syncRuntimeInfoDao = current.koin.get<SyncRuntimeInfoDao>() | ||
val syncRuntimeInfosFlow = syncRuntimeInfoDao.getAllSyncRuntimeInfos().asFlow() | ||
|
||
var rememberSyncRuntimeInfos by remember { mutableStateOf(emptyList<SyncRuntimeInfo>()) } | ||
|
||
LaunchedEffect(syncRuntimeInfosFlow) { | ||
rememberSyncRuntimeInfos = syncRuntimeInfosFlow.toList() | ||
} | ||
Column { | ||
for ((index, syncRuntimeInfo) in rememberSyncRuntimeInfos.withIndex()) { | ||
DeviceItemView(syncRuntimeInfo, currentPageViewContext) | ||
if (index != rememberSyncRuntimeInfos.size - 1) { | ||
Divider(modifier = Modifier.fillMaxWidth()) | ||
} | ||
} | ||
} | ||
} | ||
|
||
for ((index, syncRuntimeInfo) in rememberSyncRuntimeInfos.withIndex()) { | ||
DeviceItemView(syncRuntimeInfo, currentPageViewContext) | ||
if (index != rememberSyncRuntimeInfos.size - 1) { | ||
Divider(modifier = Modifier.fillMaxWidth()) | ||
@Composable | ||
fun DevicesRefreshView() { | ||
val current = LocalKoinApplication.current | ||
val deviceRefresher = current.koin.get<DeviceRefresher>() | ||
|
||
val isRefreshing by deviceRefresher.isRefreshing | ||
|
||
val rotationDegrees = remember { Animatable(0f) } | ||
|
||
LaunchedEffect(isRefreshing) { | ||
while (isRefreshing) { | ||
rotationDegrees.animateTo( | ||
targetValue = rotationDegrees.value + 360, | ||
animationSpec = tween( | ||
durationMillis = 1000, | ||
easing = LinearEasing | ||
) | ||
) | ||
} | ||
rotationDegrees.snapTo(0f) | ||
} | ||
|
||
Column(modifier = Modifier.fillMaxSize(), | ||
verticalArrangement = Arrangement.Bottom) { | ||
Row(modifier = Modifier.fillMaxWidth(), | ||
horizontalArrangement = Arrangement.End) { | ||
ClipIconButton( | ||
radius = 18.dp, | ||
onClick = { | ||
if (!isRefreshing) { | ||
deviceRefresher.refresh(CheckAction.CheckAll) | ||
} | ||
}, | ||
modifier = Modifier | ||
.padding(30.dp) | ||
.background( | ||
MaterialTheme.colors.primary, | ||
CircleShape | ||
) | ||
) { | ||
Icon( | ||
Icons.Outlined.Refresh, | ||
contentDescription = "info", | ||
modifier = Modifier.padding(3.dp) | ||
.size(25.dp) | ||
.graphicsLayer(rotationZ = rotationDegrees.value ), | ||
tint = Color.White | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
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
28 changes: 24 additions & 4 deletions
28
composeApp/src/desktopMain/kotlin/com/clipevery/net/DesktopDeviceRefresher.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 |
---|---|---|
@@ -1,13 +1,33 @@ | ||
package com.clipevery.net | ||
|
||
import androidx.compose.runtime.State | ||
import androidx.compose.runtime.mutableStateOf | ||
import com.clipevery.utils.ioDispatcher | ||
import kotlinx.coroutines.withContext | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.launch | ||
|
||
class DesktopDeviceRefresher(private val clientHandlerManager: ClientHandlerManager): DeviceRefresher { | ||
|
||
override suspend fun refresh(checkAction: CheckAction) { | ||
withContext(ioDispatcher) { | ||
clientHandlerManager.checkConnects(checkAction) | ||
val logger = KotlinLogging.logger {} | ||
|
||
private var _refreshing = mutableStateOf(false) | ||
|
||
override val isRefreshing: State<Boolean> get() = _refreshing | ||
|
||
override fun refresh(checkAction: CheckAction) { | ||
_refreshing.value = true | ||
CoroutineScope(ioDispatcher).launch { | ||
logger.info { "start launch" } | ||
try { | ||
clientHandlerManager.checkConnects(checkAction) | ||
} catch (e: Exception) { | ||
logger.error(e) { "checkConnects error" } | ||
} | ||
delay(1000) | ||
logger.info { "set refreshing false" } | ||
_refreshing.value = false | ||
} | ||
} | ||
} |
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