use of com.google.firebase.database.ValueEventListener in project BORED by invent2017.
the class ShowStory method upVote.
public void upVote() {
mDataRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.child("stories").child(STORY_KEY).getValue() != null) {
int votes = dataSnapshot.child("stories").child(STORY_KEY).child("Votes").getValue(Integer.class);
if (dataSnapshot.child("stories").child(STORY_KEY).child("Upvoters").hasChild(username)) {
votes--;
dataSnapshot.child("stories").child(STORY_KEY).child("Upvoters").child(username).getRef().setValue(null);
dataSnapshot.child("users").child(username).child("UpvotedStories").child(STORY_KEY).getRef().setValue(null);
} else if (dataSnapshot.child("stories").child(STORY_KEY).child("Downvoters").hasChild(username)) {
votes = votes + 2;
dataSnapshot.child("stories").child(STORY_KEY).child("Downvoters").child(username).getRef().setValue(null);
dataSnapshot.child("stories").child(STORY_KEY).child("Upvoters").child(username).getRef().setValue(username);
dataSnapshot.child("users").child(username).child("DownvotedStories").child(STORY_KEY).getRef().setValue(null);
dataSnapshot.child("users").child(username).child("UpvotedStories").child(STORY_KEY).getRef().setValue(STORY_KEY);
} else {
votes++;
dataSnapshot.child("stories").child(STORY_KEY).child("Upvoters").child(username).getRef().setValue(username);
dataSnapshot.child("users").child(username).child("UpvotedStories").child(STORY_KEY).getRef().setValue(STORY_KEY);
}
dataSnapshot.child("stories").child(STORY_KEY).child("Votes").getRef().setValue(votes);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
use of com.google.firebase.database.ValueEventListener in project BORED by invent2017.
the class StoryFragment method loadStoryDetails.
private void loadStoryDetails(Bundle storyDetails) {
final String storyKey = storyDetails.getString("key");
if (storyKey != null) {
mViewsRef = FirebaseDatabase.getInstance().getReference().child("stories").child(storyKey).child("Views");
mVotesRef = FirebaseDatabase.getInstance().getReference().child("stories").child(storyKey).child("Votes");
mDataRef.child("stories").child(storyKey).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String uri = dataSnapshot.child("URI").getValue(String.class);
String caption = dataSnapshot.child("Caption").getValue(String.class);
boolean isFeatured = dataSnapshot.child("Featured").getValue(boolean.class);
if (isFeatured) {
featuredText.setText(R.string.featured_story);
}
if (uri != null && caption != null) {
StorageReference mStorageRef = FirebaseStorage.getInstance().getReferenceFromUrl(uri);
// Load story image into image view.
Glide.with(getActivity()).using(new FirebaseImageLoader()).load(mStorageRef).into(imageView);
storyCaption.setText(caption);
}
int storyDay = dataSnapshot.child("DateTime").child("date").getValue(Integer.class);
int storyMonth = 1 + dataSnapshot.child("DateTime").child("month").getValue(Integer.class);
int storyYear = 1900 + dataSnapshot.child("DateTime").child("year").getValue(Integer.class);
DateCreator storyDateCreator = new DateCreator(storyDay, storyMonth, storyYear);
dateText.setText(storyDateCreator.getDateString());
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getContext(), "Failed to load story data.", Toast.LENGTH_SHORT).show();
}
});
mDataRef.child("stories").child(storyKey).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot != null) {
int votes = 0;
if (dataSnapshot.child("Votes").getValue() != null) {
votes = dataSnapshot.child("Votes").getValue(Integer.class);
}
storyVotes = votes;
voteNumber.setText(String.format(new Locale("en", "US"), "%d", votes));
// added this for views lel
int views = 0;
if (dataSnapshot.child("Views").getValue() != null) {
views = dataSnapshot.child("Views").getValue(Integer.class);
}
storyViews = views;
viewNumber.setText(String.format(new Locale("en", "US"), "%d", views));
} else {
voteNumber.setText(String.format(new Locale("en", "US"), "%d", 0));
viewNumber.setText(String.format(new Locale("en", "US"), "%d", 0));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Story does not exist.").setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(getActivity(), MapsActivityCurrentPlace.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
});
builder.create();
}
}
use of com.google.firebase.database.ValueEventListener in project BORED by invent2017.
the class StoryFragment method downVote.
public void downVote() {
mDataRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.child("stories").child(storyKey).getValue() != null) {
int votes = dataSnapshot.child("stories").child(storyKey).child("Votes").getValue(Integer.class);
if (dataSnapshot.child("stories").child(storyKey).child("Downvoters").hasChild(getUsername())) {
votes++;
dataSnapshot.child("stories").child(storyKey).child("Downvoters").child(getUsername()).getRef().setValue(null);
dataSnapshot.child("users").child(getUsername()).child("DownvotedStories").child(storyKey).getRef().setValue(null);
} else if (dataSnapshot.child("stories").child(storyKey).child("Upvoters").hasChild(getUsername())) {
votes = votes - 2;
dataSnapshot.child("stories").child(storyKey).child("Upvoters").child(getUsername()).getRef().setValue(null);
dataSnapshot.child("stories").child(storyKey).child("Downvoters").child(getUsername()).getRef().setValue(getUsername());
dataSnapshot.child("users").child(getUsername()).child("UpvotedStories").child(storyKey).getRef().setValue(null);
dataSnapshot.child("users").child(getUsername()).child("DownvotedStories").child(storyKey).getRef().setValue(storyKey);
} else {
votes--;
dataSnapshot.child("stories").child(storyKey).child("Downvoters").child(getUsername()).getRef().setValue(getUsername());
dataSnapshot.child("users").child(getUsername()).child("DownvotedStories").child(storyKey).getRef().setValue(storyKey);
}
dataSnapshot.child("stories").child(storyKey).child("Votes").getRef().setValue(votes);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
use of com.google.firebase.database.ValueEventListener in project BORED by invent2017.
the class StoryFragment method addView.
public void addView() {
DatabaseReference mStoryRef = FirebaseDatabase.getInstance().getReference().child("stories").child(storyKey).child("Views");
mStoryRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
if (mutableData.getValue() != null) {
int storyViews = mutableData.getValue(Integer.class);
++storyViews;
mutableData.setValue(storyViews);
}
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
}
});
mDataRef.child("users").child(username).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (!(dataSnapshot.child("ReadStories").hasChild(storyKey))) {
dataSnapshot.child("ReadStories").child(storyKey).getRef().setValue(storyKey);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
use of com.google.firebase.database.ValueEventListener in project BORED by invent2017.
the class StoryUpload method uploadStoryData.
private void uploadStoryData() {
final Location storyLocation = new Location(LocationManager.GPS_PROVIDER);
storyLocation.setLatitude(storySettings.getDouble("Latitude"));
storyLocation.setLongitude(storySettings.getDouble("Longitude"));
if (storyLocation != null) {
final String locationString = Double.toString(storyLocation.getLatitude()) + "," + Double.toString(storyLocation.getLongitude());
mDataRef.child("uploads").child(storyKey).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
String storyURI = dataSnapshot.getValue(String.class);
String storyCaption = caption.getText().toString();
String hashtagBoxText = hashtagBox.getText().toString();
if (!hashtagBoxText.equals("")) {
hashtagBoxText = hashtagBoxText.substring(1);
String[] hashtags = hashtagBoxText.split("#");
StringBuilder captionBuilder = new StringBuilder().append(storyCaption).append(" ");
for (int i = 0; i < hashtags.length; i++) {
hashtags[i] = hashtags[i].trim();
captionBuilder.append("#").append(hashtags[i]);
}
storyCaption = captionBuilder.toString();
}
Map<String, Object> childUpdates = new HashMap<>();
Story story = new Story(storyURI, storyLocation, storyCaption, new Date());
Map<String, Object> storyDetails = story.toMap();
childUpdates.put("/stories/" + storyKey, storyDetails);
String locationKey = (Double.toString(storyLocation.getLatitude()) + "," + Double.toString(storyLocation.getLongitude())).replace('.', 'd');
childUpdates.put("/locations/" + locationKey + "/" + storyKey, 0);
if (storySettings.getBoolean("Logged in")) {
String username = getSharedPreferences(PREFS_NAME, 0).getString("Username", "");
childUpdates.put("/users/" + username + "/stories/" + storyKey, locationString);
}
if (storyCaption.contains("#")) {
String hashTagPattern = ("#(\\w+)");
Pattern p = Pattern.compile(hashTagPattern);
Matcher m = p.matcher(storyCaption);
while (m.find()) {
String hashtag = m.group(1);
childUpdates.put("/hashtags/" + hashtag + "/" + storyKey, locationString);
}
}
mDataRef.updateChildren(childUpdates);
mDataRef.child("uploads").child(storyKey).removeValue();
Toast.makeText(StoryUpload.this, "Story added!", Toast.LENGTH_SHORT).show();
finish();
} else {
Toast.makeText(StoryUpload.this, "An error occurred. Please try again.", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
builder.setMessage("Upload failed. Please try again later.").setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(StoryUpload.this, MapsActivityCurrentPlace.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
});
builder.create().show();
}
});
} else {
Toast.makeText(this, "An error occurred.", Toast.LENGTH_SHORT).show();
}
}
Aggregations