Search in sources :

Example 1 with MainActivity

use of com.polito.mad17.madmax.activities.MainActivity in project MadMax by deviz92.

the class FriendsFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final String activityName = getActivity().getClass().getSimpleName();
    Log.d(TAG, "Sono nella activity: " + activityName);
    final View view = inflater.inflate(R.layout.skeleton_list, container, false);
    databaseReference = FirebaseDatabase.getInstance().getReference();
    friends.clear();
    setInterface((OnItemClickInterface) getActivity(), (OnItemLongClickInterface) getActivity());
    RecyclerView.ItemDecoration divider = new InsetDivider.Builder(getContext()).orientation(InsetDivider.VERTICAL_LIST).dividerHeight(getResources().getDimensionPixelSize(R.dimen.divider_height)).color(ContextCompat.getColor(getContext(), R.color.colorDivider)).insets(getResources().getDimensionPixelSize(R.dimen.divider_inset), 0).overlay(true).build();
    recyclerView = (RecyclerView) view.findViewById(R.id.rv_skeleton);
    layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.addItemDecoration(divider);
    friendsViewAdapter = new FriendsViewAdapter(this.getContext(), this, this, friends, groupDetail);
    recyclerView.setAdapter(friendsViewAdapter);
    // Se sono in MainActivity visualizzo lista degli amici
    if (activityName.equals("MainActivity")) {
        query = databaseReference.child("users").child(MainActivity.getCurrentUID()).child("friends");
    } else // Se sono dentro un gruppo, visualizzo lista membri del gruppo
    if (activityName.equals("GroupDetailActivity")) {
        Bundle b = this.getArguments();
        if (b != null) {
            groupID = b.getString("groupID");
            query = databaseReference.child("groups").child(groupID).child("members");
        }
    }
    Log.d(TAG, "query: " + query);
    groupMembersListener = query.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(final DataSnapshot externalDataSnapshot) {
            // svuoto la map, così se viene eliminato uno user dal db, non viene tenuto nella map che si ricarica da zero
            friends.clear();
            for (final DataSnapshot friendSnapshot : externalDataSnapshot.getChildren()) {
                // getFriend(friendSnapshot.getKey());
                if (activityName.equals("MainActivity")) {
                    Log.d(TAG, "key: " + friendSnapshot.getKey());
                    Log.d(TAG, "value: " + friendSnapshot.getValue());
                    if (!listenedFriends)
                        listenedFriends = true;
                    // Log.d(TAG,  friendSnapshot.child("deleted").getValue() + " ");
                    deleted = friendSnapshot.child("deleted").getValue(Boolean.class);
                    if (deleted == null) {
                        deleted = true;
                    }
                    if (deleted)
                        Log.d(TAG, friendSnapshot.getKey() + " is cancelled");
                } else if (activityName.equals("GroupDetailActivity")) {
                    deleted = friendSnapshot.child("deleted").getValue(Boolean.class);
                    if (deleted == null) {
                        deleted = true;
                    }
                    // Se sono negli amici "generali" e non nei membri di un gruppo, non c'è il campo deleted, quindi sarà null
                    if (!listenedGroups.contains(groupID))
                        listenedGroups.add(groupID);
                }
                if (!deleted) {
                    final String id = friendSnapshot.getKey();
                    databaseReference.child("users").child(id).addValueEventListener(new ValueEventListener() {

                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            String name = dataSnapshot.child("name").getValue(String.class);
                            String surname = dataSnapshot.child("surname").getValue(String.class);
                            String profileImage = dataSnapshot.child("image").getValue(String.class);
                            if (activityName.equals("MainActivity")) {
                                User u = new User();
                                u.setID(friendSnapshot.getKey());
                                u.setName(name);
                                u.setSurname(surname);
                                u.setProfileImage(profileImage);
                                if (!deleted)
                                    friends.put(id, u);
                                else
                                    friends.remove(id);
                                friendsViewAdapter.update(friends);
                                friendsViewAdapter.notifyDataSetChanged();
                            } else if (activityName.equals("GroupDetailActivity")) {
                                getUserAndGroupBalance(id, name, surname, profileImage, groupID);
                            }
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {
                        }
                    });
                }
            }
            friendsViewAdapter.update(friends);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.w(TAG, databaseError.getMessage());
        }
    });
    Log.d(TAG, "dopo setAdapter");
    return view;
}
Also used : User(com.polito.mad17.madmax.entities.User) Bundle(android.os.Bundle) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) DataSnapshot(com.google.firebase.database.DataSnapshot) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) DatabaseError(com.google.firebase.database.DatabaseError) RecyclerView(android.support.v7.widget.RecyclerView) ValueEventListener(com.google.firebase.database.ValueEventListener)

