2020-05-11 08:19:26 +02:00
|
|
|
package org.thoughtcrime.securesms.loki.activities
|
2020-01-31 03:57:24 +01:00
|
|
|
|
2020-08-14 07:23:06 +02:00
|
|
|
import android.content.Context
|
2020-02-03 00:23:24 +01:00
|
|
|
import android.content.Intent
|
2020-01-31 03:57:24 +01:00
|
|
|
import android.os.Bundle
|
2020-08-19 02:06:26 +02:00
|
|
|
import androidx.loader.app.LoaderManager
|
|
|
|
import androidx.loader.content.Loader
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
2020-01-31 03:57:24 +01:00
|
|
|
import android.view.Menu
|
|
|
|
import android.view.MenuItem
|
2020-03-12 01:16:07 +01:00
|
|
|
import android.view.View
|
2020-01-31 03:57:24 +01:00
|
|
|
import android.widget.Toast
|
2020-02-03 00:23:24 +01:00
|
|
|
import kotlinx.android.synthetic.main.activity_create_closed_group.*
|
2020-01-31 03:57:24 +01:00
|
|
|
import network.loki.messenger.R
|
2020-09-02 03:47:54 +02:00
|
|
|
import nl.komponents.kovenant.ui.successUi
|
2020-01-31 03:57:24 +01:00
|
|
|
import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity
|
2020-02-03 00:23:24 +01:00
|
|
|
import org.thoughtcrime.securesms.conversation.ConversationActivity
|
2021-01-13 07:11:30 +01:00
|
|
|
import org.session.libsession.messaging.threads.Address
|
2020-08-12 04:22:16 +02:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory
|
2020-02-03 00:23:24 +01:00
|
|
|
import org.thoughtcrime.securesms.database.ThreadDatabase
|
2020-09-02 03:47:54 +02:00
|
|
|
import org.thoughtcrime.securesms.loki.utilities.fadeIn
|
|
|
|
import org.thoughtcrime.securesms.loki.utilities.fadeOut
|
2020-01-31 03:57:24 +01:00
|
|
|
import org.thoughtcrime.securesms.mms.GlideApp
|
2021-01-13 07:11:30 +01:00
|
|
|
import org.session.libsession.messaging.threads.recipients.Recipient
|
2021-01-21 05:42:43 +01:00
|
|
|
import org.session.libsession.utilities.TextSecurePreferences
|
2021-01-13 06:13:49 +01:00
|
|
|
import org.thoughtcrime.securesms.loki.protocol.ClosedGroupsProtocolV2
|
2020-01-31 03:57:24 +01:00
|
|
|
|
2020-12-04 06:00:52 +01:00
|
|
|
//TODO Refactor to avoid using kotlinx.android.synthetic
|
2020-08-14 07:23:06 +02:00
|
|
|
class CreateClosedGroupActivity : PassphraseRequiredActionBarActivity(), LoaderManager.LoaderCallbacks<List<String>> {
|
2020-10-06 05:19:52 +02:00
|
|
|
private var isLoading = false
|
|
|
|
set(newValue) { field = newValue; invalidateOptionsMenu() }
|
2020-08-14 07:23:06 +02:00
|
|
|
private var members = listOf<String>()
|
2020-10-06 05:19:52 +02:00
|
|
|
set(value) { field = value; selectContactsAdapter.members = value }
|
2020-03-12 01:16:07 +01:00
|
|
|
|
2020-08-18 00:55:17 +02:00
|
|
|
private val selectContactsAdapter by lazy {
|
|
|
|
SelectContactsAdapter(this, GlideApp.with(this))
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
2021-01-27 05:54:12 +01:00
|
|
|
const val closedGroupCreatedResultCode = 100
|
2020-08-18 00:55:17 +02:00
|
|
|
}
|
|
|
|
|
2020-01-31 03:57:24 +01:00
|
|
|
// region Lifecycle
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?, isReady: Boolean) {
|
|
|
|
super.onCreate(savedInstanceState, isReady)
|
|
|
|
setContentView(R.layout.activity_create_closed_group)
|
2020-05-25 08:38:36 +02:00
|
|
|
supportActionBar!!.title = resources.getString(R.string.activity_create_closed_group_title)
|
2020-08-14 07:23:06 +02:00
|
|
|
recyclerView.adapter = this.selectContactsAdapter
|
2020-01-31 03:57:24 +01:00
|
|
|
recyclerView.layoutManager = LinearLayoutManager(this)
|
2020-10-06 05:19:52 +02:00
|
|
|
createNewPrivateChatButton.setOnClickListener { createNewPrivateChat() }
|
2020-01-31 03:57:24 +01:00
|
|
|
LoaderManager.getInstance(this).initLoader(0, null, this)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
2020-06-12 06:37:21 +02:00
|
|
|
menuInflater.inflate(R.menu.menu_done, menu)
|
2020-10-06 05:19:52 +02:00
|
|
|
return members.isNotEmpty() && !isLoading
|
2020-01-31 03:57:24 +01:00
|
|
|
}
|
|
|
|
// endregion
|
|
|
|
|
|
|
|
// region Updating
|
|
|
|
override fun onCreateLoader(id: Int, bundle: Bundle?): Loader<List<String>> {
|
2020-08-18 07:40:24 +02:00
|
|
|
return SelectContactsLoader(this, setOf())
|
2020-01-31 03:57:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onLoadFinished(loader: Loader<List<String>>, members: List<String>) {
|
|
|
|
update(members)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onLoaderReset(loader: Loader<List<String>>) {
|
|
|
|
update(listOf())
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun update(members: List<String>) {
|
|
|
|
this.members = members
|
2020-03-12 01:16:07 +01:00
|
|
|
mainContentContainer.visibility = if (members.isEmpty()) View.GONE else View.VISIBLE
|
|
|
|
emptyStateContainer.visibility = if (members.isEmpty()) View.VISIBLE else View.GONE
|
|
|
|
invalidateOptionsMenu()
|
2020-01-31 03:57:24 +01:00
|
|
|
}
|
|
|
|
// endregion
|
|
|
|
|
|
|
|
// region Interaction
|
|
|
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
2020-08-14 07:23:06 +02:00
|
|
|
when(item.itemId) {
|
2020-10-06 05:19:52 +02:00
|
|
|
R.id.doneButton -> if (!isLoading) { createClosedGroup() }
|
2020-01-31 03:57:24 +01:00
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item)
|
|
|
|
}
|
|
|
|
|
2020-03-12 01:16:07 +01:00
|
|
|
private fun createNewPrivateChat() {
|
2020-08-18 00:55:17 +02:00
|
|
|
setResult(Companion.closedGroupCreatedResultCode)
|
2020-03-12 01:16:07 +01:00
|
|
|
finish()
|
|
|
|
}
|
|
|
|
|
2020-01-31 03:57:24 +01:00
|
|
|
private fun createClosedGroup() {
|
2020-08-10 08:38:40 +02:00
|
|
|
val name = nameEditText.text.trim()
|
|
|
|
if (name.isEmpty()) {
|
|
|
|
return Toast.makeText(this, R.string.activity_create_closed_group_group_name_missing_error, Toast.LENGTH_LONG).show()
|
|
|
|
}
|
|
|
|
if (name.length >= 64) {
|
|
|
|
return Toast.makeText(this, R.string.activity_create_closed_group_group_name_too_long_error, Toast.LENGTH_LONG).show()
|
|
|
|
}
|
2020-08-14 09:47:48 +02:00
|
|
|
val selectedMembers = this.selectContactsAdapter.selectedMembers
|
2020-08-26 03:49:19 +02:00
|
|
|
if (selectedMembers.count() < 1) {
|
2020-08-10 08:38:40 +02:00
|
|
|
return Toast.makeText(this, R.string.activity_create_closed_group_not_enough_group_members_error, Toast.LENGTH_LONG).show()
|
|
|
|
}
|
2021-01-27 05:54:12 +01:00
|
|
|
if (selectedMembers.count() >= ClosedGroupsProtocolV2.groupSizeLimit) { // Minus one because we're going to include self later
|
2020-08-10 08:38:40 +02:00
|
|
|
return Toast.makeText(this, R.string.activity_create_closed_group_too_many_group_members_error, Toast.LENGTH_LONG).show()
|
|
|
|
}
|
2021-01-21 05:42:43 +01:00
|
|
|
val userPublicKey = TextSecurePreferences.getLocalNumber(this)!!
|
2020-10-06 05:19:52 +02:00
|
|
|
isLoading = true
|
2020-12-04 06:00:52 +01:00
|
|
|
loaderContainer.fadeIn()
|
2021-01-13 06:13:49 +01:00
|
|
|
ClosedGroupsProtocolV2.createClosedGroup(this, name.toString(), selectedMembers + setOf( userPublicKey )).successUi { groupID ->
|
2020-12-04 06:00:52 +01:00
|
|
|
loaderContainer.fadeOut()
|
2020-10-06 05:19:52 +02:00
|
|
|
isLoading = false
|
2020-11-20 08:59:13 +01:00
|
|
|
val threadID = DatabaseFactory.getThreadDatabase(this).getOrCreateThreadIdFor(Recipient.from(this, Address.fromSerialized(groupID), false))
|
2021-01-29 01:08:48 +01:00
|
|
|
if (!isFinishing) {
|
2020-09-02 03:47:54 +02:00
|
|
|
openConversationActivity(this, threadID, Recipient.from(this, Address.fromSerialized(groupID), false))
|
|
|
|
finish()
|
|
|
|
}
|
2021-02-05 06:35:15 +01:00
|
|
|
|
2020-09-02 03:47:54 +02:00
|
|
|
}
|
2020-08-10 08:38:40 +02:00
|
|
|
}
|
2020-02-03 00:23:24 +01:00
|
|
|
// endregion
|
2020-08-14 07:23:06 +02:00
|
|
|
}
|
|
|
|
|
2020-08-18 00:55:17 +02:00
|
|
|
// region Convenience
|
2020-08-14 07:23:06 +02:00
|
|
|
private fun openConversationActivity(context: Context, threadId: Long, recipient: Recipient) {
|
|
|
|
val intent = Intent(context, ConversationActivity::class.java)
|
|
|
|
intent.putExtra(ConversationActivity.THREAD_ID_EXTRA, threadId)
|
|
|
|
intent.putExtra(ConversationActivity.DISTRIBUTION_TYPE_EXTRA, ThreadDatabase.DistributionTypes.DEFAULT)
|
|
|
|
intent.putExtra(ConversationActivity.ADDRESS_EXTRA, recipient.address)
|
|
|
|
context.startActivity(intent)
|
2020-08-18 00:55:17 +02:00
|
|
|
}
|
|
|
|
// endregion
|