Advertisement
Guest User

Untitled

a guest
May 25th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.29 KB | None | 0 0
  1. package matchatmedia.matchat.chat.repository;
  2.  
  3. import android.text.TextUtils;
  4. import android.util.Log;
  5.  
  6. import com.google.firebase.database.DataSnapshot;
  7. import com.google.firebase.database.DatabaseError;
  8. import com.google.firebase.database.DatabaseReference;
  9. import com.google.firebase.database.FirebaseDatabase;
  10. import com.google.firebase.database.ValueEventListener;
  11.  
  12. import java.util.HashMap;
  13.  
  14. import matchatmedia.matchat.chat.data.Vote;
  15.  
  16. /**
  17. * Created by Rupesh Mishra on 2/21/2018.
  18. */
  19.  
  20. public class PlayerVoteRepository {
  21.  
  22. final DatabaseReference mVoteBestPlayerReference;
  23. final DatabaseReference mVoteBestPlayerUserMapReference;
  24. final DatabaseReference mVoteCountReference;
  25. OnBestPlayerVoteListener mVoteBestPlayerListener;
  26. private static final String TABLE_VOTE_BEST_PLAYER = "VoteBestPlayer";
  27. private static final String TABLE_USER_VOTE_MAP = "VoteBestPlayerUserMap";
  28. private static final String TABLE_VOTE_COUNT = "VoteCounts";
  29.  
  30. public static final String NODE_FIRST_HALF = "FirstHalf";
  31. public static final String NODE_SECOND_HALD = "SecondHalf";
  32. public static final String NODE_PLAYER = "Player";
  33.  
  34. public static final String TAG = PlayerVoteRepository.class.getSimpleName();
  35.  
  36. public interface OnBestPlayerVoteListener {
  37. void onMyLastVoteFetched(String playerId);
  38.  
  39. void onVotePosted(String playerId);
  40. }
  41.  
  42. public PlayerVoteRepository(String matchId) {
  43. mVoteBestPlayerReference = FirebaseDatabase.getInstance().getReference().child(TABLE_VOTE_BEST_PLAYER).child(matchId);
  44. mVoteBestPlayerUserMapReference = FirebaseDatabase.getInstance().getReference().child(TABLE_USER_VOTE_MAP).child(matchId);
  45. mVoteCountReference = FirebaseDatabase.getInstance().getReference().child(TABLE_VOTE_COUNT).child(matchId);
  46. }
  47.  
  48. public void setVotingListener(OnBestPlayerVoteListener listener) {
  49. this.mVoteBestPlayerListener = listener;
  50. }
  51.  
  52. public void checkMyLastVoteBestPlayer(String whichHalf, String myUserId) {
  53. mVoteBestPlayerUserMapReference.child(whichHalf).child(myUserId).addListenerForSingleValueEvent(new ValueEventListener() {
  54.  
  55. @Override
  56. public void onDataChange(DataSnapshot dataSnapshot) {
  57. if (dataSnapshot != null && dataSnapshot.exists()) {
  58. Log.v(TAG, "user has already voted for best player");
  59. String playerId = (String) dataSnapshot.getValue();
  60. if (mVoteBestPlayerListener != null) {
  61. mVoteBestPlayerListener.onMyLastVoteFetched(playerId);
  62. }
  63. } else {
  64. if (mVoteBestPlayerListener != null) {
  65. mVoteBestPlayerListener.onMyLastVoteFetched("");
  66. }
  67. }
  68. }
  69.  
  70. @Override
  71. public void onCancelled(DatabaseError databaseError) {
  72. Log.e(TAG, "database error - checkMyLastVoteBestPlayer - " + databaseError.getMessage());
  73. }
  74. });
  75. }
  76.  
  77. /**
  78. * Add user's vote for a player in a match
  79. *
  80. * @param myUserId
  81. * @param player_id
  82. */
  83. public void voteForPlayer(String whichHalf, String myUserId, String lastPlayerId, String player_id, String player_name) {
  84. if (!TextUtils.isEmpty(lastPlayerId)) {
  85. reduceLastPlayerVoteCount(whichHalf, lastPlayerId);
  86. removeLastPlayerVote(whichHalf, lastPlayerId, myUserId);
  87. }
  88. mVoteBestPlayerReference.child(whichHalf).child(player_id).addListenerForSingleValueEvent(new ValueEventListener() {
  89. @Override
  90. public void onDataChange(DataSnapshot dataSnapshot) {
  91. HashMap<String, Boolean> votes = null;
  92. if (dataSnapshot != null && dataSnapshot.exists()) {
  93. votes = (HashMap<String, Boolean>) dataSnapshot.getValue();
  94. if (votes == null) {
  95. votes = new HashMap<>();
  96. }
  97. } else {
  98. votes = new HashMap<>();
  99. }
  100. votes.put(myUserId, true);
  101. mVoteBestPlayerReference.child(whichHalf).child(player_id).setValue(votes);
  102. updateUserVoteForPlayerMap(whichHalf, myUserId, player_id);
  103. updatePlayerVoteCount(whichHalf, player_id, player_name);
  104. if (mVoteBestPlayerListener != null) {
  105. mVoteBestPlayerListener.onVotePosted(player_id);
  106. }
  107. }
  108.  
  109. @Override
  110. public void onCancelled(DatabaseError databaseError) {
  111.  
  112. }
  113. });
  114. }
  115.  
  116. private void updateUserVoteForPlayerMap(String whichHalf, String myUserId, String playerId) {
  117. mVoteBestPlayerUserMapReference.child(whichHalf).addListenerForSingleValueEvent(new ValueEventListener() {
  118. @Override
  119. public void onDataChange(DataSnapshot dataSnapshot) {
  120. HashMap<String, String> voteMap = null;
  121. if (dataSnapshot != null && dataSnapshot.exists()) {
  122. voteMap = (HashMap<String, String>) dataSnapshot.getValue();
  123. if (voteMap == null) {
  124. voteMap = new HashMap<>();
  125. }
  126. } else {
  127. voteMap = new HashMap<>();
  128. }
  129. voteMap.put(myUserId, playerId);
  130. mVoteBestPlayerUserMapReference.child(whichHalf).setValue(voteMap);
  131. }
  132.  
  133. @Override
  134. public void onCancelled(DatabaseError databaseError) {
  135.  
  136. }
  137. });
  138. }
  139.  
  140. private void updatePlayerVoteCount(String whichHalf, String player_id, String player_name) {
  141. mVoteCountReference.child(whichHalf).child(NODE_PLAYER).child(player_id).addListenerForSingleValueEvent(new ValueEventListener() {
  142. @Override
  143. public void onDataChange(DataSnapshot dataSnapshot) {
  144. Vote playersVotes = null;
  145. if (dataSnapshot != null && dataSnapshot.exists()) {
  146. playersVotes = dataSnapshot.getValue(Vote.class);
  147. if (playersVotes != null && playersVotes.getVotes() > 0) {
  148. long votes = playersVotes.getVotes();
  149. votes++;
  150. playersVotes.setVotes(votes);
  151. } else {
  152. playersVotes = new Vote(1, player_id, player_name);
  153. }
  154. } else {
  155. playersVotes = new Vote(1, player_id, player_name);
  156. }
  157. mVoteCountReference.child(whichHalf).child(NODE_PLAYER).child(player_id).setValue(playersVotes);
  158. }
  159.  
  160. @Override
  161. public void onCancelled(DatabaseError databaseError) {
  162. Log.e(TAG, "updatePlayerVoteCount() - error - " + databaseError.getMessage());
  163. }
  164. });
  165. }
  166.  
  167. private void removeLastPlayerVote(String whichHalf, String lastPlayerId, String userId){
  168. mVoteBestPlayerReference.child(whichHalf).child(lastPlayerId).child(userId).removeValue();
  169. }
  170.  
  171. private void reduceLastPlayerVoteCount(String whichHalf, String lastPlayerId) {
  172. mVoteCountReference.child(whichHalf).child(NODE_PLAYER).child(lastPlayerId).addListenerForSingleValueEvent(new ValueEventListener() {
  173. @Override
  174. public void onDataChange(DataSnapshot dataSnapshot) {
  175. Vote playersVotes = null;
  176. if (dataSnapshot != null && dataSnapshot.exists()) {
  177. playersVotes = dataSnapshot.getValue(Vote.class);
  178. if (playersVotes != null && playersVotes.getVotes() > 0) {
  179. long votes = playersVotes.getVotes();
  180. votes--;
  181. playersVotes.setVotes(votes);
  182. }
  183. }
  184. mVoteCountReference.child(whichHalf).child(NODE_PLAYER).child(lastPlayerId).setValue(playersVotes);
  185. }
  186.  
  187. @Override
  188. public void onCancelled(DatabaseError databaseError) {
  189. Log.e(TAG, "reduceLastPlayerVoteCount() - error - " + databaseError.getMessage());
  190. }
  191. });
  192. }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement