Skip to content

Commit

Permalink
WIP: unread message badges and more notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
zoff99 committed Dec 12, 2023
1 parent d10f29b commit 69d34d9
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ static incoming_group_file_meta_data handle_incoming_group_file(String msg_id_ha
m.tox_group_peer_pubkey = tox_peerpk;
m.direction = TRIFA_MSG_DIRECTION.TRIFA_MSG_DIRECTION_RECVD.value;
m.TOX_MESSAGE_TYPE = 0;
m.read = false;
m.read = true;
m.tox_group_peername = peername;
m.private_message = 0;
m.group_identifier = group_id.toLowerCase();
Expand Down
18 changes: 18 additions & 0 deletions src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,7 @@ fun App()
UiMode.CONTACTS ->
{
contactstore.visible(true)
groupstore.visible(false)
val focusRequester = remember { FocusRequester() }
Row(modifier = Modifier.fillMaxWidth()) {
val contacts by contactstore.stateFlow.collectAsState()
Expand All @@ -983,6 +984,7 @@ fun App()
{
Log.i(TAG, "CONTACTS -> draw")
load_messages_for_friend(contacts.selectedContactPubkey)
GlobalScope.launch { globalstore.try_clear_unread_message_count() }
ChatAppWithScaffold(focusRequester = focusRequester, contactList = contacts, ui_scale = ui_scale)
LaunchedEffect(contacts.selectedContactPubkey) {
focusRequester.requestFocus()
Expand All @@ -993,6 +995,7 @@ fun App()
UiMode.GROUPS ->
{
contactstore.visible(false)
groupstore.visible(true)
val groupfocusRequester = remember { FocusRequester() }
val groups by groupstore.stateFlow.collectAsState()
val grouppeers by grouppeerstore.stateFlow.collectAsState()
Expand All @@ -1012,6 +1015,7 @@ fun App()
} else
{
load_groupmessages_for_friend(groups.selectedGroupId)
GlobalScope.launch { globalstore.try_clear_unread_group_message_count() }
GroupAppWithScaffold(focusRequester = groupfocusRequester, groupList = groups, ui_scale = ui_scale)
LaunchedEffect(groups.selectedGroupId) {
groupfocusRequester.requestFocus()
Expand All @@ -1021,24 +1025,29 @@ fun App()
}
UiMode.ADDFRIEND -> {
contactstore.visible(false)
groupstore.visible(false)
if (tox_running_state == "running") AddFriend()
else ExplainerToxNotRunning()
}
UiMode.ADDGROUP -> {
contactstore.visible(false)
groupstore.visible(false)
if (tox_running_state == "running") AddGroup()
else ExplainerToxNotRunning()
}
UiMode.SETTINGS -> {
contactstore.visible(false)
groupstore.visible(false)
SettingDetails()
}
UiMode.ABOUT -> {
contactstore.visible(false)
groupstore.visible(false)
AboutScreen()
}
else -> {
contactstore.visible(false)
groupstore.visible(false)
UiPlaceholder()
}
}
Expand Down Expand Up @@ -1072,6 +1081,10 @@ fun load_messages_for_friend(selectedContactPubkey: String?)
try
{
val toxpk = selectedContactPubkey.uppercase()
try {
orma!!.updateMessage().tox_friendpubkeyEq(toxpk).read(true).execute()
} catch(_: Exception) {
}
val uimessages = ArrayList<UIMessage>()
val messages = orma!!.selectFromMessage().
tox_friendpubkeyEq(toxpk).orderBySent_timestampAsc().toList()
Expand Down Expand Up @@ -1118,6 +1131,11 @@ fun load_groupmessages_for_friend(selectedGroupId: String?)
try
{
val groupid = selectedGroupId.lowercase()
try {
orma!!.updateGroupMessage().group_identifierEq(selectedGroupId)
.read(true).execute()
} catch(_: Exception) {
}
val uigroupmessages = ArrayList<UIGroupMessage>()
val messages = orma!!.selectFromGroupMessage().group_identifierEq(selectedGroupId).orderBySent_timestampAsc().toList()
messages.forEach() { // 0 -> msg received, 1 -> msg sent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ fun CoroutineScope.createContactStore(): ContactStore
global_semaphore_contactlist_ui.release()
}
}

override fun visible(value: Boolean)
{
global_semaphore_contactlist_ui.acquire((Throwable().stackTrace[0].fileName + ":" + Throwable().stackTrace[0].lineNumber))
Expand Down
10 changes: 9 additions & 1 deletion src/main/kotlin/com/zoffcc/applications/trifa/GroupStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import kotlinx.coroutines.launch
import org.briarproject.briar.desktop.contact.ContactItem
import org.briarproject.briar.desktop.contact.GroupItem

data class StateGroups(val groups: List<GroupItem> = emptyList(), val selectedGroupId: String? = null, val selectedGroup: GroupItem? = null)
data class StateGroups(val groups: List<GroupItem> = emptyList(), val visible: Boolean = false, val selectedGroupId: String? = null, val selectedGroup: GroupItem? = null)

interface GroupStore
{
fun add(item: GroupItem)
fun remove(item: GroupItem)
fun select(pubkey: String?)
fun visible(value: Boolean)
fun clear()
fun update(item: GroupItem)
val stateFlow: StateFlow<StateGroups>
Expand Down Expand Up @@ -127,6 +128,13 @@ fun CoroutineScope.createGroupStore(): GroupStore
}
}

override fun visible(value: Boolean)
{
global_semaphore_grouplist_ui.acquire((Throwable().stackTrace[0].fileName + ":" + Throwable().stackTrace[0].lineNumber))
mutableStateFlow.value = state.copy(visible = value)
global_semaphore_grouplist_ui.release()
}

override fun update(item: GroupItem)
{
launch {
Expand Down
32 changes: 28 additions & 4 deletions src/main/kotlin/com/zoffcc/applications/trifa/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ import SnackBarToast
import UIGroupMessage
import UIMessage
import User
import androidx.compose.ui.platform.LocalWindowInfo
import avstatestore
import avstatestorecallstate
import avstatestorevcapfpsstate
import avstatestorevplayfpsstate
import com.notification.Notification
import com.zoffcc.applications.ffmpegav.AVActivity.ffmpegav_loadjni
import com.zoffcc.applications.jninotifications.NTFYActivity
import com.zoffcc.applications.jninotifications.NTFYActivity.jninotifications_loadjni
Expand Down Expand Up @@ -2630,7 +2628,11 @@ class MainActivity
m.tox_friendpubkey = toxpk
m.direction = TRIFAGlobals.TRIFA_MSG_DIRECTION.TRIFA_MSG_DIRECTION_RECVD.value // msg received
m.TOX_MESSAGE_TYPE = 0
m.read = true
m.read = false
if ((contactstore.state.visible) && (contactstore.state.selectedContactPubkey == toxpk))
{
m.read = true
}
m.TRIFA_MESSAGE_TYPE = TRIFA_MSG_TYPE.TRIFA_MSG_TYPE_TEXT.value
if (message_timestamp > 0)
{
Expand Down Expand Up @@ -2662,6 +2664,7 @@ class MainActivity
e2.printStackTrace()
}
HelperNotification.displayNotification("new Message" + fname)
globalstore.increase_unread_message_count()
}
} catch (e: Exception)
{
Expand All @@ -2677,7 +2680,7 @@ class MainActivity
m.tox_friendpubkey = toxpk
m.direction = TRIFAGlobals.TRIFA_MSG_DIRECTION.TRIFA_MSG_DIRECTION_SENT.value // msg sent
m.TOX_MESSAGE_TYPE = 0
m.read = false
m.read = true
m.TRIFA_MESSAGE_TYPE = TRIFA_MSG_TYPE.TRIFA_MSG_TYPE_TEXT.value
if (message_timestamp > 0)
{
Expand Down Expand Up @@ -2715,6 +2718,10 @@ class MainActivity
m.direction = TRIFAGlobals.TRIFA_MSG_DIRECTION.TRIFA_MSG_DIRECTION_RECVD.value // msg received
m.TOX_MESSAGE_TYPE = 0
m.read = false
if ((groupstore.state.visible) && (groupstore.state.selectedGroupId == groupid))
{
m.read = true
}
if (peername == null)
{
m.tox_group_peername = ""
Expand All @@ -2735,6 +2742,23 @@ class MainActivity
try
{
row_id = orma!!.insertIntoGroupMessage(m)
if ((!globalstore.isFocused()) || (globalstore.isMinimized()) || (!groupstore.state.visible) || (groupstore.state.selectedGroupId != groupid))
{
var grptitle: String? = ""
try
{
val group_title = tox_group_get_name(groupnum)
if ((group_title != null) && (group_title.length > 0))
{
grptitle = " in " + group_title
}
}
catch(_: Exception)
{
}
HelperNotification.displayNotification("new Group Message" + grptitle)
globalstore.increase_unread_group_message_count()
}
} catch (e: Exception)
{
}
Expand Down
18 changes: 18 additions & 0 deletions src/main/kotlin/com/zoffcc/applications/trifa/TrifaToxService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import com.zoffcc.applications.trifa.TRIFAGlobals.USE_MAX_NUMBER_OF_BOOTSTRAP_NO
import com.zoffcc.applications.trifa.TRIFAGlobals.USE_MAX_NUMBER_OF_BOOTSTRAP_TCP_RELAYS
import com.zoffcc.applications.trifa.TRIFAGlobals.global_last_activity_outgoung_ft_ts
import contactstore
import globalstore
import grouppeerstore
import groupstore
import online_button_text_wrapper
Expand All @@ -63,6 +64,15 @@ class TrifaToxService
// ------ correct startup order ------
orma = com.zoffcc.applications.sorm.OrmaDatabase()
load_db_prefs()
try {
globalstore.try_clear_unread_message_count()
} catch(_: Exception) {
}

try {
globalstore.try_clear_unread_group_message_count()
} catch(_: Exception) {
}
val old_is_tox_started = is_tox_started
Log.i(TAG, "is_tox_started:==============================")
Log.i(TAG, "is_tox_started=" + is_tox_started)
Expand Down Expand Up @@ -181,6 +191,14 @@ class TrifaToxService
set_tox_running_state("stopped")
clear_friends()
clear_groups()
try {
globalstore.hard_clear_unread_message_count()
} catch(_: Exception) {
}
try {
globalstore.hard_clear_unread_group_message_count()
} catch(_: Exception) {
}
orma = null
com.zoffcc.applications.sorm.OrmaDatabase.shutdown()
unlock_data_dir_input()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,24 @@ import java.io.File

data class globalstore_state(
val mainwindow_minimized: Boolean = false,
val mainwindow_focused: Boolean = true
val mainwindow_focused: Boolean = true,
val contacts_unread_message_count: Int = 0,
val contacts_unread_group_message_count: Int = 0
)

interface GlobalStore {
fun updateMinimized(value: Boolean)
fun updateFocused(value: Boolean)
fun isMinimized(): Boolean
fun isFocused(): Boolean
fun increase_unread_message_count()
fun get_unread_message_count(): Int
fun try_clear_unread_message_count()
fun hard_clear_unread_message_count()
fun increase_unread_group_message_count()
fun get_unread_group_message_count(): Int
fun try_clear_unread_group_message_count()
fun hard_clear_unread_group_message_count()
val stateFlow: StateFlow<globalstore_state>
val state get() = stateFlow.value
}
Expand Down Expand Up @@ -51,6 +61,67 @@ fun CoroutineScope.createGlobalStore(): GlobalStore {
{
return state.mainwindow_focused
}
override fun increase_unread_message_count()
{
mutableStateFlow.value = state.copy(contacts_unread_message_count = (state.contacts_unread_message_count + 1))
}
override fun get_unread_message_count(): Int
{
return state.contacts_unread_message_count
}

override fun try_clear_unread_message_count()
{
var unread_count = 0
try
{
unread_count = TrifaToxService.orma!!.selectFromMessage()
.directionEq(TRIFAGlobals.TRIFA_MSG_DIRECTION.TRIFA_MSG_DIRECTION_RECVD.value)
.readEq(false).count()
Log.i(TAG, "try_clear_unread_message_count:unread_count=" + unread_count)
}
catch (e: Exception)
{
e.printStackTrace()
}
mutableStateFlow.value = state.copy(contacts_unread_message_count = unread_count)
}

override fun hard_clear_unread_message_count()
{
mutableStateFlow.value = state.copy(contacts_unread_message_count = 0)
}

override fun increase_unread_group_message_count()
{
mutableStateFlow.value = state.copy(contacts_unread_group_message_count = (state.contacts_unread_group_message_count + 1))
}
override fun get_unread_group_message_count(): Int
{
return state.contacts_unread_group_message_count
}

override fun try_clear_unread_group_message_count()
{
var unread_count = 0
try
{
unread_count = TrifaToxService.orma!!.selectFromGroupMessage()
.directionEq(TRIFAGlobals.TRIFA_MSG_DIRECTION.TRIFA_MSG_DIRECTION_RECVD.value)
.readEq(false).count()
Log.i(TAG, "try_clear_unread_group_message_count:unread_count=" + unread_count)
}
catch (e: Exception)
{
e.printStackTrace()
}
mutableStateFlow.value = state.copy(contacts_unread_group_message_count = unread_count)
}

override fun hard_clear_unread_group_message_count()
{
mutableStateFlow.value = state.copy(contacts_unread_group_message_count = 0)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import androidx.compose.ui.unit.dp
import com.zoffcc.applications.trifa.HelperGeneric.delete_friend_wrapper
import com.zoffcc.applications.trifa.StateContacts
import contactstore
import globalstore
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -46,7 +47,10 @@ fun ContactList(
contentType = { item -> item::class }
) { item ->
ListItemView(
onSelect = { contactstore.select(item.pubkey) },
onSelect = {
GlobalScope.launch { globalstore.try_clear_unread_message_count() }
contactstore.select(item.pubkey)
},
selected = (contactList.selectedContactPubkey == item.pubkey)
) {
val modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ import com.zoffcc.applications.trifa.HelperGeneric
import com.zoffcc.applications.trifa.StateContacts
import com.zoffcc.applications.trifa.StateGroups
import contactstore
import globalstore
import groupstore
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.briarproject.briar.desktop.ui.ListItemView
import org.briarproject.briar.desktop.ui.VerticallyScrollableArea
import org.briarproject.briar.desktop.utils.InternationalizationUtils.i18n

@OptIn(DelicateCoroutinesApi::class)
@Composable
fun GroupList(
groupList: StateGroups,
Expand All @@ -48,7 +51,10 @@ fun GroupList(
contentType = { item -> item::class }
) { item ->
ListItemView(
onSelect = { groupstore.select(item.groupId) },
onSelect = {
GlobalScope.launch { globalstore.try_clear_unread_group_message_count() }
groupstore.select(item.groupId)
},
selected = (groupList.selectedGroupId == item.groupId)
) {
val modifier = Modifier
Expand Down
Loading

0 comments on commit 69d34d9

Please sign in to comment.