Skip to content

Instantly share code, notes, and snippets.

@feresr
Created August 23, 2016 01:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feresr/d0028cfa3c6de40f6e02c9170ec06dc1 to your computer and use it in GitHub Desktop.
Save feresr/d0028cfa3c6de40f6e02c9170ec06dc1 to your computer and use it in GitHub Desktop.
package com.productify.urge.usecase;
import android.os.Handler;
import android.util.Log;
import com.google.gson.Gson;
import com.productify.urge.common.UseCase;
import com.productify.urge.model.Message;
import com.productify.urge.model.Profile;
import com.pubnub.api.PubNub;
import com.pubnub.api.callbacks.SubscribeCallback;
import com.pubnub.api.enums.PNStatusCategory;
import com.pubnub.api.models.consumer.PNStatus;
import com.pubnub.api.models.consumer.pubsub.PNMessageResult;
import com.pubnub.api.models.consumer.pubsub.PNPresenceEventResult;
import java.util.Collections;
import javax.inject.Inject;
/**
* Created by feresr on 8/08/16.
* This use case connects to the PubNub server and informs its listener of incoming messages.
* Usage: Simply setting a listener will start the PunNub subscription. Setting the listener to null will cancel it.
*/
public class InboxUseCase extends UseCase<InboxUseCase.InboxCallbackListener> {
private final static String TAG = InboxUseCase.class.getSimpleName();
private PubNub pubnub;
private Profile profile;
private Handler handler;
@Inject
public InboxUseCase(PubNub pubnub, Profile profile) {
this.pubnub = pubnub;
this.profile = profile;
this.handler = new Handler();
pubnub.addListener(new SubscribeCallback() {
@Override
public void status(PubNub pubnub, PNStatus status) {
Log.e(TAG, "pubnub status: " + status.getCategory().toString());
if (status.getCategory() == PNStatusCategory.PNTimeoutCategory) {
pubnub.reconnect();
} else {
if (status.isError()) {
Log.d(TAG, status.getErrorData().toString());
}
}
}
@Override
public void message(PubNub pubnub, PNMessageResult m) {
try {
Log.d(TAG, "pubnub message: " + m.getMessage().toString());
final Message message = new Gson().fromJson(m.getMessage().toString(), Message.class);
message.setTimeToken(m.getTimetoken());
if (listener != null) {
handler.post(new Runnable() {
@Override
public void run() {
listener.onNewMessageReceived(message);
}
});
}
} catch (final Exception e) {
e.printStackTrace();
if (listener != null) {
handler.post(new Runnable() {
@Override
public void run() {
listener.onErrorReceivingMessage(e.getMessage());
}
});
}
}
}
@Override
public void presence(PubNub pubnub, PNPresenceEventResult presence) {
}
});
}
@Override
public void setListener(final InboxCallbackListener listener) {
super.setListener(listener);
String channel = profile.isMerchant() ? profile.getStore().getUrgeUid() : profile.getUrgeUid();
if (listener != null) {
pubnub.subscribe().withPresence()
.channels(Collections.singletonList(channel)) // subscribe to channels
.execute();
} else {
pubnub.unsubscribe()
.channels(Collections.singletonList(channel)) // subscribe to channels
.execute();
}
}
/**
* This interface must be implemented by those interested in receiving new messages
*/
public interface InboxCallbackListener {
void onNewMessageReceived(Message message);
void onErrorReceivingMessage(String error);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment