Search in sources :

Example 16 with DatabaseReference

use of com.google.firebase.database.DatabaseReference 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)

Example 17 with DatabaseReference

use of com.google.firebase.database.DatabaseReference in project Robot-Scouter by SUPERCILEX.

the class ScoutTemplateSheet method getTemplateKey.

private void getTemplateKey() {
    TeamHelper teamHelper = TeamHelper.parse(getArguments());
    mTemplateKey = teamHelper.getTeam().getTemplateKey();
    if (TextUtils.isEmpty(mTemplateKey)) {
        if (!Constants.sFirebaseScoutTemplates.isEmpty()) {
            mTemplateKey = Constants.sFirebaseScoutTemplates.get(0).getKey();
            teamHelper.updateTemplateKey(mTemplateKey);
            return;
        }
        DatabaseReference newTemplateRef = FIREBASE_SCOUT_TEMPLATES.push();
        mTemplateKey = newTemplateRef.getKey();
        FirebaseCopier.copyTo(Constants.sDefaultTemplate, newTemplateRef);
        teamHelper.updateTemplateKey(mTemplateKey);
        UserHelper.getScoutTemplateIndicesRef().child(mTemplateKey).setValue(true);
        for (int i = 0; i < Constants.sFirebaseTeams.size(); i++) {
            Team team = Constants.sFirebaseTeams.getObject(i);
            String templateKey = team.getTemplateKey();
            if (TextUtils.isEmpty(templateKey)) {
                team.getHelper().updateTemplateKey(mTemplateKey);
            }
        }
    }
}
Also used : DatabaseReference(com.google.firebase.database.DatabaseReference) TeamHelper(com.supercilex.robotscouter.data.util.TeamHelper) Team(com.supercilex.robotscouter.data.model.Team)

Example 18 with DatabaseReference

use of com.google.firebase.database.DatabaseReference in project NPSmiles by bmcglynn1.

the class NewsFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View viewer = inflater.inflate(R.layout.activity_news_feed, container, false);
    View popupLayout = inflater.inflate(R.layout.popup_layout_logged_in, null);
    final ListView list = (ListView) viewer.findViewById(R.id.list);
    final ArrayList<NewsMessage> newsList = new ArrayList<>();
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("Reference");
    mAuth = FirebaseAuth.getInstance();
    if (mAuth.getCurrentUser() != null) {
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                PopupMenu popup = new PopupMenu(parent.getContext(), view);
                popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
                final TextView title = (TextView) view.findViewById(R.id.title);
                final TextView message = (TextView) view.findViewById(R.id.information);
                if (mAuth.getCurrentUser() != null) {
                    popup.show();
                    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

                        @Override
                        public boolean onMenuItemClick(MenuItem item) {
                            switch(item.getItemId()) {
                                case R.id.EditMenu:
                                    // refresh view if needed
                                    break;
                                case R.id.DeleteMenu:
                                    // delete from firebase
                                    // refresh view
                                    FirebaseDatabase database = FirebaseDatabase.getInstance();
                                    DatabaseReference myRef = database.getReference("Reference");
                                    Query titleQuery = myRef.child(title.getText().toString());
                                    titleQuery.addListenerForSingleValueEvent(new ValueEventListener() {

                                        @Override
                                        public void onDataChange(DataSnapshot dataSnapshot) {
                                            for (DataSnapshot titleSnapshot : dataSnapshot.getChildren()) {
                                                dataSnapshot.getRef().removeValue();
                                            }
                                        }

                                        @Override
                                        public void onCancelled(DatabaseError databaseError) {
                                        }
                                    });
                                    break;
                            }
                            return true;
                        }
                    });
                }
            }
        });
    }
    myRef.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            newsList.clear();
            for (DataSnapshot newsItem : dataSnapshot.getChildren()) {
                NewsMessage newsMessage = new NewsMessage();
                for (DataSnapshot item : newsItem.getChildren()) {
                    if (item.getKey().equals("title")) {
                        newsMessage.setTitle((String) item.getValue());
                    } else if (item.getKey().equals("message")) {
                        newsMessage.setMessage((String) item.getValue());
                    } else {
                        newsMessage.setDate((String) item.getValue());
                    }
                }
                newsList.add(newsMessage);
            }
            Collections.sort(newsList);
            CustomAdapter adapter = new CustomAdapter(context, newsList);
            list.setAdapter(adapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
    return viewer;
}
Also used : FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) Query(com.google.firebase.database.Query) DatabaseReference(com.google.firebase.database.DatabaseReference) ArrayList(java.util.ArrayList) MenuItem(android.view.MenuItem) DataSnapshot(com.google.firebase.database.DataSnapshot) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) ListView(android.widget.ListView) DatabaseError(com.google.firebase.database.DatabaseError) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ValueEventListener(com.google.firebase.database.ValueEventListener) PopupMenu(android.widget.PopupMenu)