Example 2 with MainActivity

use of com.polito.mad17.madmax.activities.MainActivity in project MadMax by deviz92.

the class LoginSignUpActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // the first time user open the app the default preferences must be setted up
    PreferenceManager.setDefaultValues(this, R.layout.preferences, false);
    Log.i(TAG, "onCreate");
    FirebaseUtils.getInstance().setUp();
    // getting Intent from invitation
    Intent startingIntent = getIntent();
    // prepare intent for MainActivity
    goToMain = new Intent(getApplicationContext(), MainActivity.class);
    goToMain.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    String action = startingIntent.getAction();
    Log.d(TAG, "action " + action);
    // retrieving data from the intent inviterID & groupToBeAddedID as the group ID where to add the current user
    if (startingIntent.hasExtra("inviterID")) {
        // to be used to set the current user as friend of the inviter
        Log.d(TAG, "there is an invite");
        inviterID = startingIntent.getStringExtra("inviterID");
        startingIntent.removeExtra("inviterID");
        goToMain.putExtra("inviterID", inviterID);
    } else
        inviterID = null;
    if (startingIntent.hasExtra("groupToBeAddedID")) {
        groupToBeAddedID = startingIntent.getStringExtra("groupToBeAddedID");
        startingIntent.removeExtra("groupToBeAddedID");
        goToMain.putExtra("groupToBeAddedID", groupToBeAddedID);
    } else
        groupToBeAddedID = null;
    // insert tabs and current fragment in the main layout
    setContentView(R.layout.activity_log_in_signup);
    // for adding custom font to the title of the app
    TextView titleTextView = (TextView) findViewById(R.id.title);
    Typeface mycustomfont = Typeface.createFromAsset(getAssets(), "fonts/Lobster-Regular.ttf");
    titleTextView.setTypeface(mycustomfont);
    // for adding custom background image
    ImageView background = (ImageView) findViewById(R.id.background);
    Glide.with(this).load(R.drawable.background).centerCrop().diskCacheStrategy(DiskCacheStrategy.ALL).into(background);
    // findViewById(R.id.activity_log_in_signup_layout).setOnClickListener(this);
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setText(R.string.login));
    tabLayout.addTab(tabLayout.newTab().setText(R.string.signup));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    viewPager = (ViewPager) findViewById(R.id.main_view_pager);
    adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    viewPager.setCurrentItem(0);
    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            Log.d(TAG, String.valueOf(tab.getPosition()));
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
        }
    });
}
Also used : Typeface(android.graphics.Typeface) TabLayout(android.support.design.widget.TabLayout) Intent(android.content.Intent) TextView(android.widget.TextView) MainActivity(com.polito.mad17.madmax.activities.MainActivity) ImageView(android.widget.ImageView) FragmentPagerAdapter(android.support.v4.app.FragmentPagerAdapter)

Aggregations

Intent (android.content.Intent)1 Typeface (android.graphics.Typeface)1 Bundle (android.os.Bundle)1 TabLayout (android.support.design.widget.TabLayout)1 FragmentPagerAdapter (android.support.v4.app.FragmentPagerAdapter)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 View (android.view.View)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 DataSnapshot (com.google.firebase.database.DataSnapshot)1 DatabaseError (com.google.firebase.database.DatabaseError)1 ValueEventListener (com.google.firebase.database.ValueEventListener)1 MainActivity (com.polito.mad17.madmax.activities.MainActivity)1 User (com.polito.mad17.madmax.entities.User)1