chat-sdk-android icon indicating copy to clipboard operation
chat-sdk-android copied to clipboard

Any closer to deleting contacts?

Open RyanElliott94 opened this issue 9 years ago • 14 comments

I know i have been banging on about this a lot but i was wondering if we're any closer to achieving it?

This is what i know works, the deleteContact method, i know because when you delete the contact, it does indeed get removed BUT it won't be gone from the list and when you restart the app, it will crash, the app will also crash when you click the contact after deleting. I think that is because of what @kykrueger told me about.

Now, i remember @kykrueger saying that this is because the contact has an open thread so it will keep the name of user in the list but not as a contact but a name related to an open thread, i was also told that i would need to search any open threads and delete the user from said thread before deleting the contact BUT that is where I'm struggling because i don't think i am quite getting how to do that at all!

So if possible, i either need to know exactly how to search current threads and then delete them or like before, post the code that i am currently using and wait to see if any of you (when you have the time of course) can use to the code and tweak it to get it to work!

Ryan

RyanElliott94 avatar Mar 23 '17 03:03 RyanElliott94

deleteContact method in BUser.class

   `@Override
    public void deleteContact(BUser user) {
    List contactLinkList = getContactLinks();

    ContactLink contactLink = new ContactLink();
    // Set link owner
    contactLink.setLinkOwnerBUser(this);

    contactLink.setLinkOwnerBUserDaoId(this.getId());

    contactLink.setBUserDaoId(user.getId());

    daoSession.delete(user);

    myDao.delete(user);

    contactLinkList.remove(user);

    this.update();
}`

Exactly how i use the method

       ` if (onItemLongClickListener != null) {
        listView.setOnItemLongClickListener(onItemLongClickListener);
         } else
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, final    long id) {
                final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                String[] items = {"Delete Contact"};
                builder.setItems(items, new DialogInterface.OnClickListener() {
                @Override
                 public void onClick(DialogInterface dialog, int which) {
                 final BUser currentUser =     BNetworkManager.sharedManager().getNetworkAdapter().currentUserModel();

                 final BUser clickedUser = DaoCore.fetchEntityWithEntityID(BUser.class,  adapter.getItem(position).getEntityID());

                        List<BThread> openThreadList = BNetworkManager.sharedManager().getNetworkAdapter().currentUserModel().getThreads(BThread.Type.Private);

                        if(openThreadList.contains(clickedUser)) {
                            openThreadList.remove(clickedUser);
                            currentUser.deleteContact(clickedUser);
                            loadSourceUsers();
                        }else{
                            currentUser.deleteContact(clickedUser);
                           loadSourceUsers();
                        }
                    }

                });
              builder.show();
                return false;
            }
        });`

RyanElliott94 avatar Mar 23 '17 03:03 RyanElliott94

Hi,

I think the problem is that you are deleting the user. The app stores a lot of users and these should never be deleted because they are needed in the threads. But it keeps track of the contacts using the ContactLink object.

You should remove these lines:

    daoSession.delete(user);
    myDao.delete(user);

And remove the contact links instead. Look at how contacts are displayed:

// BUser.java 

    public List<BUser> getContacts() {
        List<BUser> contactList = new ArrayList<>();
        List<ContactLink> contactLinks;
        // For some reason the default ContactLinks do not persist, have to find in DB

        contactLinks = DaoCore.fetchEntitiesWithProperty(ContactLink.class,
                ContactLinkDao.Properties.LinkOwnerBUserDaoId, this.getId());
        for (ContactLink contactLink : contactLinks){
            contactList.add(contactLink.getBUser());
        }

        return contactList;
    }

We loop over the contact links and then get the user for each link.

bensmiley avatar Mar 23 '17 04:03 bensmiley

I had a go but still can't get it too work!

BUser.class

    `@Override
    public void deleteContact(BUser user) {
    List<BUser> contactList = new ArrayList<>();

    List<ContactLink> contactLinks;

    List contactLinkList = getContactLinks();

    contactLinks = DaoCore.fetchEntitiesWithProperty(ContactLink.class,
            ContactLinkDao.Properties.BUserDaoId, this.getId());

    for (ContactLink contactLink : contactLinks){
        user = contactLink.getBUser();
        contactLinks.remove(user);
        contactList.remove(user);
    }
}`

