|
|
|
|
@ -1,19 +1,27 @@
|
|
|
|
|
package com.utun.chat |
|
|
|
|
|
|
|
|
|
import android.content.Intent |
|
|
|
|
import android.os.Bundle |
|
|
|
|
import androidx.activity.ComponentActivity |
|
|
|
|
import androidx.activity.compose.BackHandler |
|
|
|
|
import androidx.activity.compose.setContent |
|
|
|
|
import androidx.compose.foundation.layout.Box |
|
|
|
|
import androidx.compose.foundation.layout.fillMaxSize |
|
|
|
|
import androidx.compose.material3.SnackbarHost |
|
|
|
|
import androidx.compose.material3.SnackbarHostState |
|
|
|
|
import androidx.compose.runtime.* |
|
|
|
|
import androidx.compose.ui.Alignment |
|
|
|
|
import androidx.compose.ui.Modifier |
|
|
|
|
import androidx.core.content.ContextCompat |
|
|
|
|
import androidx.lifecycle.viewmodel.compose.viewModel |
|
|
|
|
import com.utun.chat.data.Channel |
|
|
|
|
import com.utun.chat.data.InviteData |
|
|
|
|
import com.utun.chat.data.LogManager |
|
|
|
|
import com.utun.chat.data.NativeLib |
|
|
|
|
import com.utun.chat.headless.HeadlessService |
|
|
|
|
import com.utun.chat.ui.screens.* |
|
|
|
|
import com.utun.chat.viewmodel.ChatViewModel |
|
|
|
|
import android.content.Intent |
|
|
|
|
import kotlinx.coroutines.launch |
|
|
|
|
|
|
|
|
|
class MainActivity : ComponentActivity() { |
|
|
|
|
enum class Screen { ChannelList, Chat, MemberList, Settings, Logs, QrScan } |
|
|
|
|
@ -32,6 +40,23 @@ class MainActivity : ComponentActivity() {
|
|
|
|
|
var showJoinDialog by remember { mutableStateOf(false) } |
|
|
|
|
var joinAutoLink by remember { mutableStateOf<String?>(null) } |
|
|
|
|
var inviteLinkText by remember { mutableStateOf<String?>(null) } |
|
|
|
|
val snackbarHostState = remember { SnackbarHostState() } |
|
|
|
|
val scope = rememberCoroutineScope() |
|
|
|
|
|
|
|
|
|
val showInvite: (Channel) -> Unit = { ch -> |
|
|
|
|
val chId = ch.id.toLongOrNull() |
|
|
|
|
if (chId == null) { |
|
|
|
|
LogManager.addLog("ERROR", "INVITE", "channel id is not numeric: ${ch.id}") |
|
|
|
|
scope.launch { snackbarHostState.showSnackbar("Invalid channel id") } |
|
|
|
|
} else { |
|
|
|
|
val link = NativeLib.encodeInviteLink(chId) |
|
|
|
|
if (link != null) inviteLinkText = link |
|
|
|
|
else { |
|
|
|
|
LogManager.addLog("ERROR", "INVITE", "encodeInviteLink returned null for ch=$chId") |
|
|
|
|
scope.launch { snackbarHostState.showSnackbar("Could not generate invite link") } |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
BackHandler(enabled = screen != Screen.ChannelList) { |
|
|
|
|
when (screen) { |
|
|
|
|
@ -48,6 +73,7 @@ class MainActivity : ComponentActivity() {
|
|
|
|
|
onDispose { NativeLib.subscribeNativeLogs(false) } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Box(modifier = Modifier.fillMaxSize()) { |
|
|
|
|
when (screen) { |
|
|
|
|
Screen.ChannelList -> { |
|
|
|
|
val channels by vm.channels.collectAsState() |
|
|
|
|
@ -59,16 +85,7 @@ class MainActivity : ComponentActivity() {
|
|
|
|
|
onSelectChannel = { ch -> vm.selectChannel(ch); screen = Screen.Chat }, |
|
|
|
|
onCreateChannel = { vm.createChannel("general", "channel_1") }, |
|
|
|
|
onJoinChannel = { showJoinDialog = true; joinAutoLink = null }, |
|
|
|
|
onInviteClick = { ch -> |
|
|
|
|
val chId = ch.id.toLongOrNull() |
|
|
|
|
if (chId == null) { |
|
|
|
|
LogManager.addLog("ERROR", "INVITE", "channel id is not numeric: ${ch.id}") |
|
|
|
|
} else { |
|
|
|
|
val link = NativeLib.encodeInviteLink(chId) |
|
|
|
|
if (link != null) inviteLinkText = link |
|
|
|
|
else LogManager.addLog("ERROR", "INVITE", "encodeInviteLink returned null for ch=$chId") |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
onInviteClick = showInvite, |
|
|
|
|
onSettings = { screen = Screen.Settings }, |
|
|
|
|
onLogs = { screen = Screen.Logs }, |
|
|
|
|
onRefresh = { vm.requestRefresh() } |
|
|
|
|
@ -85,6 +102,7 @@ class MainActivity : ComponentActivity() {
|
|
|
|
|
viewModel = vm, |
|
|
|
|
onSendMessage = { vm.sendMessage(it) }, |
|
|
|
|
onMembersClick = { screen = Screen.MemberList }, |
|
|
|
|
onInvite = { showInvite(channel) }, |
|
|
|
|
onBack = { screen = Screen.ChannelList } |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
@ -115,6 +133,8 @@ class MainActivity : ComponentActivity() {
|
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
SnackbarHost(snackbarHostState, Modifier.align(Alignment.BottomCenter)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* Show join dialog after QR scan returns */ |
|
|
|
|
LaunchedEffect(qrResult) { |
|
|
|
|
|