Search in sources :

Example 46 with ValueEventListener

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) {
        }
    });
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 47 with ValueEventListener

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();
    }
}
Also used : Locale(java.util.Locale) AlertDialog(android.support.v7.app.AlertDialog) StorageReference(com.google.firebase.storage.StorageReference) DialogInterface(android.content.DialogInterface) Intent(android.content.Intent) DataSnapshot(com.google.firebase.database.DataSnapshot) FirebaseImageLoader(com.firebase.ui.storage.images.FirebaseImageLoader) DatabaseError(com.google.firebase.database.DatabaseError) ValueEventListener(com.google.firebase.database.ValueEventListener)

Example 48 with ValueEventListener

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) {
        }
    });
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 49 with ValueEventListener

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) {
        }
    });
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) Transaction(com.google.firebase.database.Transaction) DatabaseReference(com.google.firebase.database.DatabaseReference) ValueEventListener(com.google.firebase.database.ValueEventListener) MutableData(com.google.firebase.database.MutableData) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 50 with ValueEventListener

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();
    }
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) DialogInterface(android.content.DialogInterface) Intent(android.content.Intent) DataSnapshot(com.google.firebase.database.DataSnapshot) Date(java.util.Date) DatabaseError(com.google.firebase.database.DatabaseError) ValueEventListener(com.google.firebase.database.ValueEventListener) HashMap(java.util.HashMap) Map(java.util.Map) Location(android.location.Location)

Aggregations

DataSnapshot (com.google.firebase.database.DataSnapshot)211 ValueEventListener (com.google.firebase.database.ValueEventListener)211 DatabaseError (com.google.firebase.database.DatabaseError)210 DatabaseReference (com.google.firebase.database.DatabaseReference)62 View (android.view.View)47 Intent (android.content.Intent)43 TextView (android.widget.TextView)30 FirebaseDatabase (com.google.firebase.database.FirebaseDatabase)24 RecyclerView (android.support.v7.widget.RecyclerView)20 FirebaseUser (com.google.firebase.auth.FirebaseUser)20 HashMap (java.util.HashMap)20 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)19 Bundle (android.os.Bundle)16 ImageView (android.widget.ImageView)15 ArrayList (java.util.ArrayList)15 User (com.jexapps.bloodhub.m_Model.User)11 Map (java.util.Map)11 Date (java.util.Date)10 Query (com.google.firebase.database.Query)9 User (com.polito.mad17.madmax.entities.User)9