Skip to content

Instantly share code, notes, and snippets.

@Avjeet
Last active April 7, 2020 13:13
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 Avjeet/c7461afb2a2e9e91eb28c2e4160243e7 to your computer and use it in GitHub Desktop.
Save Avjeet/c7461afb2a2e9e91eb28c2e4160243e7 to your computer and use it in GitHub Desktop.
public class BroadcastSimulator implements ListItemClicked {
private static final int SIZE = 10;
public static final int LOAD_MORE_UP = 45;
private final AdapterBroadcastComments adapter;
private HashMap<Integer, SparseArray<List<BroadcastComment>>> commentMap;
private final Context context;
private final int sessionId;
private Long duration;
private Long startTimeInSec;
private int timer = 0;
private boolean listen;
private RecyclerView rvComments;
public BroadcastSimulator(Context context, long sessionStartTime, long duration, int sessionId, RecyclerView rvComments) {
this.startTimeInSec = sessionStartTime / 1000;
this.duration = duration;
this.context = context;
this.sessionId = sessionId;
this.rvComments = rvComments;
commentMap = new HashMap<>();
((LinearLayoutManager) rvComments.getLayoutManager()).setStackFromEnd(false);
rvComments.setHasFixedSize(true);
adapter = new AdapterBroadcastComments(context, new ArrayList<>(), this, startTimeInSec.intValue());
rvComments.setAdapter(adapter);
Log.d("BrSim", String.format("starttime- %d | duration - %d | sessionid - %d", sessionStartTime, duration, sessionId));
fetchCommentsForList(SIZE);
}
public void tick(int timeinMilliSec) {
Log.d("BrSim", String.format("tickTime - %d", timeinMilliSec));
if (timer == timeinMilliSec) {
return;
} else {
timer = timeinMilliSec;
}
int currentSecInFrame = (timer % SIZE);
int currentFrame = currentSecInFrame == 0 ? timer : ((timer / SIZE) * SIZE) + SIZE;
int nextFrame = currentFrame + SIZE;
int previousFrame = currentFrame - SIZE;
Log.d("BrSim", String.format("tickTime - %d | previous- %d | current- %d | next - %d", timer, previousFrame, currentFrame, nextFrame));
List<BroadcastComment> comments = null;
SparseArray<List<BroadcastComment>> array = commentMap.get(currentFrame);
if (array != null) {
comments = array.get(timer % SIZE, null);
}
if (comments != null && !comments.isEmpty()) {
Log.d("BrSim", String.format("Comment Shown - %s", comments.size() + ""));
adapter.setBroadcastComments(comments);
smoothScroll();
}
if (currentSecInFrame < 2 || currentSecInFrame > SIZE - 2) {
checkForMoreData(nextFrame, previousFrame);
}
}
/**
* Method to fetch comments for frame specified by
*
* @param time is the end value of frame time
*/
private void fetchCommentsForList(int time) {
FSBroadcast.getInstance().getCommentsFromTime(context, sessionId, startTimeInSec, time - SIZE, time, new ApiCallback<List<BroadcastComment>>() {
@Override
public void onResponse(List<BroadcastComment> response) {
parseDataIntoMap(response, time - SIZE, time);
}
@Override
public void onFail(String reason) {
}
});
}
/**
* Method to parse the data into sublist and insert them
* in a sparse array and further into hashmap.
*
* @param response list
* @param responseStartTime
* @param responseEndTime specifies the frame
*/
private void parseDataIntoMap(List<BroadcastComment> response, int responseStartTime, int responseEndTime) {
for (int frameStartTime = responseStartTime; frameStartTime < responseEndTime; frameStartTime += SIZE) {
int frameEndTime = frameStartTime + SIZE;
SparseArray<List<BroadcastComment>> sparseArray = new SparseArray<>();
if (response.isEmpty()) {
commentMap.put(responseEndTime, null);
return;
}
int startOffset = 0;
int pos;
Long timeInSec = response.get(0).getCreatedAt() - startTimeInSec;
Long timeDiff = 0L;
for (pos = 0; pos < response.size(); pos++) {
timeDiff = (response.get(pos).getCreatedAt() - startTimeInSec) - frameStartTime;
if (!timeInSec.equals(timeDiff)) {
sparseArray.put(timeInSec.intValue() + 1, response.subList(startOffset, pos));
timeInSec = timeDiff;
startOffset = pos;
}
}
sparseArray.put(timeDiff.intValue() + 1, response.subList(startOffset, pos));
commentMap.put(frameEndTime, sparseArray);
}
}
private void parseDataIntoMap(List<BroadcastComment> response) {
if (response.isEmpty()) {
return;
}
SparseArray<List<BroadcastComment>> sparseArray = new SparseArray<>();
int startOffset = 0;
int pos;
Long timeInSec = (response.get(0).getCreatedAt() - startTimeInSec);
Long frameEndTime = timeInSec / SIZE == 0L ? timeInSec : ((timeInSec / SIZE) * SIZE) + SIZE;
for (pos = 0; pos < response.size(); pos++) {
Long timeDiff = (response.get(pos).getCreatedAt() - startTimeInSec) % SIZE;
if (!timeInSec.equals(timeDiff)) {
if (commentMap.containsKey(frameEndTime.intValue())) {
SparseArray currentArray = commentMap.get(frameEndTime.intValue());
if (currentArray == null || currentArray.size() < sparseArray.size()) {
sparseArray.put(timeInSec.intValue() + 1, response.subList(startOffset, pos));
}
}
sparseArray.put(timeInSec.intValue() + 1, response.subList(startOffset, pos));
commentMap.put(frameEndTime.intValue(), sparseArray);
sparseArray.clear();
timeInSec = timeDiff;
startOffset = pos;
frameEndTime = timeInSec / SIZE == 0L ? timeInSec : ((timeInSec / SIZE) * SIZE) + SIZE;
}
}
sparseArray.put(timeInSec.intValue() + 1, response.subList(startOffset, pos));
commentMap.put(frameEndTime.intValue(), sparseArray);
}
private void checkForMoreData(int nextFrame, int previousFrame) {
int sec = (timer % SIZE);
if (sec > SIZE - 2) { //TODO:// check that sec doesnot exceed video duration
if (!commentMap.containsKey(nextFrame)) {
fetchCommentsForList(nextFrame);
}
} else if (sec < 2) {
if (!commentMap.containsKey(previousFrame) && previousFrame > 0) {
fetchCommentsForList(previousFrame);
}
}
}
public void toggleListener(boolean listen) {
this.listen = listen;
}
public boolean isListening() {
return listen;
}
private void smoothScroll() {
new Handler().postDelayed(() -> {
if (rvComments != null && rvComments.getLayoutManager() != null) {
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) rvComments.getLayoutManager();
if (linearLayoutManager.findFirstVisibleItemPosition() < 2) {
rvComments.smoothScrollToPosition(0);
}
}
}, 100);
}
public void seekToPos(int time) {
timer = time;
adapter.clear();
FSBroadcast.getInstance().getCommentsUptoTime(context, sessionId, startTimeInSec, time, new ApiCallback<List<BroadcastComment>>() {
@Override
public void onResponse(List<BroadcastComment> response) {
//adapter.setBroadcastComments(response);
// smoothScroll();
adapter.getCommentList().addAll(response);
adapter.notifyDataSetChanged();
parseDataIntoMap(response);
}
@Override
public void onFail(String reason) {
}
});
}
@Override
public void itemClicked(int position, Object obj, int type) {
if (type == LOAD_MORE_UP) {
BroadcastComment broadcastComment = (BroadcastComment) obj;
FSBroadcast.getInstance().getCommentsUptoId(context, sessionId, broadcastComment.getId(), new ApiCallback<List<BroadcastComment>>() {
@Override
public void onResponse(List<BroadcastComment> response) {
adapter.addBroadcastComments(response);
parseDataIntoMap(response);
}
@Override
public void onFail(String reason) {
}
});
} else if(type == COMMENT_ITEM_SELECTED){
Router.with(context).startUserProfileActivity(((BroadcastComment) obj).getSportsFan().getId(),
"commentary_broadcast", 0, false);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment