Skip to content

Commit

Permalink
Integrated Socket.IO to stickers
Browse files Browse the repository at this point in the history
  • Loading branch information
Keith Leong Wei Mun authored and Keith Leong Wei Mun committed Apr 13, 2016
1 parent 8856e25 commit 3f443e8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ public class WorldScopeSocketService {
private static ArrayList<OnJoinEventListener> joinEventListeners = new ArrayList<>();
private static ArrayList<OnCommentEventListener> commentEventListeners = new ArrayList<>();
private static ArrayList<OnLeaveEventListener> leaveEventListeners = new ArrayList<>();
private static ArrayList<OnStickerEventListener> stickerEventListeners = new ArrayList<>();

// Event names
private static String EVENT_IDENTIFY = "identify";
private static String EVENT_JOIN = "join";
private static String EVENT_COMMENT = "comment";
private static String EVENT_LEAVE = "leave";
private static String EVENT_STICKER = "sticker";


// Connect to Socket.IO in App Server
Expand All @@ -58,13 +60,15 @@ private static void startListening() {
socket.on(EVENT_JOIN, onJoinEvent);
socket.on(EVENT_COMMENT, onCommentEvent);
socket.on(EVENT_LEAVE, onLeaveEvent);
socket.on(EVENT_STICKER, onStickerEvent);
}

private static void stopListening() {
socket.off(EVENT_IDENTIFY, onIdentifyEvent);
socket.off(EVENT_JOIN, onJoinEvent);
socket.off(EVENT_COMMENT, onCommentEvent);
socket.off(EVENT_LEAVE, onLeaveEvent);
socket.off(EVENT_STICKER, onStickerEvent);
}

// Emits an identify event, payload should be the current cookie
Expand Down Expand Up @@ -99,6 +103,14 @@ public static void emitLeave(String data) {
}
}

// Emits a sticker event, no payload
public static void emitSticker() {
if(isInitialized) {
Log.d(TAG, "Emitting sticker");
socket.emit(EVENT_STICKER);
}
}

// Adds the object as a listener if it is valid
public static boolean registerListener(Object listener) {

Expand Down Expand Up @@ -128,6 +140,13 @@ public static boolean registerListener(Object listener) {
return true;
}

if(listener instanceof OnStickerEventListener) {
Log.d(TAG, "Sticker listener added");
stickerEventListeners.add((OnStickerEventListener)listener);
return true;
}


return false;
}

Expand All @@ -154,6 +173,11 @@ public static void unregisterListener(Object listener) {
Log.d(TAG, "Removing Leave listener");
leaveEventListeners.remove(listener);
}

if(listener instanceof OnStickerEventListener) {
Log.d(TAG, "Removing Leave listener");
stickerEventListeners.remove(listener);
}
}

public interface OnIdentifyEventListener {
Expand All @@ -172,6 +196,10 @@ public interface OnLeaveEventListener {
void onLeaveEventEmitted(String data);
}

public interface OnStickerEventListener {
void onStickerEventEmitted();
}

// Generate an instance of the Emitter.Listener for identify
private static Emitter.Listener onIdentifyEvent = new Emitter.Listener() {
@Override
Expand Down Expand Up @@ -226,4 +254,14 @@ public void call(final Object... args) {
}
}
};

// Generate an instance of the Emitter.Listener for sticker
private static Emitter.Listener onStickerEvent = new Emitter.Listener() {
@Override
public void call(final Object... args) {
for (OnStickerEventListener listener: stickerEventListeners) {
listener.onStickerEventEmitted();
}
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
import android.widget.RelativeLayout;

import com.litmus.worldscope.R;
import com.litmus.worldscope.utility.WorldScopeSocketService;

import java.util.ArrayList;
import java.util.Random;

public class StickerFragment extends Fragment {
public class StickerFragment extends Fragment implements WorldScopeSocketService.OnStickerEventListener {

final String TAG = "StickerFragment";
final int CUT_OFF_RANGE = 500;
Expand Down Expand Up @@ -58,8 +59,19 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
return view;
}

@Override
public void onResume() {
super.onResume();
// Register this as a listener of WorldScopeSocketService
WorldScopeSocketService.registerListener(this);
}

public void sendStickers() {
Log.d(TAG, "Sending stickers");
WorldScopeSocketService.emitSticker();
}

private void showStickers() {
ImageView starView = createStarView();
insertIntoStarContainer(starView);
animateStarView(starView);
Expand Down Expand Up @@ -115,4 +127,16 @@ private AnimatorSet createWigglyAnimation(ImageView starView) {
animatorSet.setInterpolator(new LinearInterpolator());
return animatorSet;
}

@Override
public void onStickerEventEmitted() {
if(getActivity() != null) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
showStickers();
}
});
}
}
}

0 comments on commit 3f443e8

Please sign in to comment.