Create closed group activity refactoring.

This commit is contained in:
Anton Chekulaev 2020-08-14 15:23:06 +10:00
parent 6664c2baa7
commit 15d463161a
9 changed files with 44 additions and 55 deletions

View File

@ -63,7 +63,7 @@
<Button
style="@style/MediumProminentOutlineButton"
android:id="@+id/createNewPrivateChatButton"
android:id="@+id/btnCreateNewPrivateChat"
android:layout_width="196dp"
android:layout_height="@dimen/medium_button_height"
android:layout_marginTop="@dimen/medium_spacing"

View File

@ -107,7 +107,7 @@
<Button
style="@style/MediumProminentOutlineButton"
android:id="@+id/createNewPrivateChatButton"
android:id="@+id/btnCreateNewPrivateChat"
android:layout_width="196dp"
android:layout_height="@dimen/medium_button_height"
android:layout_marginTop="@dimen/medium_spacing"

View File

@ -35,7 +35,7 @@
<Button
style="@style/MediumProminentOutlineButton"
android:id="@+id/createNewPrivateChatButton"
android:id="@+id/btnCreateNewPrivateChat"
android:layout_width="196dp"
android:layout_height="@dimen/medium_button_height"
android:layout_marginTop="@dimen/medium_spacing"

View File

@ -1,5 +1,6 @@
package org.thoughtcrime.securesms.loki.activities
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.os.AsyncTask
@ -25,31 +26,29 @@ import org.thoughtcrime.securesms.util.TextSecurePreferences
import org.whispersystems.libsignal.util.guava.Optional
import java.lang.ref.WeakReference
class CreateClosedGroupActivity : PassphraseRequiredActionBarActivity(), MemberClickListener, LoaderManager.LoaderCallbacks<List<String>> {
const val RESULT_CODE_CREATE_CLOSED_GROUP = 100
class CreateClosedGroupActivity : PassphraseRequiredActionBarActivity(), LoaderManager.LoaderCallbacks<List<String>> {
private lateinit var selectContactsAdapter: SelectContactsAdapter
private var members = listOf<String>()
set(value) { field = value; createClosedGroupAdapter.members = value }
private val createClosedGroupAdapter by lazy {
val glide = GlideApp.with(this)
val result = SelectContactsAdapter(this, glide, this)
result
}
private val selectedMembers: Set<String>
get() { return createClosedGroupAdapter.selectedMembers }
companion object {
public val createNewPrivateChatResultCode = 100
}
set(value) {
field = value
selectContactsAdapter.members = value
}
// region Lifecycle
override fun onCreate(savedInstanceState: Bundle?, isReady: Boolean) {
super.onCreate(savedInstanceState, isReady)
setContentView(R.layout.activity_create_closed_group)
supportActionBar!!.title = resources.getString(R.string.activity_create_closed_group_title)
recyclerView.adapter = createClosedGroupAdapter
this.selectContactsAdapter = SelectContactsAdapter(this, GlideApp.with(this))
recyclerView.adapter = this.selectContactsAdapter
recyclerView.layoutManager = LinearLayoutManager(this)
createNewPrivateChatButton.setOnClickListener { createNewPrivateChat() }
btnCreateNewPrivateChat.setOnClickListener { createNewPrivateChat() }
LoaderManager.getInstance(this).initLoader(0, null, this)
}
@ -82,23 +81,17 @@ class CreateClosedGroupActivity : PassphraseRequiredActionBarActivity(), MemberC
// region Interaction
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val id = item.itemId
when(id) {
when(item.itemId) {
R.id.doneButton -> createClosedGroup()
else -> { /* Do nothing */ }
}
return super.onOptionsItemSelected(item)
}
private fun createNewPrivateChat() {
setResult(createNewPrivateChatResultCode)
setResult(RESULT_CODE_CREATE_CLOSED_GROUP)
finish()
}
override fun onMemberClick(member: String) {
createClosedGroupAdapter.onMemberClick(member)
}
private fun createClosedGroup() {
val name = nameEditText.text.trim()
if (name.isEmpty()) {
@ -107,7 +100,7 @@ class CreateClosedGroupActivity : PassphraseRequiredActionBarActivity(), MemberC
if (name.length >= 64) {
return Toast.makeText(this, R.string.activity_create_closed_group_group_name_too_long_error, Toast.LENGTH_LONG).show()
}
val selectedMembers = this.selectedMembers
val selectedMembers = this.selectContactsAdapter.selectedMembers
if (selectedMembers.count() < 2) {
return Toast.makeText(this, R.string.activity_create_closed_group_not_enough_group_members_error, Toast.LENGTH_LONG).show()
}
@ -119,16 +112,8 @@ class CreateClosedGroupActivity : PassphraseRequiredActionBarActivity(), MemberC
}.toSet()
val masterHexEncodedPublicKey = TextSecurePreferences.getMasterHexEncodedPublicKey(this) ?: TextSecurePreferences.getLocalNumber(this)
val admin = Recipient.from(this, Address.fromSerialized(masterHexEncodedPublicKey), false)
CreateClosedGroupTask(WeakReference(this), null, name.toString(), recipients, setOf( admin )).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
}
private fun handleOpenConversation(threadId: Long, recipient: Recipient) {
val intent = Intent(this, 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)
startActivity(intent)
finish()
CreateClosedGroupTask(WeakReference(this), null, name.toString(), recipients, setOf( admin ))
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
}
// endregion
@ -150,7 +135,8 @@ class CreateClosedGroupActivity : PassphraseRequiredActionBarActivity(), MemberC
val activity = activity.get() ?: return super.onPostExecute(result)
if (result.isPresent && result.get().threadId > -1) {
if (!activity.isFinishing) {
activity.handleOpenConversation(result.get().threadId, result.get().groupRecipient)
openConversationActivity(activity, result.get().threadId, result.get().groupRecipient)
activity.finish()
}
} else {
super.onPostExecute(result)
@ -160,3 +146,11 @@ class CreateClosedGroupActivity : PassphraseRequiredActionBarActivity(), MemberC
}
// endregion
}
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)
}

