2022-09-30 05:32:07 +02:00
|
|
|
package org.thoughtcrime.securesms.groups
|
|
|
|
|
|
|
|
import androidx.lifecycle.LiveData
|
|
|
|
import androidx.lifecycle.MutableLiveData
|
|
|
|
import androidx.lifecycle.ViewModel
|
2023-11-01 06:39:34 +01:00
|
|
|
import androidx.lifecycle.liveData
|
2022-09-30 05:32:07 +02:00
|
|
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
2023-09-07 09:33:24 +02:00
|
|
|
import network.loki.messenger.R
|
2022-09-30 05:32:07 +02:00
|
|
|
import org.session.libsession.utilities.recipients.Recipient
|
2023-09-07 09:33:24 +02:00
|
|
|
import org.thoughtcrime.securesms.database.Storage
|
2023-10-30 07:45:15 +01:00
|
|
|
import org.thoughtcrime.securesms.groups.compose.ViewState
|
2022-09-30 05:32:07 +02:00
|
|
|
import javax.inject.Inject
|
|
|
|
|
|
|
|
@HiltViewModel
|
|
|
|
class CreateGroupViewModel @Inject constructor(
|
2023-09-07 09:33:24 +02:00
|
|
|
private val storage: Storage,
|
2022-09-30 05:32:07 +02:00
|
|
|
) : ViewModel() {
|
|
|
|
|
2023-10-30 07:45:15 +01:00
|
|
|
private val _viewState = MutableLiveData(ViewState.DEFAULT)
|
|
|
|
val viewState: LiveData<ViewState> = _viewState
|
2023-09-07 09:33:24 +02:00
|
|
|
|
2023-11-02 02:27:23 +01:00
|
|
|
val contacts = liveData { emit(storage.getAllContacts()) }
|
2023-11-01 06:39:34 +01:00
|
|
|
|
2023-11-02 02:27:23 +01:00
|
|
|
fun tryCreateGroup(): Recipient? {
|
2023-11-01 06:39:34 +01:00
|
|
|
|
2023-11-02 02:27:23 +01:00
|
|
|
val currentState = _viewState.value!!
|
2023-09-07 09:33:24 +02:00
|
|
|
|
2023-11-02 02:27:23 +01:00
|
|
|
_viewState.postValue(currentState.copy(isLoading = true, error = null))
|
2023-09-07 09:33:24 +02:00
|
|
|
|
2023-11-02 02:27:23 +01:00
|
|
|
val name = currentState.name
|
|
|
|
val description = currentState.description
|
|
|
|
val members = currentState.members.toMutableSet()
|
2023-09-08 04:42:53 +02:00
|
|
|
|
2023-09-18 09:26:34 +02:00
|
|
|
// do some validation
|
2023-10-04 00:59:10 +02:00
|
|
|
// need a name
|
2023-09-08 04:42:53 +02:00
|
|
|
if (name.isEmpty()) {
|
2023-09-13 02:12:35 +02:00
|
|
|
_viewState.postValue(
|
2023-11-02 02:27:23 +01:00
|
|
|
currentState.copy(isLoading = false, error = R.string.error)
|
2023-09-07 09:33:24 +02:00
|
|
|
)
|
2023-09-13 02:12:35 +02:00
|
|
|
return null
|
2022-09-30 05:32:07 +02:00
|
|
|
}
|
2023-10-24 08:15:28 +02:00
|
|
|
|
|
|
|
storage.getAllContacts().forEach { contact ->
|
|
|
|
members.add(contact)
|
|
|
|
}
|
|
|
|
|
2023-10-04 00:59:10 +02:00
|
|
|
if (members.size <= 1) {
|
2023-10-02 08:39:16 +02:00
|
|
|
_viewState.postValue(
|
2023-11-02 02:27:23 +01:00
|
|
|
currentState.copy(isLoading = false, error = R.string.activity_create_closed_group_not_enough_group_members_error)
|
2023-10-02 08:39:16 +02:00
|
|
|
)
|
|
|
|
}
|
2023-09-07 09:33:24 +02:00
|
|
|
|
|
|
|
// make a group
|
2023-10-02 08:39:16 +02:00
|
|
|
val newGroup = storage.createNewGroup(name, description, members)
|
2023-09-18 09:26:34 +02:00
|
|
|
if (!newGroup.isPresent) {
|
2023-11-02 02:27:23 +01:00
|
|
|
_viewState.postValue(currentState.copy(isLoading = false, error = null))
|
2023-09-18 09:26:34 +02:00
|
|
|
}
|
2023-09-13 02:12:35 +02:00
|
|
|
return newGroup.orNull()
|
2022-09-30 05:32:07 +02:00
|
|
|
}
|
|
|
|
}
|