Add 'add members' functionality and cleanup, refactor

This commit is contained in:
Haafingar 2020-06-12 14:37:21 +10:00
parent 50799d9d90
commit 91af472d3b
13 changed files with 202 additions and 107 deletions

View File

@ -142,6 +142,9 @@
<activity
android:name="org.thoughtcrime.securesms.loki.activities.SeedActivity"
android:screenOrientation="portrait" />
<activity
android:name="org.thoughtcrime.securesms.loki.activities.SelectContactsActivity"
android:screenOrientation="portrait" />
<activity
android:name="org.thoughtcrime.securesms.loki.activities.PrivacySettingsActivity"
android:screenOrientation="portrait" />

View File

@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/default_session_background" >
<LinearLayout
android:id="@+id/mainContentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:id="@+id/emptyStateContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
android:layout_centerInParent="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/medium_font_size"
android:textColor="@color/text"
android:text="@string/activity_create_closed_group_empty_state_message" />
<Button
style="@style/MediumProminentOutlineButton"
android:id="@+id/createNewPrivateChatButton"
android:layout_width="196dp"
android:layout_height="@dimen/medium_button_height"
android:layout_marginTop="@dimen/medium_spacing"
android:text="@string/activity_create_closed_group_empty_state_button_title" />
</LinearLayout>
</RelativeLayout>

View File

@ -1,10 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:title="@string/GroupCreateActivity_menu_apply_button"
android:id="@+id/menu_create_group"
android:icon="?menu_accept_icon"
app:showAsAction="always|withText"/>
</menu>

View File

@ -3,8 +3,9 @@
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title="@string/activity_edit_closed_group_apply_changes"
android:id="@+id/editClosedGroupButton"
android:title="@string/menu_apply_button"
android:id="@+id/applyButton"
android:icon="?menu_accept_icon"
app:showAsAction="always" />
</menu>

View File

@ -3,8 +3,9 @@
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title="Done"
android:id="@+id/createClosedGroupButton"
android:title="@string/menu_done_button"
android:id="@+id/doneButton"
android:icon="?menu_accept_icon"
app:showAsAction="always" />
</menu>

View File

@ -1781,11 +1781,13 @@
<string name="activity_edit_closed_group_invalid_session_id_error">One of the members of your group has an invalid Session ID</string>
<string name="activity_edit_closed_group_confirm_removal">Are you sure you want to remove this user?</string>
<string name="activity_edit_closed_group_member_removed">User removed from group</string>
<string name="activity_edit_closed_group_apply_changes">Apply</string>
<string name="fragment_edit_group_bottom_sheet_remove">Remove user from group</string>
<string name="fragment_edit_group_bottom_sheet_admin">Make this user a group admin</string>
<string name="activity_select_contacts_title">Select Contacts</string>
<string name="activity_join_public_chat_title">Join Open Group</string>
<string name="activity_join_public_chat_error">Couldn\'t join group</string>
<string name="activity_join_public_chat_enter_group_url_tab_title">Open Group URL</string>
@ -1878,4 +1880,7 @@
<string name="fragment_contact_selection_closed_groups_title">Closed Groups</string>
<string name="fragment_contact_selection_open_groups_title">Open Groups</string>
<string name="menu_apply_button">Apply</string>
<string name="menu_done_button">Done</string>
</resources>

View File

@ -209,7 +209,7 @@ public class GroupCreateActivity extends PassphraseRequiredActionBarActivity
MenuInflater inflater = this.getMenuInflater();
menu.clear();
inflater.inflate(R.menu.group_create, menu);
inflater.inflate(R.menu.menu_apply, menu);
super.onPrepareOptionsMenu(menu);
return true;
}
@ -221,7 +221,7 @@ public class GroupCreateActivity extends PassphraseRequiredActionBarActivity
case android.R.id.home:
finish();
return true;
case R.id.menu_create_group:
case R.id.applyButton:
if (groupToUpdate.isPresent()) handleGroupUpdate();
else handleGroupCreate();
return true;
@ -389,7 +389,7 @@ public class GroupCreateActivity extends PassphraseRequiredActionBarActivity
protected void onPreExecute() {
activity.findViewById(R.id.group_details_layout).setVisibility(View.GONE);
activity.findViewById(R.id.creating_group_layout).setVisibility(View.VISIBLE);
activity.findViewById(R.id.menu_create_group).setVisibility(View.GONE);
activity.findViewById(R.id.applyButton).setVisibility(View.GONE);
final int titleResId = activity.groupToUpdate.isPresent()
? R.string.GroupCreateActivity_updating_group
: R.string.GroupCreateActivity_creating_group;
@ -401,7 +401,7 @@ public class GroupCreateActivity extends PassphraseRequiredActionBarActivity
if (activity.isFinishing()) return;
activity.findViewById(R.id.group_details_layout).setVisibility(View.VISIBLE);
activity.findViewById(R.id.creating_group_layout).setVisibility(View.GONE);
activity.findViewById(R.id.menu_create_group).setVisibility(View.VISIBLE);
activity.findViewById(R.id.applyButton).setVisibility(View.VISIBLE);
}
}