View File

@ -80,7 +80,7 @@ class EditClosedGroupActivity : PassphraseRequiredActionBarActivity() {
override fun onLoadFinished(loader: Loader<List<String>>, members: List<String>) {
// We no longer need any subsequent loading events
// (they will occur on every activity resume)
// (they will occur on every activity resume).
LoaderManager.getInstance(this@EditClosedGroupActivity).destroyLoader(LOADER_ID_MEMBERS)
originalMembers.clear()

View File

@ -31,7 +31,6 @@ import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.database.ThreadDatabase
import org.thoughtcrime.securesms.database.model.ThreadRecord
import org.thoughtcrime.securesms.jobs.MultiDeviceBlockedUpdateJob
import org.thoughtcrime.securesms.loki.database.LokiAPIDatabase
import org.thoughtcrime.securesms.loki.dialogs.ConversationOptionsBottomSheet
import org.thoughtcrime.securesms.loki.dialogs.MultiDeviceRemovalBottomSheet
import org.thoughtcrime.securesms.loki.protocol.ClosedGroupsProtocol
@ -120,7 +119,7 @@ class HomeActivity : PassphraseRequiredActionBarActivity, ConversationClickListe
recyclerView.adapter = homeAdapter
recyclerView.layoutManager = LinearLayoutManager(this)
// Set up empty state view
createNewPrivateChatButton.setOnClickListener { createNewPrivateChat() }
btnCreateNewPrivateChat.setOnClickListener { createNewPrivateChat() }
// This is a workaround for the fact that CursorRecyclerViewAdapter doesn't actually auto-update (even though it says it will)
LoaderManager.getInstance(this).restartLoader(0, null, object : LoaderManager.LoaderCallbacks<Cursor> {
@ -221,7 +220,7 @@ class HomeActivity : PassphraseRequiredActionBarActivity, ConversationClickListe
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == CreateClosedGroupActivity.createNewPrivateChatResultCode) {
if (resultCode == RESULT_CODE_CREATE_CLOSED_GROUP) {
createNewPrivateChat()
}
}

View File

@ -1,5 +1,6 @@
package org.thoughtcrime.securesms.loki.activities
@FunctionalInterface
interface MemberClickListener {
fun onMemberClick(member: String)
}

View File

@ -17,13 +17,13 @@ import org.thoughtcrime.securesms.mms.GlideApp
const val EXTRA_SELECTED_CONTACTS = "SELECTED_CONTACTS_RESULT"
class SelectContactsActivity : PassphraseRequiredActionBarActivity(), MemberClickListener, LoaderManager.LoaderCallbacks<List<String>> {
class SelectContactsActivity : PassphraseRequiredActionBarActivity(), LoaderManager.LoaderCallbacks<List<String>> {
private var members = listOf<String>()
set(value) { field = value; selectContactsAdapter.members = value }
private val selectContactsAdapter by lazy {
val glide = GlideApp.with(this)
val result = SelectContactsAdapter(this, glide, this)
val result = SelectContactsAdapter(this, glide)
result
}
@ -41,7 +41,7 @@ class SelectContactsActivity : PassphraseRequiredActionBarActivity(), MemberClic
supportActionBar!!.title = resources.getString(R.string.activity_select_contacts_title)
recyclerView.adapter = selectContactsAdapter
recyclerView.layoutManager = LinearLayoutManager(this)
createNewPrivateChatButton.setOnClickListener { createNewPrivateChat() }
btnCreateNewPrivateChat.setOnClickListener { createNewPrivateChat() }
LoaderManager.getInstance(this).initLoader(0, null, this)
}
@ -87,10 +87,6 @@ class SelectContactsActivity : PassphraseRequiredActionBarActivity(), MemberClic
finish()
}
override fun onMemberClick(member: String) {
selectContactsAdapter.onMemberClick(member)
}
private fun returnContacts() {
val selectedMembers = this.selectedMembers
val selectedContacts = selectedMembers.toTypedArray()

View File

@ -10,8 +10,7 @@ import org.thoughtcrime.securesms.recipients.Recipient
class SelectContactsAdapter(
private val context: Context,
private val glide: GlideRequests,
private val memberClickListener: MemberClickListener? = null)
private val glide: GlideRequests)
: RecyclerView.Adapter<SelectContactsAdapter.ViewHolder>() {
val selectedMembers = mutableSetOf<String>()
@ -32,12 +31,12 @@ class SelectContactsAdapter(
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
val member = members[position]
viewHolder.view.setOnClickListener { memberClickListener?.onMemberClick(member) }
viewHolder.view.setOnClickListener { onMemberClick(member) }
val isSelected = selectedMembers.contains(member)
viewHolder.view.bind(Recipient.from(context, Address.fromSerialized(member), false), isSelected, glide, false)
}
fun onMemberClick(member: String) {
private fun onMemberClick(member: String) {
if (selectedMembers.contains(member)) {
selectedMembers.remove(member)
} else {