It is highly likely that I've not understood again but i am also running on little sleep so it is to be expected lol

RyanElliott94 avatar Mar 23 '17 15:03 RyanElliott94

It looks to me like now you're deleting the user from the contact link. The database schema looks like this:

User 1
    - Contact Link (with User 2)
    - Contact Link (with User 3)

I think what you are doing will result in something like:

User 1
    - Contact Link (with null)
    - Contact Link (with User 3)

If you set the contact owner to null that would solve your problem but you would end up with an orphan link.

contactLink.setLinkOwnerBUser(null);

I think you need to do something like this:

    // Added to BUser.java
    @Override
    public void removeContact(BUser contactUser) {
        if (newContact.equals(this))
            return;

        // Retrieve contacts
        List contacts = getContacts();
        // Check if user is already in contact list
        if ( !contacts.contains(newContact)) return;

        // refresh contact list before updating
        resetContactLinks();
        List contactLinkList = getContactLinks();

        for (ContactLink contactLink : contactLinkList){
            user = contactLink.getBUser();
            if(user.equals(contactUser)) {
                contactLink.setBUser(null);
                contactLink.setLinkOwnerBUser(null);
                myDao.delete(contactLink);
                break;
            }
       }
        this.update();
    }

I've not tested this code but it should be pretty much correct.

bensmiley avatar Mar 24 '17 02:03 bensmiley

I couldn't get it to work, there were problems with for (ContactLink contactLink : contactLinkList) along with myDao.delete(contactLink); because it needs to be a BUser to delete.

I have decided that i will have to give up for the time being to give up trying to do it because it is really holding back the release but i am very desperate to have this feature added so depending on how much it would cost and if you can guarantee that the work would be carried out quickly, i may have to pay for either you or one of the team to add it for me, even it's just for me.

I haven't got much money to spare as i was sacked from my job about a month or 2 ago and I'm still living on the holiday pay that i got so I'll only be able to go up to £50 but, I'd ideally want that to under that figure!

So yes, if it will be possible then please let me know because at the moment I'm worried that i will end up losing a lot of users or end up with loads of bad reviews because of it!

Also this isn't overly important right now but do you think you will ever add a function to block users from the contact list?

I forgot to mention that i have also already paid £80 for the audio module.

Thanks

RyanElliott94 avatar Mar 24 '17 13:03 RyanElliott94

Hi!

I think you are almost there, and I have another suggestion before we talk about making this a paid project (since it seems so close!).

There is a problem with the suggestion to use ´myDao.delete(contactLink)´ from the BUser class. This is because you want to delete the myDao variable for the ContactLink and not the BUser. Conveniently there is a method for this.

To use it, you should replace

myDao.delete(contactLink);

with

contactLink.delete();

Realistically we should never have to touch the myDao object since it is a byproduct of greenDAO that is meant to be abstracted away. Hence the delete functions (which are generated for every greenDAO entity).

What is the other problem you are having with the list? If you clarify I could try to help!

kykrueger avatar Mar 24 '17 17:03 kykrueger

So this is the closet I've gotten so far. Right now when you go to delete the contact, it will remove the whole list instead of the selected contact but when you close the app completely and start it again, all the contacts re-appear!

I feel like all i need right now is just a line or two more then it will work.

    @Override
    public void deleteContact(BUser user) {
    BUser currentUser = BNetworkManager.sharedManager().getNetworkAdapter().currentUserModel();
    List<BUser> contactList = currentUser.getContacts();
    List<ContactLink> contactLinks;

    contactLinks = DaoCore.fetchEntitiesWithProperty(ContactLink.class,
            ContactLinkDao.Properties.LinkOwnerBUserDaoId, this.getId());

    for (ContactLink contactLink : contactLinks){
        contactLink.setBUser(null);
        contactLink.setLinkOwnerBUser(null);
        contactLink.delete();
        contactList.remove(user);
    }
    this.update();
}