View File

@ -30,7 +30,7 @@ class CreateClosedGroupActivity : PassphraseRequiredActionBarActivity(), MemberC
set(value) { field = value; createClosedGroupAdapter.members = value }
private val createClosedGroupAdapter by lazy {
val result = CreateClosedGroupAdapter(this)
val result = SelectContactsAdapter(this)
result.glide = GlideApp.with(this)
result.memberClickListener = this
result
@ -55,14 +55,14 @@ class CreateClosedGroupActivity : PassphraseRequiredActionBarActivity(), MemberC
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu_create_closed_group, menu)
menuInflater.inflate(R.menu.menu_done, menu)
return members.isNotEmpty()
}
// endregion
// region Updating
override fun onCreateLoader(id: Int, bundle: Bundle?): Loader<List<String>> {
return CreateClosedGroupLoader(this)
return SelectContactsLoader(this)
}
override fun onLoadFinished(loader: Loader<List<String>>, members: List<String>) {
@ -85,7 +85,7 @@ class CreateClosedGroupActivity : PassphraseRequiredActionBarActivity(), MemberC
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val id = item.itemId
when(id) {
R.id.createClosedGroupButton -> createClosedGroup()
R.id.doneButton -> createClosedGroup()
else -> { /* Do nothing */ }
}
return super.onOptionsItemSelected(item)

View File

@ -2,8 +2,6 @@ package org.thoughtcrime.securesms.loki.activities
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.os.AsyncTask
import android.os.Bundle
import android.support.v4.app.LoaderManager
import android.support.v4.content.Loader
@ -22,18 +20,13 @@ import kotlinx.android.synthetic.main.activity_linked_devices.recyclerView
import kotlinx.android.synthetic.main.activity_settings.*
import network.loki.messenger.R
import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity
import org.thoughtcrime.securesms.conversation.ConversationActivity
import org.thoughtcrime.securesms.database.Address
import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.database.ThreadDatabase
import org.thoughtcrime.securesms.groups.GroupManager
import org.thoughtcrime.securesms.loki.dialogs.GroupEditingOptionsBottomSheet
import org.thoughtcrime.securesms.mms.GlideApp
import org.thoughtcrime.securesms.recipients.Recipient
import org.thoughtcrime.securesms.util.TextSecurePreferences
import org.whispersystems.libsignal.util.guava.Optional
import org.whispersystems.signalservice.api.crypto.ProfileCipher
import java.lang.ref.WeakReference
class EditClosedGroupActivity : PassphraseRequiredActionBarActivity(), MemberClickListener, LoaderManager.LoaderCallbacks<List<String>> {
private var members = listOf<String>()
@ -42,8 +35,10 @@ class EditClosedGroupActivity : PassphraseRequiredActionBarActivity(), MemberCli
private var membersToRemove = setOf<String>()
private var membersToAdd = setOf<String>()
private var admins = setOf<String>()
private var displayNameToBeUploaded: String? = null
private val originalName by lazy { DatabaseFactory.getGroupDatabase(this).getGroup(groupID).get().title }
private var nameHasChanged = false
private lateinit var newGroupDisplayName: String
private val editClosedGroupAdapter by lazy {
val result = EditClosedGroupAdapter(this)
@ -55,7 +50,6 @@ class EditClosedGroupActivity : PassphraseRequiredActionBarActivity(), MemberCli
set(value) { field = value; handleIsEditingDisplayNameChanged() }
companion object {
public val createNewPrivateChatResultCode = 100
@JvmField
public val GROUP_ID = "GROUP_ID"
}
@ -72,12 +66,12 @@ class EditClosedGroupActivity : PassphraseRequiredActionBarActivity(), MemberCli
saveEditButton.setOnClickListener { saveDisplayName() }
recyclerView.adapter = editClosedGroupAdapter
recyclerView.layoutManager = LinearLayoutManager(this)
addMembersClosedGroupButton.setOnClickListener { createNewPrivateChat() }
addMembersClosedGroupButton.setOnClickListener { onAddMember() }
LoaderManager.getInstance(this).initLoader(0, null, this)
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu_edit_closed_group, menu)
menuInflater.inflate(R.menu.menu_apply, menu)
return members.isNotEmpty()
}
// endregion
@ -107,16 +101,11 @@ class EditClosedGroupActivity : PassphraseRequiredActionBarActivity(), MemberCli
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val id = item.itemId
when(id) {
R.id.editClosedGroupButton -> modifyClosedGroup()
else -> { /* Do nothing */ }
R.id.applyButton -> modifyClosedGroup()
}
return super.onOptionsItemSelected(item)
}
private fun createNewPrivateChat() {
setResult(createNewPrivateChatResultCode)
finish()
}
private fun showEditDisplayNameUI() {
isEditingGroupName = true
}
@ -137,15 +126,21 @@ class EditClosedGroupActivity : PassphraseRequiredActionBarActivity(), MemberCli
bottomSheet.show(supportFragmentManager, "closeBottomSheet")
}
private fun onAddMember() {
val intent = Intent(this@EditClosedGroupActivity, SelectContactsActivity::class.java)
startActivityForResult(intent, 124)
}
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()
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK) {
if (data != null && data.hasExtra("Selected Contacts Result")) {
val returnedContacts = data.extras.getStringArray("Selected Contacts Result")
var selectedContacts = returnedContacts.toSet()
membersToAdd = selectedContacts + membersToAdd
members = members + membersToAdd
}
}
}
private fun handleIsEditingDisplayNameChanged() {
@ -163,7 +158,8 @@ class EditClosedGroupActivity : PassphraseRequiredActionBarActivity(), MemberCli
}
private fun saveDisplayName() {
val groupDisplayName = displayNameEditText.text.toString().trim()
var groupDisplayName = displayNameEditText.text.toString().trim()
// something about the implementation of displayNameEdit text makes it null insead of whatever the user inputs, causes a crash
if (groupDisplayName.isEmpty()) {
return Toast.makeText(this, R.string.activity_settings_display_name_missing_error, Toast.LENGTH_SHORT).show()
}
@ -174,13 +170,16 @@ class EditClosedGroupActivity : PassphraseRequiredActionBarActivity(), MemberCli
return Toast.makeText(this, R.string.activity_settings_display_name_too_long_error, Toast.LENGTH_SHORT).show()
}
isEditingGroupName = false
displayNameToBeUploaded = groupDisplayName
nameHasChanged = true
newGroupDisplayName = groupDisplayName
}
private fun modifyClosedGroup() {
if (originalName == displayNameToBeUploaded && membersToRemove.isEmpty() && membersToAdd.isEmpty()) { /* do nothing, close the activity and return to conversation */ } else {
if (!nameHasChanged && membersToRemove.isEmpty() && membersToAdd.isEmpty()) { finish() } else {
var groupDisplayName = originalName
if (originalName != displayNameToBeUploaded) {
groupDisplayName = displayNameToBeUploaded.toString()
if (nameHasChanged) {
groupDisplayName = newGroupDisplayName
}
val finalGroupMembers = members.map {
Recipient.from(this, Address.fromSerialized(it), false)
@ -192,33 +191,4 @@ class EditClosedGroupActivity : PassphraseRequiredActionBarActivity(), MemberCli
finish()
}
}
// endregion
// region Tasks
internal class EditClosedGroupTask(
private val activity: WeakReference<EditClosedGroupActivity>,
private val groupID: String,
private val profilePicture: Bitmap?,
private val name: String?,
private val members: Set<Recipient>,
private val admins: Set<Recipient>
) : AsyncTask<Void, Void, Optional<GroupManager.GroupActionResult>>() {
override fun doInBackground(vararg params: Void?): Optional<GroupManager.GroupActionResult> {
val activity = activity.get() ?: return Optional.absent()
return Optional.of(GroupManager.updateGroup(activity, groupID, members, profilePicture, name, admins))
}
override fun onPostExecute(result: Optional<GroupManager.GroupActionResult>) {
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)
}
} else {
super.onPostExecute(result)
Toast.makeText(activity.applicationContext, R.string.activity_create_closed_group_invalid_session_id_error, Toast.LENGTH_LONG).show()
}
}
}
}

