Skip to content

Commit

Permalink
Fix friend request messages not always being logged
Browse files Browse the repository at this point in the history
They were missing if you accepted a friend request from the long-press
menu on a friend request in the contact list. This commit moves the
logic for adding it down one layer, so now it can't be missed unless you
skip over abstractions.
  • Loading branch information
robinlinden committed Dec 9, 2024
1 parent 286cc3f commit 338e544
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ class FriendRequestFragment : BaseFragment<FragmentFriendRequestBinding>(Fragmen

accept.setOnClickListener {
vm.accept(friendRequest)
vm.addToChatLog(friendRequest)
findNavController().popBackStack()
}

Expand Down
28 changes: 3 additions & 25 deletions atox/src/main/kotlin/ui/friendrequest/FriendRequestViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// SPDX-FileCopyrightText: 2020-2022 aTox contributors
// SPDX-FileCopyrightText: 2020-2024 Robin Lindén <[email protected]>
// SPDX-FileCopyrightText: 2022 aTox contributors
//
// SPDX-License-Identifier: GPL-3.0-only

Expand All @@ -7,36 +8,13 @@ package ltd.evilcorp.atox.ui.friendrequest
import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.asLiveData
import java.util.Date
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import ltd.evilcorp.core.repository.MessageRepository
import ltd.evilcorp.core.vo.FriendRequest
import ltd.evilcorp.core.vo.Message
import ltd.evilcorp.core.vo.MessageType
import ltd.evilcorp.core.vo.Sender
import ltd.evilcorp.domain.feature.FriendRequestManager
import ltd.evilcorp.domain.tox.PublicKey

class FriendRequestViewModel @Inject constructor(
private val scope: CoroutineScope,
private val friendRequests: FriendRequestManager,
private val messageRepository: MessageRepository,
) : ViewModel() {
class FriendRequestViewModel @Inject constructor(private val friendRequests: FriendRequestManager) : ViewModel() {
fun byId(pk: PublicKey): LiveData<FriendRequest> = friendRequests.get(pk).asLiveData()
fun accept(request: FriendRequest) = friendRequests.accept(request)
fun reject(request: FriendRequest) = friendRequests.reject(request)
fun addToChatLog(request: FriendRequest) = scope.launch {
messageRepository.add(
Message(
request.publicKey,
request.message,
Sender.Received,
MessageType.Normal,
0,
Date().time,
),
)
}
}
21 changes: 19 additions & 2 deletions domain/src/main/kotlin/feature/FriendRequestManager.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// SPDX-FileCopyrightText: 2019-2021 Robin Lindén
// SPDX-FileCopyrightText: 2019-2024 Robin Lindén
// SPDX-FileCopyrightText: 2022 aTox contributors
//
// SPDX-License-Identifier: GPL-3.0-only

Expand All @@ -11,24 +12,40 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch
import ltd.evilcorp.core.repository.ContactRepository
import ltd.evilcorp.core.repository.FriendRequestRepository
import ltd.evilcorp.core.repository.MessageRepository
import ltd.evilcorp.core.vo.Contact
import ltd.evilcorp.core.vo.FriendRequest
import ltd.evilcorp.core.vo.Message
import ltd.evilcorp.core.vo.MessageType
import ltd.evilcorp.core.vo.Sender
import ltd.evilcorp.domain.tox.PublicKey
import ltd.evilcorp.domain.tox.Tox

class FriendRequestManager @Inject constructor(
private val scope: CoroutineScope,
private val contactRepository: ContactRepository,
private val friendRequestRepository: FriendRequestRepository,
private val messageRepository: MessageRepository,
private val tox: Tox,
) {
fun getAll(): Flow<List<FriendRequest>> = friendRequestRepository.getAll()
fun get(id: PublicKey): Flow<FriendRequest> = friendRequestRepository.get(id.string())

fun accept(friendRequest: FriendRequest) = scope.launch {
val acceptTime = Date().time
tox.acceptFriendRequest(PublicKey(friendRequest.publicKey))
messageRepository.add(
Message(
friendRequest.publicKey,
friendRequest.message,
Sender.Received,
MessageType.Normal,
0,
acceptTime,
),
)
contactRepository.add(Contact(friendRequest.publicKey))
contactRepository.setLastMessage(friendRequest.publicKey, Date().time)
contactRepository.setLastMessage(friendRequest.publicKey, acceptTime)
friendRequestRepository.delete(friendRequest)
}

Expand Down

0 comments on commit 338e544

Please sign in to comment.