I tried to implement all of the suggestions that I've had so far and that it what i have come up with

RyanElliott94 avatar Mar 25 '17 15:03 RyanElliott94

Your deleteContact method looks good, but you dropped if(user.equals(contactUser)) you need to put this statement back to make sure you are only deleting your target user.

Furthermore, the users are still likely getting added back to the list due to open private chats. Are you still removing the threads like this,


        final BUser clickedUser = DaoCore.fetchEntityWithEntityID(BUser.class,  adapter.getItem(position).getEntityID());

        List<BThread> openThreadList = BNetworkManager.sharedManager().getNetworkAdapter().currentUserModel().getThreads(BThread.Type.Private);
        if(openThreadList.contains(clickedUser)) {
            openThreadList.remove(clickedUser);
            currentUser.deleteContact(clickedUser);
            loadSourceUsers();
        }else{
            currentUser.deleteContact(clickedUser);
            loadSourceUsers();
        }

Take a look at how the threads were being removed here. You retrieve your list of threads, and you are checking if the list contains a user. This will always enter the else portion of your if statement since the list of threads does not contain and user objects.

What you need to fix this to do is loop through each of the open threads, and check that object for the users instead. And when you find a thread containing that user, you need to make the active user leave the thread.

    for (BThread thread : openThreadList) {
        // Do nothing if the target user is not in the thread.
        if ( ! thread.hasUser(clickedUser) continue; 

        // Leave the thread.
        BNetworkManager.sharedManager().getNetworkAdapter().deleteThread(thread);
        // Remove it from the current list for completeness.
        openThreadList.remove(thread);
    }

   // Finish deleting the contact like before.
   currentUser.deleteContact(clickedUser);
   loadSourceUsers();

However, with all of this working, there is still a bit of a user experience quirk. The other user will still see the thread, and might try sending messages. These messages will not be received by the user who deleted them. Maybe that is the desired experience, but if that user then deletes the chat, and opens a new one, it could start a new chat that does function, and re-adds the user.

Furthermore, private group chats that contain this user will be left instead of kicking the user!

Hope this helps, -- Kyle

kykrueger avatar Mar 25 '17 16:03 kykrueger

Still not working, i honestly don't know what else to try now!

ChatSDKAbstractContactsFragment.java

      BUser currentUser =    BNetworkManager.sharedManager().getNetworkAdapter().currentUserModel();
       BUser clickedUser = DaoCore.fetchEntityWithEntityID(BUser.class,  adapter.getItem(position).getEntityID());
                        List<BThread> openThreadList = BNetworkManager.sharedManager().getNetworkAdapter().currentUserModel().getThreads(BThread.Type.Private);
        for (BThread thread : openThreadList) {
        // Do nothing if the target user is not in the thread.
         if (!thread.hasUser(clickedUser)) continue;
         // Leave the thread.
         BNetworkManager.sharedManager().getNetworkAdapter().deleteThread(thread);
          // Remove it from the current list for completeness.
         openThreadList.remove(thread);
         }
          // Finish deleting the contact like before.
         currentUser.deleteContact(clickedUser);
         loadData();
                    }
                });
              builder.show();

BUser.java

   @Override
   public void deleteContact(BUser user) {
    // Retrieve contacts
    List contacts = getContacts();
    // refresh contact list before updating
    resetContactLinks();

    contactLinks = DaoCore.fetchEntitiesWithProperty(ContactLink.class,
            ContactLinkDao.Properties.LinkOwnerBUserDaoId, this.getId());

    for (ContactLink contactLink : contactLinks){
        if(user.equals(this)) {
            contactLink.setBUser(null);
            contactLink.setLinkOwnerBUser(null);
            contactLink.delete();
            contacts.remove(user);
        }
    }
    this.update();
}

I also run into this error when i try to delete the contact at the end of the list:

      E/AndroidRuntime: FATAL EXCEPTION: main
                                                                  Process: com.skintmedia.voice, PID: 11237
                                                                  java.util.ConcurrentModificationException
                                                                      at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)
                                                                      at com.braunster.chatsdk.fragments.abstracted.ChatSDKAbstractContactsFragment$4$1.onClick(ChatSDKAbstractContactsFragment.java:444)