View File

@ -12,28 +12,6 @@ class EditClosedGroupLoader(val groupID: String, context: Context) : AsyncLoader
return members.map {
it.address.toPhoneString()
}
/* To do: Load admins in the process here */
}
/* For loading contacts for Add members, and loading admins from group list
override fun loadContactsInBackground(): List<String> {
val contacts = ContactUtilities.getAllContacts(context)
// Only show the master devices of the users we are friends with
return contacts.filter { contact ->
!contact.recipient.isGroupRecipient && contact.isFriend && !contact.isOurDevice && !contact.isSlave
}.map {
it.recipient.address.toPhoneString()
}
}
override fun loadAdminsInBackground(): List<String> {
val contacts = ContactUtilities.getAllContacts(context)
// Only show the master devices of the users we are friends with
return contacts.filter { contact ->
!contact.recipient.isGroupRecipient && contact.isFriend && !contact.isOurDevice && !contact.isSlave
}.map {
it.recipient.address.toPhoneString()
}
}
*/
}

View File

@ -0,0 +1,101 @@
package org.thoughtcrime.securesms.loki.activities
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.LoaderManager
import android.support.v4.content.Loader
import android.support.v7.widget.LinearLayoutManager
import android.view.Menu
import android.view.MenuItem
import android.view.View
import kotlinx.android.synthetic.main.activity_create_closed_group.*
import kotlinx.android.synthetic.main.activity_linked_devices.recyclerView
import network.loki.messenger.R
import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity
import org.thoughtcrime.securesms.mms.GlideApp
class SelectContactsActivity : PassphraseRequiredActionBarActivity(), MemberClickListener, LoaderManager.LoaderCallbacks<List<String>> {
private var members = listOf<String>()
set(value) { field = value; selectContactsAdapter.members = value }
private val selectContactsAdapter by lazy {
val result = SelectContactsAdapter(this)
result.glide = GlideApp.with(this)
result.memberClickListener = this
result
}
private val selectedMembers: Set<String>
get() { return selectContactsAdapter.selectedMembers }
companion object {
public val createNewPrivateChatResultCode = 100
}
// region Lifecycle
override fun onCreate(savedInstanceState: Bundle?, isReady: Boolean) {
super.onCreate(savedInstanceState, isReady)
setContentView(R.layout.activity_select_contacts)
supportActionBar!!.title = resources.getString(R.string.activity_select_contacts_title)
recyclerView.adapter = selectContactsAdapter
recyclerView.layoutManager = LinearLayoutManager(this)
createNewPrivateChatButton.setOnClickListener { createNewPrivateChat() }
LoaderManager.getInstance(this).initLoader(0, null, this)
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu_done, menu)
return members.isNotEmpty()
}
// endregion
// region Updating
override fun onCreateLoader(id: Int, bundle: Bundle?): Loader<List<String>> {
return SelectContactsLoader(this)
}
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
mainContentContainer.visibility = if (members.isEmpty()) View.GONE else View.VISIBLE
emptyStateContainer.visibility = if (members.isEmpty()) View.VISIBLE else View.GONE
invalidateOptionsMenu()
}
// endregion
// region Interaction
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val id = item.itemId
when(id) {
R.id.doneButton -> returnContacts()
else -> { /* Do nothing */ }
}
return super.onOptionsItemSelected(item)
}
private fun createNewPrivateChat() {
setResult(createNewPrivateChatResultCode)
finish()
}
override fun onMemberClick(member: String) {
selectContactsAdapter.onMemberClick(member)
}
private fun returnContacts() {
val selectedMembers = this.selectedMembers
val selectedContacts = selectedMembers.toTypedArray()
val data = Intent()
data.putExtra("Selected Contacts Result", selectedContacts)
setResult(Activity.RESULT_OK, data)
finish()
}
}

View File

@ -8,7 +8,7 @@ import org.thoughtcrime.securesms.loki.views.UserView
import org.thoughtcrime.securesms.mms.GlideRequests
import org.thoughtcrime.securesms.recipients.Recipient
class CreateClosedGroupAdapter(private val context: Context) : RecyclerView.Adapter<CreateClosedGroupAdapter.ViewHolder>() {
class SelectContactsAdapter(private val context: Context) : RecyclerView.Adapter<SelectContactsAdapter.ViewHolder>() {
lateinit var glide: GlideRequests
val selectedMembers = mutableSetOf<String>()
var members = listOf<String>()

View File

@ -4,7 +4,7 @@ import android.content.Context
import org.thoughtcrime.securesms.loki.utilities.ContactUtilities
import org.thoughtcrime.securesms.util.AsyncLoader
class CreateClosedGroupLoader(context: Context) : AsyncLoader<List<String>>(context) {
class SelectContactsLoader(context: Context) : AsyncLoader<List<String>>(context) {
override fun loadInBackground(): List<String> {
val contacts = ContactUtilities.getAllContacts(context)