update profile on changes

This commit is contained in:
B. Petersen
2019-05-23 00:43:11 +02:00
parent ecbbe30259
commit 4988f94f76
5 changed files with 104 additions and 81 deletions
@@ -1,7 +1,6 @@
package org.thoughtcrime.securesms;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
@@ -12,10 +11,11 @@ import android.view.MenuItem;
import com.b44t.messenger.DcChat;
import com.b44t.messenger.DcContact;
import com.b44t.messenger.DcContext;
import com.b44t.messenger.DcEventCenter;
import org.thoughtcrime.securesms.connect.ApplicationDcContext;
import org.thoughtcrime.securesms.connect.DcHelper;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.util.DynamicLanguage;
import org.thoughtcrime.securesms.util.DynamicNoActionBarTheme;
import org.thoughtcrime.securesms.util.DynamicTheme;
@@ -23,7 +23,9 @@ import org.thoughtcrime.securesms.util.ViewUtil;
import java.util.ArrayList;
public class ProfileActivity extends PassphraseRequiredActionBarActivity {
public class ProfileActivity extends PassphraseRequiredActionBarActivity
implements DcEventCenter.DcEventDelegate
{
@SuppressWarnings("unused")
private final static String TAG = ProfileActivity.class.getSimpleName();
@@ -43,9 +45,8 @@ public class ProfileActivity extends PassphraseRequiredActionBarActivity {
private ApplicationDcContext dcContext;
private int chatId;
private @Nullable DcChat dcChat;
private boolean chatIsGroup;
private int contactId;
private @Nullable DcContact dcContact;
private ArrayList<Integer> tabs = new ArrayList<>();
private Toolbar toolbar;
@@ -68,6 +69,8 @@ public class ProfileActivity extends PassphraseRequiredActionBarActivity {
this.tabLayout.setupWithViewPager(viewPager);
this.viewPager.setAdapter(new ProfilePagerAdapter(getSupportFragmentManager()));
dcContext.eventCenter.addObserver(DcContext.DC_EVENT_CHAT_MODIFIED, this);
dcContext.eventCenter.addObserver(DcContext.DC_EVENT_CONTACTS_CHANGED, this);
}
@Override
@@ -77,6 +80,12 @@ public class ProfileActivity extends PassphraseRequiredActionBarActivity {
dynamicLanguage.onResume(this);
}
@Override
public void onDestroy() {
dcContext.eventCenter.removeObservers(this);
super.onDestroy();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
@@ -88,25 +97,25 @@ public class ProfileActivity extends PassphraseRequiredActionBarActivity {
return false;
}
@Override
public void handleEvent(int eventId, Object data1, Object data2) {
updateToolbar();
}
private void initializeResources() {
chatId = getIntent().getIntExtra(CHAT_ID_EXTRA, 0);
contactId = getIntent().getIntExtra(CONTACT_ID_EXTRA, 0);
chatId = getIntent().getIntExtra(CHAT_ID_EXTRA, 0);
contactId = getIntent().getIntExtra(CONTACT_ID_EXTRA, 0);
chatIsGroup = false;
if (contactId!=0) {
dcContact = dcContext.getContact(contactId);
chatId = dcContext.getChatIdByContactId(contactId);
if (chatId!=0) {
dcChat = dcContext.getChat(chatId);
}
}
else if(chatId!=0) {
dcChat = dcContext.getChat(chatId);
if(!dcChat.isGroup()) {
DcChat dcChat = dcContext.getChat(chatId);
chatIsGroup = dcChat.isGroup();
if(!chatIsGroup) {
final int[] members = dcContext.getChatContacts(chatId);
contactId = members.length>=1? members[0] : 0;
if (contactId!=0) {
dcContact = dcContext.getContact(contactId);
}
}
}
@@ -137,10 +146,10 @@ public class ProfileActivity extends PassphraseRequiredActionBarActivity {
getSupportActionBar().setTitle(R.string.menu_all_media);
}
else if (isContactProfile()){
getSupportActionBar().setTitle(dcContact.getName());
getSupportActionBar().setTitle(dcContext.getContact(contactId).getName());
}
else if (dcChat != null) {
getSupportActionBar().setTitle(dcChat.getName());
else if (chatId >= 0) {
getSupportActionBar().setTitle(dcContext.getChat(chatId).getName());
}
}
@@ -150,25 +159,13 @@ public class ProfileActivity extends PassphraseRequiredActionBarActivity {
private boolean isContactProfile() {
// there may still be a single-chat lined to the contact profile
return dcContact!=null && (dcChat==null || !dcChat.isGroup());
return contactId!=0 && (chatId==0 || !chatIsGroup);
}
private boolean isSelfProfile() {
return isContactProfile() && contactId==DcContact.DC_CONTACT_ID_SELF;
}
private Recipient getRecipient() {
if(dcChat!=null) {
return dcContext.getRecipient(dcChat);
}
else if(dcContact!=null) {
return dcContext.getRecipient(dcContact);
}
else {
return dcContext.getRecipient(dcContext.getContact(0));
}
}
private class ProfilePagerAdapter extends FragmentStatePagerAdapter {
ProfilePagerAdapter(FragmentManager fragmentManager) {
@@ -184,20 +181,20 @@ public class ProfileActivity extends PassphraseRequiredActionBarActivity {
switch(tabId) {
case TAB_SETTINGS:
fragment = new ProfileSettingsFragment();
args.putInt(ProfileSettingsFragment.CHAT_ID_EXTRA, dcChat==null? -1 : chatId);
args.putInt(ProfileSettingsFragment.CONTACT_ID_EXTRA, dcContact==null? -1 : contactId);
args.putInt(ProfileSettingsFragment.CHAT_ID_EXTRA, (chatId==0&&!isGlobalProfile())? -1 : chatId);
args.putInt(ProfileSettingsFragment.CONTACT_ID_EXTRA, (contactId==0&&!isGlobalProfile())? -1 : contactId);
args.putSerializable(ProfileSettingsFragment.LOCALE_EXTRA, dynamicLanguage.getCurrentLocale());
break;
case TAB_GALLERY:
fragment = new ProfileGalleryFragment();
args.putInt(ProfileGalleryFragment.CHAT_ID_EXTRA, dcChat==null? -1 : chatId);
args.putInt(ProfileGalleryFragment.CHAT_ID_EXTRA, (chatId==0&&!isGlobalProfile())? -1 : chatId);
args.putSerializable(ProfileGalleryFragment.LOCALE_EXTRA, dynamicLanguage.getCurrentLocale());
break;
default:
fragment = new ProfileDocumentsFragment();
args.putInt(ProfileGalleryFragment.CHAT_ID_EXTRA, dcChat==null? -1 : chatId);
args.putInt(ProfileGalleryFragment.CHAT_ID_EXTRA, (chatId==0&&!isGlobalProfile())? -1 : chatId);
args.putSerializable(ProfileDocumentsFragment.LOCALE_EXTRA, dynamicLanguage.getCurrentLocale());
break;
}
@@ -24,6 +24,7 @@ import android.view.Window;
import android.widget.TextView;
import com.b44t.messenger.DcContext;
import com.b44t.messenger.DcEventCenter;
import com.b44t.messenger.DcMsg;
import com.codewaves.stickyheadergrid.StickyHeaderGridLayoutManager;
@@ -37,7 +38,9 @@ import java.util.Collection;
import java.util.Locale;
public class ProfileDocumentsFragment
extends Fragment implements LoaderManager.LoaderCallbacks<BucketedThreadMediaLoader.BucketedThreadMedia>, ProfileDocumentsAdapter.ItemClickListener
extends Fragment
implements LoaderManager.LoaderCallbacks<BucketedThreadMediaLoader.BucketedThreadMedia>,
ProfileDocumentsAdapter.ItemClickListener, DcEventCenter.DcEventDelegate
{
public static final String LOCALE_EXTRA = "locale_extra";
public static final String CHAT_ID_EXTRA = "chat_id";
@@ -48,13 +51,15 @@ public class ProfileDocumentsFragment
private ActionMode actionMode;
private ActionModeCallback actionModeCallback = new ActionModeCallback();
protected int chatId;
protected Locale locale;
private ApplicationDcContext dcContext;
protected int chatId;
protected Locale locale;
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
dcContext = DcHelper.getContext(getContext());
chatId = getArguments().getInt(CHAT_ID_EXTRA, -1);
locale = (Locale)getArguments().getSerializable(LOCALE_EXTRA);
if (locale == null) throw new AssertionError();
@@ -77,9 +82,22 @@ public class ProfileDocumentsFragment
this.recyclerView.setLayoutManager(gridManager);
this.recyclerView.setHasFixedSize(true);
dcContext.eventCenter.addObserver(DcContext.DC_EVENT_MSGS_CHANGED, this);
dcContext.eventCenter.addObserver(DcContext.DC_EVENT_INCOMING_MSG, this);
return view;
}
@Override
public void onDestroyView() {
dcContext.eventCenter.removeObservers(this);
super.onDestroyView();
}
@Override
public void handleEvent(int eventId, Object data1, Object data2) {
getLoaderManager().restartLoader(0, null, this);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
@@ -130,7 +148,7 @@ public class ProfileDocumentsFragment
}
private void handleMediaPreviewClick(@NonNull DcMsg dcMsg) {
// audio is stated by the play-button
// audio is started by the play-button
if (dcMsg.getType()==DcMsg.DC_MSG_AUDIO || dcMsg.getType()==DcMsg.DC_MSG_VOICE) {
return;
}
@@ -140,7 +158,6 @@ public class ProfileDocumentsFragment
return;
}
ApplicationDcContext dcContext = DcHelper.getContext(context);
dcContext.openForViewOrShare(dcMsg.getId(), Intent.ACTION_VIEW);
}
@@ -165,7 +182,6 @@ public class ProfileDocumentsFragment
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setMessage(confirmMessage);
builder.setCancelable(true);
final DcContext dcContext = DcHelper.getContext(getContext());
builder.setPositiveButton(R.string.delete, (dialogInterface, i) -> {
new ProgressDialogAsyncTask<DcMsg, Void, Void>(getContext(),
@@ -24,9 +24,11 @@ import android.view.Window;
import android.widget.TextView;
import com.b44t.messenger.DcContext;
import com.b44t.messenger.DcEventCenter;
import com.b44t.messenger.DcMsg;
import com.codewaves.stickyheadergrid.StickyHeaderGridLayoutManager;
import org.thoughtcrime.securesms.connect.ApplicationDcContext;
import org.thoughtcrime.securesms.connect.DcHelper;
import org.thoughtcrime.securesms.database.Address;
import org.thoughtcrime.securesms.database.loaders.BucketedThreadMediaLoader;
@@ -38,7 +40,9 @@ import java.util.Collection;
import java.util.Locale;
public class ProfileGalleryFragment
extends Fragment implements LoaderManager.LoaderCallbacks<BucketedThreadMediaLoader.BucketedThreadMedia>, ProfileGalleryAdapter.ItemClickListener
extends Fragment
implements LoaderManager.LoaderCallbacks<BucketedThreadMediaLoader.BucketedThreadMedia>,
ProfileGalleryAdapter.ItemClickListener, DcEventCenter.DcEventDelegate
{
public static final String LOCALE_EXTRA = "locale_extra";
public static final String CHAT_ID_EXTRA = "chat_id";
@@ -49,13 +53,15 @@ public class ProfileGalleryFragment
private ActionMode actionMode;
private ActionModeCallback actionModeCallback = new ActionModeCallback();
protected int chatId;
protected Locale locale;
private ApplicationDcContext dcContext;
private int chatId;
private Locale locale;
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
dcContext = DcHelper.getContext(getContext());
chatId = getArguments().getInt(CHAT_ID_EXTRA, -1);
locale = (Locale)getArguments().getSerializable(LOCALE_EXTRA);
if (locale == null) throw new AssertionError();
@@ -79,9 +85,22 @@ public class ProfileGalleryFragment
this.recyclerView.setLayoutManager(gridManager);
this.recyclerView.setHasFixedSize(true);
dcContext.eventCenter.addObserver(DcContext.DC_EVENT_MSGS_CHANGED, this);
dcContext.eventCenter.addObserver(DcContext.DC_EVENT_INCOMING_MSG, this);
return view;
}
@Override
public void onDestroyView() {
dcContext.eventCenter.removeObservers(this);
super.onDestroyView();
}
@Override
public void handleEvent(int eventId, Object data1, Object data2) {
getLoaderManager().restartLoader(0, null, this);
}
private int getCols() {
return getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE? 5 : 3;
}
@@ -174,7 +193,6 @@ public class ProfileGalleryFragment
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setMessage(confirmMessage);
builder.setCancelable(true);
final DcContext dcContext = DcHelper.getContext(getContext());
builder.setPositiveButton(R.string.delete, (dialogInterface, i) -> {
new ProgressDialogAsyncTask<DcMsg, Void, Void>(getContext(),
@@ -18,14 +18,11 @@ import org.thoughtcrime.securesms.connect.ApplicationDcContext;
import org.thoughtcrime.securesms.connect.DcHelper;
import org.thoughtcrime.securesms.contacts.ContactSelectionListItem;
import org.thoughtcrime.securesms.mms.GlideRequests;
import org.thoughtcrime.securesms.util.LRUCache;
import org.thoughtcrime.securesms.util.StickyHeaderDecoration.StickyHeaderAdapter;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Locale;
import java.util.Map;
public class ProfileSettingsAdapter extends RecyclerView.Adapter
implements StickyHeaderAdapter<ProfileSettingsAdapter.HeaderViewHolder>
@@ -42,10 +39,6 @@ public class ProfileSettingsAdapter extends RecyclerView.Adapter
public static final int SETTING_SOUND = 320;
public static final int SETTING_VIBRATE = 330;
private static final int MAX_CACHE_SIZE = 100;
private final Map<Integer,SoftReference<DcContact>> recordCache =
Collections.synchronizedMap(new LRUCache<>(MAX_CACHE_SIZE));
private final @NonNull Context context;
private final @NonNull Locale locale;
private final @NonNull ApplicationDcContext dcContext;
@@ -106,24 +99,6 @@ public class ProfileSettingsAdapter extends RecyclerView.Adapter
return itemData.size();
}
private @NonNull DcContact getContact(int position) {
if(position<0 || position>=itemData.size() || itemData.get(position).type!=ItemData.TYPE_MEMBER) {
return new DcContact(0);
}
final SoftReference<DcContact> reference = recordCache.get(position);
if (reference != null) {
final DcContact fromCache = reference.get();
if (fromCache != null) {
return fromCache;
}
}
final DcContact fromDb = dcContext.getContact(itemData.get(position).contactId);
recordCache.put(position, new SoftReference<>(fromDb));
return fromDb;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View itemView) {
super(itemView);
@@ -175,7 +150,7 @@ public class ProfileSettingsAdapter extends RecyclerView.Adapter
name = context.getString(R.string.qrshow_title);
}
else {
dcContact = getContact(i);
dcContact = dcContext.getContact(itemData.get(i).contactId);
name = dcContact.getDisplayName();
addr = dcContact.getAddr();
}
@@ -285,7 +260,6 @@ public class ProfileSettingsAdapter extends RecyclerView.Adapter
itemData.add(new ItemData(ItemData.TYPE_SECONDARY_SETTING, SETTING_BLOCK_CONTACT, context.getString(R.string.menu_block_contact)));
}
recordCache.clear();
notifyDataSetChanged();
}
}
@@ -15,6 +15,8 @@ import android.widget.Toast;
import com.b44t.messenger.DcChat;
import com.b44t.messenger.DcChatlist;
import com.b44t.messenger.DcContact;
import com.b44t.messenger.DcContext;
import com.b44t.messenger.DcEventCenter;
import org.thoughtcrime.securesms.connect.ApplicationDcContext;
import org.thoughtcrime.securesms.connect.DcHelper;
@@ -25,7 +27,8 @@ import org.thoughtcrime.securesms.util.ViewUtil;
import java.util.Locale;
public class ProfileSettingsFragment extends Fragment implements ProfileSettingsAdapter.ItemClickListener {
public class ProfileSettingsFragment extends Fragment
implements ProfileSettingsAdapter.ItemClickListener, DcEventCenter.DcEventDelegate {
public static final String LOCALE_EXTRA = "locale_extra";
public static final String CHAT_ID_EXTRA = "chat_id";
@@ -37,9 +40,7 @@ public class ProfileSettingsFragment extends Fragment implements ProfileSettings
private Locale locale;
private ApplicationDcContext dcContext;
protected int chatId;
private DcChat dcChat;
private int contactId;
private DcContact dcContact;
@Override
public void onCreate(Bundle bundle) {
@@ -50,10 +51,6 @@ public class ProfileSettingsFragment extends Fragment implements ProfileSettings
chatId = getArguments().getInt(CHAT_ID_EXTRA, -1);
contactId = getArguments().getInt(CONTACT_ID_EXTRA, -1);
dcContext = DcHelper.getContext(getContext());
// if given, the ids really belong together, this is checked in ProfileActivity
if (contactId>0) { dcContact = dcContext.getContact(contactId); }
if (chatId>0) { dcChat = dcContext.getChat(chatId); }
}
@Override
@@ -68,14 +65,34 @@ public class ProfileSettingsFragment extends Fragment implements ProfileSettings
update();
dcContext.eventCenter.addObserver(DcContext.DC_EVENT_CHAT_MODIFIED, this);
dcContext.eventCenter.addObserver(DcContext.DC_EVENT_CONTACTS_CHANGED, this);
dcContext.eventCenter.addObserver(DcContext.DC_EVENT_MSGS_CHANGED, this);
dcContext.eventCenter.addObserver(DcContext.DC_EVENT_INCOMING_MSG, this);
return view;
}
@Override
public void onDestroyView() {
dcContext.eventCenter.removeObservers(this);
super.onDestroyView();
}
@Override
public void handleEvent(int eventId, Object data1, Object data2) {
update();
}
private void update()
{
int[] memberList = null;
DcChatlist sharedChats = null;
DcChat dcChat = null;
DcContact dcContact = null;
if (contactId>0) { dcContact = dcContext.getContact(contactId); }
if (chatId>0) { dcChat = dcContext.getChat(chatId); }
if(dcChat!=null && dcChat.isGroup()) {
memberList = dcContext.getChatContacts(chatId);
}
@@ -102,7 +119,7 @@ public class ProfileSettingsFragment extends Fragment implements ProfileSettings
}
private void onContactAddrClicked() {
String address = dcContact.getAddr();
String address = dcContext.getContact(contactId).getAddr();
new AlertDialog.Builder(getContext())
.setTitle(address)
.setItems(new CharSequence[]{
@@ -125,6 +142,7 @@ public class ProfileSettingsFragment extends Fragment implements ProfileSettings
}
private void onEditContactName() {
DcContact dcContact = dcContext.getContact(contactId);
final EditText txt = new EditText(getActivity());
txt.setText(dcContact.getName());
new AlertDialog.Builder(getActivity())