Example 19 with DatabaseReference

use of com.google.firebase.database.DatabaseReference in project NPSmiles by bmcglynn1.

the class CreatePost method addPost.

public void addPost() {
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("Reference");
    DatabaseReference childRef = myRef.child(newsMessage.getTitle());
    childRef.setValue(newsMessage);
}
Also used : FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) DatabaseReference(com.google.firebase.database.DatabaseReference)

Example 20 with DatabaseReference

use of com.google.firebase.database.DatabaseReference in project iNGAGE by davis123123.

the class ChatActivity method onUpvoteClick.

//modify unblock functions here
@Override
public void onUpvoteClick(int p) {
    //Log.d("vote" , "up : ");
    //get correct chat msg with ith key from chatmessage helper
    ChatMessageHelper chatMessageHelper = (ChatMessageHelper) chatAdapter.getItem(p);
    String chat_key = chatMessageHelper.getMessageID();
    DatabaseReference message_root = root.child(chat_key);
    //get upvote data
    DatabaseReference upvote_count = message_root.child("upvotes");
    upvote_count.runTransaction(new Transaction.Handler() {

        @Override
        public Transaction.Result doTransaction(MutableData currentData) {
            Log.d("Data", String.valueOf(currentData));
            if (currentData.getValue() == null) {
                currentData.setValue(1);
            } else {
                currentData.setValue((Long) currentData.getValue() + 1);
            }
            //we can also abort by calling Transaction.abort()
            return Transaction.success(currentData);
        }

        //TODO:Error handle here
        @Override
        public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
        }
    });
}
Also used : ChatMessageHelper(ingage.ingage20.helpers.ChatMessageHelper) DatabaseError(com.google.firebase.database.DatabaseError) Transaction(com.google.firebase.database.Transaction) DatabaseReference(com.google.firebase.database.DatabaseReference) MutableData(com.google.firebase.database.MutableData) DataSnapshot(com.google.firebase.database.DataSnapshot)

Aggregations

DatabaseReference (com.google.firebase.database.DatabaseReference)25 DataSnapshot (com.google.firebase.database.DataSnapshot)13 DatabaseError (com.google.firebase.database.DatabaseError)13 RecyclerView (android.support.v7.widget.RecyclerView)6 View (android.view.View)6 MutableData (com.google.firebase.database.MutableData)6 Transaction (com.google.firebase.database.Transaction)6 ValueEventListener (com.google.firebase.database.ValueEventListener)6 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)5 Intent (android.content.Intent)4 FirebaseDatabase (com.google.firebase.database.FirebaseDatabase)4 Query (com.google.firebase.database.Query)4 ChatMessageHelper (ingage.ingage20.helpers.ChatMessageHelper)4 Bundle (android.os.Bundle)2 TextView (android.widget.TextView)2 Event (com.polito.mad17.madmax.entities.Event)2 User (com.polito.mad17.madmax.entities.User)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2