Giter Site home page Giter Site logo

Comments (4)

anas-ambri avatar anas-ambri commented on June 28, 2024

Update on this:

Had to subclass ConversationActivity, and set that as a delegate:

public class SupportActivity extends ConversationActivity {

    @Override
    public boolean shouldTriggerAction(MessageAction messageAction) {
        if (messageAction.getUri().startsWith("special:")) {
            //Do something special
            return true;
        } else {
            return super.shouldTriggerAction(messageAction);
        }
    }
}

Smooch.getConversation().setDelegate(new SupportActivity());

This seems to do the trick, but I am not sure if it would break something internally. It is also an awful hack (you know, for the whole new SupportActivity() thing). Any ideas?

from sunshine-conversations-android.

alavers avatar alavers commented on June 28, 2024

Hey Anas!

Since I am not interested in all the other methods, there is no way for me to use this without breaking the rest of the functionality.

I'm not sure what you mean here, can you clarify? Overriding the delegate object with empty implementations (for the void methods at least) shouldn't break any functionality.

from sunshine-conversations-android.

anas-ambri avatar anas-ambri commented on June 28, 2024

Hey Andrew,

So yeah, reading through it again, I definitely wasn't clear enough.
Here is my understanding of how Smooch initialization works:
1- When init is called, a new "SmoochManager" is created. This manager takes care of launching the service, listens to the Application lifecycle, and does all the bookkeeping for the queue system. (Note that "SmoochManager" might not be the name of the actual class internally; this is just a name I made up, since the code is obfuscated).
2- This manager takes care of creating the Conversation object.
3- Finally, ConversationActivity implements the Delegate interface. This suggests that it is the ConversationActivity that is used as delegate at some point. (This is my assumption).
4- Based on the previous assumption, then, at some point, between init and ConversationActivity.show() are called, ConversationActivity must be set as the Delegate.

Now, to the problem:

  • I can't seem to find at which point ConversationActivity (or any other object) becomes the Delegate. The natural place would be inside the onCreate of ConversationActivity, but there is nothing there.
  • I haven't been able to find any other class that implements Delegate other than ConversationActivity.
  • My understanding is that Conversation has a single reference to its Delegate. Thus, when providing a new object as Delegate (through Smooch.getConversation().setDelegate()), it is the delegate that was set last that gets called back. (As opposed to, say, listeners, where you can have plenty of them).

Now, say I wanted to change the behaviour of Smooch when a MessageAction is received I would have to set the delegate myself, after my init:

Smooch.init(this, "TOKEN");
Smooch.getConversation().setDelegate(new Conversation.Delegate() {
            @Override
            public void onMessagesReceived(Conversation conversation, List<Message> list) { }

            @Override
            public void onUnreadCountChanged(Conversation conversation, int i) { }

            @Override
            public void onMessageSent(Message message, MessageUploadStatus messageUploadStatus) { }

            @Override
            public void onInitializationStatusChanged(InitializationStatus initializationStatus) { }

            @Override
            public void onPaymentProcessed(MessageAction messageAction, PaymentStatus paymentStatus) { }

            @Override
            public boolean shouldTriggerAction(MessageAction messageAction) { 
                 if(messageAction.getUri().startsWith("special:") {
                       return false;
                 } else {
                      //Call super to handle the other cases
                 }
            }

            @Override
            public void onCardSummaryLoaded(CardSummary cardSummary) { }

            @Override
            public void onSmoochShown() { }

            @Override
            public void onSmoochHidden() { }
        });

Now, as you can see, there is no way for me to use the super implementation of delegate. Basically, ConversationActivity's implementation of Delegate is not called here. This is bad, because ConversationActivity is probably doing very important stuff in all these methods. This was the gist of what I put on that first comment.

The only alternative is to subclass ConversationActivity, and override only the behaviour that I want to change (thus, my SupportActivity). Passing this as the delegate requires doing this:

Smooch.getConversation().setDelegate(new SupportActivity());

which, to me, feels wrong, because:

  • You are not supposed to call new on an Activity object
  • What happens if setDelegate is called some time later? (I don't know when that is done, so I don't have any way to confirm)
  • This feels very iOS-y, with an Activity implementing a protocol.

I feel that I have been reading way too much into all of this, and that the solution is probably simpler. Plus, the code works, so I hope you are able to see where my confusion is stemming from.

Also, thanks for taking a look into this, I know you guys must be super busy.

from sunshine-conversations-android.

jugarrit avatar jugarrit commented on June 28, 2024

Hey Anas, sorry it's taken us so long to get back to you.
The ConversationActivity does in fact implement the delegate, but it is a separate delegate from the one you will be overriding by calling setDelegate().

For our ConversationActivity, we are using the method setSmoochUiDelegate() and it is being called in the activity's onPause.

As long as you stick to using setDelegate(), you will not run into any problems :)

from sunshine-conversations-android.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.