RyanElliott94 avatar Mar 25 '17 17:03 RyanElliott94

Ok,

Don't give up!

First things first, can you clarify this error? Is it only happening when you try to delete the user at the end of the contact list, or do you mean it happens every time when you try to delete the user after it has went through the list of threads?

As for not working, is it still that the users are being added back after re-starting the app, or is the delete operation failing all together?

I don't think it will take much more time, and I am happy to continue providing support like this, but if you are working on a deadline I suggest we work out a quote for priority support hours. With that we could arrange a few hours to work together on the problem. Please follow through at our support email ([email protected]) for a quote if you are interested.

-- Kyle

kykrueger avatar Mar 25 '17 20:03 kykrueger

Every time i try to delete the user after it has went through the list of threads and it's failing all together unfortunately.

Right now i don't have a deadline, just a goal to get the app released but as it stands i will need to release the app even without the ability to delete contacts and then just add it as soon as possible afterwards

RyanElliott94 avatar Mar 25 '17 20:03 RyanElliott94

Hi Ryan, You managed to delete all the contacts previously. I think that if you added in the conditional statement, you could just delete one contact. You probably need to delete this line:

// UserAddedListener.java
// Users that are members of threads are shown in contacts
currentUser.addContact(bUser);

It will automatically create a contact for each user the current user has a thread with.

bensmiley avatar Mar 29 '17 02:03 bensmiley

Right 2 things, 1. you are an absolute legend! i didn't even know the file existed and what you suggested works! 2. I'm still unable to only delete the one contact, i have tried so far if(this.equals(user)), if(user.equals(user), if(this == user) and i also tried if(user != null) but they haven't worked!

One thing i do need to know is if the code i am using to get the clicked user is in fact working correctly.

 `BUser currentUser = BNetworkManager.sharedManager().getNetworkAdapter().currentUserModel();
  BUser clickedUser = DaoCore.fetchEntityWithEntityID(BUser.class,    adapter.getItem(position).getEntityID());

 currentUser.deleteContact(clickedUser);`

Thanks

Ryan

RyanElliott94 avatar Mar 29 '17 14:03 RyanElliott94

Quick update! I can now successfully delete contacts!! I'd like to thank all of you for your help!

Here's the final code just in case you want to add it to the SDK:

 // ChatSDKAbstractContactsFragment.java

 BUser currentUser = BNetworkManager.sharedManager().getNetworkAdapter().currentUserModel();
 BUser clickedUser = DaoCore.fetchEntityWithEntityID(BUser.class, adapter.getItem(position).getEntityID());

 List<BThread> openThreadList = BNetworkManager.sharedManager().getNetworkAdapter().currentUserModel().getThreads(BThread.Type.Private);
  for (BThread thread : openThreadList) {
  if(!thread.hasUser(clickedUser)) continue;

   BNetworkManager.sharedManager().getNetworkAdapter().deleteThread(thread);

   openThreadList.remove(thread);
            }
   currentUser.deleteContact(clickedUser);
   loadDataOnBackground();
                    }


  // BUser.java

  @Override
public void deleteContact(BUser user) {
    // Retrieve contacts
    List contacts = getContacts();
    // refresh contact list before updating
    resetContactLinks();

    contactLinks = DaoCore.fetchEntitiesWithProperty(ContactLink.class,
            ContactLinkDao.Properties.BUserDaoId, user.getId());

    for (ContactLink contactLink : contactLinks){
        if(contactLinks != null) {
            contactLink.setBUser(null);
            contactLink.setLinkOwnerBUser(null);
            contactLink.delete();
            contacts.remove(user);
        }
        }
    resetContactLinks();
}


 // BUserEntity.java

 public abstract void deleteContact(BUser user);

Like Ben said, remove

 // UserAddedListener.java
// Users that are members of threads are shown in contacts
currentUser.addContact(bUser);

And that's it, done!

Thanks again

Ryan

RyanElliott94 avatar Mar 29 '17 16:03 RyanElliott94