Search in sources :

Example 1 with Post

use of com.example.management.models.Post in project priend by TakoJ.

the class NewPostActivity method writeNewPost.

// [START write_fan_out]
private void writeNewPost(String userId, String username, String title, String body) {
    // Create new post at /user-posts/$userid/$postid and at
    // /posts/$postid simultaneously
    String key = mDatabase.child("posts").push().getKey();
    Post post = new Post(userId, username, title, body);
    Map<String, Object> postValues = post.toMap();
    Map<String, Object> childUpdates = new HashMap<>();
    childUpdates.put("/posts/" + key, postValues);
    childUpdates.put("/user-posts/" + userId + "/" + key, postValues);
    mDatabase.updateChildren(childUpdates);
}
Also used : HashMap(java.util.HashMap) Post(com.example.management.models.Post)

Example 2 with Post

use of com.example.management.models.Post in project priend by TakoJ.

the class PostDetailActivity method onStart.

@Override
public void onStart() {
    super.onStart();
    // Add value event listener to the post
    // [START post_value_event_listener]
    ValueEventListener postListener = new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // Get Post object and use the values to update the UI
            Post post = dataSnapshot.getValue(Post.class);
            // [START_EXCLUDE]
            mAuthorView.setText(post.author);
            mTitleView.setText(post.title);
            mBodyView.setText(post.body);
        // [END_EXCLUDE]
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            // Getting Post failed, log a message
            Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
            // [START_EXCLUDE]
            Toast.makeText(PostDetailActivity.this, "Failed to load post.", Toast.LENGTH_SHORT).show();
        // [END_EXCLUDE]
        }
    };
    mPostReference.addValueEventListener(postListener);
    // [END post_value_event_listener]
    // Keep copy of post listener so we can remove it when app stops
    mPostListener = postListener;
    // Listen for comments
    mAdapter = new CommentAdapter(this, mCommentsReference);
    mCommentsRecycler.setAdapter(mAdapter);
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) Post(com.example.management.models.Post) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 3 with Post

use of com.example.management.models.Post in project priend by TakoJ.

the class PostListFragment method onActivityCreated.

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    // Set up Layout Manager, reverse layout
    mManager = new LinearLayoutManager(getActivity());
    mManager.setReverseLayout(true);
    mManager.setStackFromEnd(true);
    mRecycler.setLayoutManager(mManager);
    // Set up FirebaseRecyclerAdapter with the Query
    Query postsQuery = getQuery(mDatabase);
    mAdapter = new FirebaseRecyclerAdapter<Post, PostViewHolder>(Post.class, R.layout.item_post, PostViewHolder.class, postsQuery) {

        @Override
        protected void populateViewHolder(final PostViewHolder viewHolder, final Post model, final int position) {
            final DatabaseReference postRef = getRef(position);
            // Set click listener for the whole post view
            final String postKey = postRef.getKey();
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // Launch PostDetailActivity
                    Intent intent = new Intent(getActivity(), PostDetailActivity.class);
                    intent.putExtra(PostDetailActivity.EXTRA_POST_KEY, postKey);
                    startActivity(intent);
                }
            });
            // Determine if the current user has liked this post and set UI accordingly
            if (model.stars.containsKey(getUid())) {
                viewHolder.starView.setImageResource(R.mipmap.ic_toggle_star_24);
            } else {
                viewHolder.starView.setImageResource(R.mipmap.ic_toggle_star_outline_24);
            }
            // Bind Post to ViewHolder, setting OnClickListener for the star button
            viewHolder.bindToPost(model, new View.OnClickListener() {

                @Override
                public void onClick(View starView) {
                    // Need to write to both places the post is stored
                    DatabaseReference globalPostRef = mDatabase.child("posts").child(postRef.getKey());
                    DatabaseReference userPostRef = mDatabase.child("user-posts").child(model.uid).child(postRef.getKey());
                    // Run two transactions
                    onStarClicked(globalPostRef);
                    onStarClicked(userPostRef);
                }
            });
        }
    };
    mRecycler.setAdapter(mAdapter);
}
Also used : PostViewHolder(com.example.management.viewholder.PostViewHolder) Query(com.google.firebase.database.Query) DatabaseReference(com.google.firebase.database.DatabaseReference) Post(com.example.management.models.Post) Intent(android.content.Intent) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View)

Aggregations

Post (com.example.management.models.Post)3 Intent (android.content.Intent)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 View (android.view.View)1 PostViewHolder (com.example.management.viewholder.PostViewHolder)1 DataSnapshot (com.google.firebase.database.DataSnapshot)1 DatabaseError (com.google.firebase.database.DatabaseError)1 DatabaseReference (com.google.firebase.database.DatabaseReference)1 Query (com.google.firebase.database.Query)1 ValueEventListener (com.google.firebase.database.ValueEventListener)1 HashMap (java.util.HashMap)1