use of com.polito.mad17.madmax.entities.Group in project MadMax by deviz92.
the class GroupsViewAdapter method onBindViewHolder.
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
final ItemGroupViewHolder groupViewHolder = (ItemGroupViewHolder) holder;
Map.Entry<String, Group> item = getItem(position);
if (position == (mData.size() - 1)) {
Log.d(TAG, "item.getKey().equals(\"nullGroup\")");
groupViewHolder.imageView.setVisibility(View.GONE);
groupViewHolder.nameTextView.setText("");
groupViewHolder.balanceTextTextView.setText("");
groupViewHolder.balanceTextView.setText("");
} else {
Log.d(TAG, "item ID " + item.getKey() + " item name " + item.getValue().getName());
// String p = groups.get(String.valueOf(position)).getImage();
groupViewHolder.imageView.setVisibility(View.VISIBLE);
String p = item.getValue().getImage();
if (p != null && !p.equals("noImage")) {
Log.d(TAG, "Image not null " + p);
Glide.with(layoutInflater.getContext()).load(p).centerCrop().bitmapTransform(new CropCircleTransformation(layoutInflater.getContext())).diskCacheStrategy(DiskCacheStrategy.ALL).into(groupViewHolder.imageView);
} else {
Glide.with(layoutInflater.getContext()).load(R.drawable.group_default).centerCrop().bitmapTransform(new CropCircleTransformation(layoutInflater.getContext())).diskCacheStrategy(DiskCacheStrategy.ALL).into(groupViewHolder.imageView);
}
groupViewHolder.nameTextView.setText(item.getValue().getName());
// mydebt = mio debito con il gruppo
String groupname = item.getValue().getName();
totBalances = item.getValue().getCurrencyBalances();
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
String defaultCurrency = sharedPref.getString(SettingsFragment.DEFAULT_CURRENCY, "");
Boolean multipleCurrencies = false;
Double shownBal;
String shownCurr;
if (groupname.equals("fff") || groupname.equals("Regalo")) {
Log.d(TAG, "sii");
}
Log.d(TAG, "callingActivity " + callingActivity);
if (!callingActivity.equals("FriendDetailActivity") && !callingActivity.equals("ChooseGroupActivity")) {
if (totBalances != null) {
groupViewHolder.balanceTextTextView.setVisibility(View.VISIBLE);
groupViewHolder.balanceTextView.setVisibility(View.VISIBLE);
for (Map.Entry<String, Double> entry : totBalances.entrySet()) {
Log.d(TAG, "Bilancio in " + groupname + " : " + entry.getValue() + " " + entry.getKey());
}
if (!totBalances.isEmpty()) {
// If there is more than one currency
if (totBalances.size() > 1) {
multipleCurrencies = true;
} else // If there is just one currency
{
multipleCurrencies = false;
}
if (totBalances.containsKey(defaultCurrency)) {
shownBal = totBalances.get(defaultCurrency);
shownCurr = defaultCurrency;
} else {
shownCurr = (String) totBalances.keySet().toArray()[0];
shownBal = totBalances.get(shownCurr);
}
// Print balance
if (shownBal > 0) {
groupViewHolder.balanceTextTextView.setText(R.string.you_should_receive);
groupViewHolder.balanceTextTextView.setTextColor(ContextCompat.getColor(context, R.color.colorAccent));
if (multipleCurrencies)
groupViewHolder.balanceTextView.setText(df.format(shownBal) + " " + shownCurr + "*");
else
groupViewHolder.balanceTextView.setText(df.format(shownBal) + " " + shownCurr);
groupViewHolder.balanceTextView.setTextColor(ContextCompat.getColor(context, R.color.colorAccent));
} else if (shownBal < 0) {
groupViewHolder.balanceTextTextView.setText(R.string.you_owe);
groupViewHolder.balanceTextTextView.setTextColor(ContextCompat.getColor(context, R.color.colorPrimaryDark));
if (multipleCurrencies)
groupViewHolder.balanceTextView.setText(df.format(Math.abs(shownBal)) + " " + shownCurr + "*");
else
groupViewHolder.balanceTextView.setText(df.format(Math.abs(shownBal)) + " " + shownCurr);
groupViewHolder.balanceTextView.setTextColor(ContextCompat.getColor(context, R.color.colorPrimaryDark));
} else if (shownBal == 0) {
groupViewHolder.balanceTextTextView.setText(R.string.no_debts);
groupViewHolder.balanceTextTextView.setTextColor(ContextCompat.getColor(context, R.color.colorSecondaryText));
groupViewHolder.balanceTextView.setText("0 " + defaultCurrency);
groupViewHolder.balanceTextView.setTextColor(ContextCompat.getColor(context, R.color.colorSecondaryText));
}
}
groupViewHolder.balanceTextTextView.setVisibility(View.VISIBLE);
groupViewHolder.balanceTextView.setVisibility(View.VISIBLE);
} else // If there are no balances in the map
{
groupViewHolder.balanceTextTextView.setText(R.string.no_debts);
groupViewHolder.balanceTextTextView.setTextColor(ContextCompat.getColor(context, R.color.colorSecondaryText));
groupViewHolder.balanceTextView.setText("0 " + defaultCurrency);
groupViewHolder.balanceTextView.setTextColor(ContextCompat.getColor(context, R.color.colorSecondaryText));
groupViewHolder.balanceTextTextView.setVisibility(View.VISIBLE);
groupViewHolder.balanceTextView.setVisibility(View.VISIBLE);
}
}
if (callingActivity.equals(DetailFragment.class.getSimpleName())) {
groupViewHolder.balanceTextTextView.setVisibility(View.GONE);
groupViewHolder.balanceTextView.setVisibility(View.GONE);
}
/*
else
{
groupViewHolder.balanceTextTextView.setVisibility(View.GONE);
groupViewHolder.balanceTextView.setVisibility(View.GONE);
}
*/
// check if there are expenses with other currencies than the default
/*Boolean defaultCurrencyExpense = false;
Boolean otherCurrenciesPresent = false;
Double groupBalanceDefaultCurrency = 0.0d;
HashMap<String, Double> otherCurrenciesBalance = new HashMap<>();
HashMap<String, Expense> groupExpenses = item.getValue().getExpenses();
if(groupExpenses != null) {
for (Map.Entry<String, Expense> expenseEntry : groupExpenses.entrySet()) {
String currentCurrency = expenseEntry.getValue().getCurrency();
if (currentCurrency.equals(defaultCurrency)) {
groupBalanceDefaultCurrency += expenseEntry.getValue().getAmount();
defaultCurrencyExpense = true;
} else {
otherCurrenciesPresent = true;
if (otherCurrenciesBalance.containsKey(currentCurrency)) {
Double currentBalance = otherCurrenciesBalance.get(currentCurrency);
currentBalance += otherCurrenciesBalance.get(currentCurrency);
otherCurrenciesBalance.put(currentCurrency, currentBalance);
} else {
otherCurrenciesBalance.put(currentCurrency, expenseEntry.getValue().getAmount());
}
}
}
if (defaultCurrencyExpense) {
if (groupBalanceDefaultCurrency == null) {
groupViewHolder.balanceTextTextView.setText("");
groupViewHolder.balanceTextView.setText("");
return;
}
if (groupBalanceDefaultCurrency > 0) {
groupViewHolder.balanceTextTextView.setText(R.string.credit_of);
groupViewHolder.balanceTextTextView.setTextColor(ContextCompat.getColor(context, R.color.colorPrimaryDark));
String balance = df.format(Math.abs(groupBalanceDefaultCurrency)) + " " + defaultCurrency;
if (otherCurrenciesPresent) {
balance += " *";
}
groupViewHolder.balanceTextView.setText(balance);
groupViewHolder.balanceTextView.setTextColor(ContextCompat.getColor(context, R.color.colorPrimaryDark));
} else {
if (groupBalanceDefaultCurrency < 0) {
groupViewHolder.balanceTextTextView.setText(R.string.debt_of);
groupViewHolder.balanceTextTextView.setTextColor(ContextCompat.getColor(context, R.color.colorAccent));
String balance = df.format(Math.abs(groupBalanceDefaultCurrency)) + " " + defaultCurrency;
if (otherCurrenciesPresent) {
balance += " *";
}
groupViewHolder.balanceTextView.setText(balance);
groupViewHolder.balanceTextView.setTextColor(ContextCompat.getColor(context, R.color.colorAccent));
} else if (!otherCurrenciesPresent) {
groupViewHolder.balanceTextTextView.setText(R.string.no_debts);
groupViewHolder.balanceTextTextView.setTextColor(ContextCompat.getColor(context, R.color.colorSecondaryText));
groupViewHolder.balanceTextView.setText("0.0 " + defaultCurrency);
groupViewHolder.balanceTextView.setTextColor(ContextCompat.getColor(context, R.color.colorAccent));
} else {
Map.Entry<String, Double> entry = otherCurrenciesBalance.entrySet().iterator().next();
if (entry.getValue() > 0) {
groupViewHolder.balanceTextTextView.setText(R.string.credit_of);
groupViewHolder.balanceTextTextView.setTextColor(ContextCompat.getColor(context, R.color.colorPrimaryDark));
String balance = df.format(Math.abs(entry.getValue())) + " " + entry.getKey();
groupViewHolder.balanceTextView.setText(balance);
groupViewHolder.balanceTextView.setTextColor(ContextCompat.getColor(context, R.color.colorPrimaryDark));
} else {
groupViewHolder.balanceTextTextView.setText(R.string.debt_of);
groupViewHolder.balanceTextTextView.setTextColor(ContextCompat.getColor(context, R.color.colorAccent));
String balance = df.format(Math.abs(entry.getValue())) + " " + entry.getKey();
groupViewHolder.balanceTextView.setText(balance);
groupViewHolder.balanceTextView.setTextColor(ContextCompat.getColor(context, R.color.colorAccent));
}
}
}
}
}
else
{
groupViewHolder.balanceTextTextView.setText(R.string.no_debts);
groupViewHolder.balanceTextTextView.setTextColor(ContextCompat.getColor(context, R.color.colorSecondaryText));
groupViewHolder.balanceTextView.setText("0.0 " + defaultCurrency);
groupViewHolder.balanceTextView.setTextColor(ContextCompat.getColor(context, R.color.colorAccent));
}*/
}
}
use of com.polito.mad17.madmax.entities.Group in project MadMax by deviz92.
the class HashMapGroupsAdapter method getView.
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final View result;
if (convertView == null) {
result = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
} else {
result = convertView;
}
TextView name = (TextView) result.findViewById(R.id.tv_sender);
if (position == (mData.size() - 1)) {
Log.d(TAG, "item.getKey().equals(\"nullGroup\")");
// groupViewHolder.imageView.
name.setText("");
} else {
Map.Entry<String, Group> item = getItem(position);
name.setText(item.getValue().getName());
ImageView img_photo = (ImageView) result.findViewById(R.id.img_photo);
Glide.with(parent.getContext()).load(item.getValue().getImage()).centerCrop().bitmapTransform(new CropCircleTransformation(parent.getContext())).diskCacheStrategy(DiskCacheStrategy.ALL).into(img_photo);
Log.d("Adapter image: ", item.getValue().getImage());
result.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String groupID = getItem(position).getKey();
}
});
}
return result;
}
use of com.polito.mad17.madmax.entities.Group in project MadMax by deviz92.
the class MainActivity method onStart.
@Override
protected void onStart() {
super.onStart();
Log.i(TAG, "onStart");
startingIntent = getIntent();
currentFragment = startingIntent.getIntExtra("currentFragment", 1);
// start declaration of a listener on all the current user data -> attached in onStart()
currentUserListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d(TAG, "onDataChange currentUserref");
if (currentUser == null) {
// makeText(MainActivity.this, "Ricreato user", Toast.LENGTH_SHORT).show(); // todo: di debug, da rimuovere
currentUser = new User();
}
currentUser.setID(currentUID);
currentUser.setName(dataSnapshot.child("name").getValue(String.class));
currentUser.setSurname(dataSnapshot.child("surname").getValue(String.class));
currentUser.setUsername(dataSnapshot.child("username").getValue(String.class));
currentUser.setProfileImage(dataSnapshot.child("image").getValue(String.class));
currentUser.setEmail(dataSnapshot.child("email").getValue(String.class));
currentUser.setPassword(dataSnapshot.child("password").getValue(String.class));
Log.d(TAG, "taken basic data of currentUser " + currentUser.toString());
// get user friends's IDs
if (dataSnapshot.child("friends").hasChildren())
for (DataSnapshot friend : dataSnapshot.child("friends").getChildren()) {
if (!friend.hasChild("deleted") || !friend.child("deleted").getValue(Boolean.class))
currentUser.getUserFriends().put(friend.getKey(), null);
}
// get user groups's IDs
if (dataSnapshot.child("groups").hasChildren())
for (DataSnapshot group : dataSnapshot.child("groups").getChildren()) {
if (group.getValue(Boolean.class))
currentUser.getUserGroups().put(group.getKey(), null);
}
Log.d(TAG, "Taken friends and groups, now creating the adapter");
if (currentFragment != null) {
viewPager.setCurrentItem(currentFragment);
updateFab(currentFragment);
} else {
viewPager.setCurrentItem(1);
updateFab(1);
}
// load nav menu header data for the current user
loadNavHeader();
Log.d(TAG, "logged user name: " + currentUser.getName());
Log.d(TAG, "logged user surname: " + currentUser.getSurname());
Uri data = startingIntent.getData();
if (data != null) {
inviterID = data.getQueryParameter("inviterID");
groupToBeAddedID = data.getQueryParameter("groupToBeAddedID");
} else {
// 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");
}
if (startingIntent.hasExtra("groupToBeAddedID")) {
groupToBeAddedID = startingIntent.getStringExtra("groupToBeAddedID");
startingIntent.removeExtra("groupToBeAddedID");
}
}
// control if user that requires the friendship is already a friend
if (inviterID != null) {
if (!currentUser.getUserFriends().containsKey(inviterID)) {
FirebaseUtils.getInstance().addFriend(inviterID);
inviterID = null;
makeText(MainActivity.this, getString(R.string.new_friend), Toast.LENGTH_LONG).show();
}
/*else
makeText(MainActivity.this, getString(R.string.already_friends), Toast.LENGTH_LONG).show();*/
}
// control if user is already part of requested group
if (groupToBeAddedID != null) {
if (!currentUser.getUserGroups().containsKey(groupToBeAddedID)) {
// currentUser.joinGroup(groupToBeAddedID); //todo usare questa? non aggiorna il numero dei membri
currentUser.getUserGroups().put(groupToBeAddedID, null);
FirebaseUtils.getInstance().joinGroupFirebase(currentUID, groupToBeAddedID);
groupToBeAddedID = null;
makeText(MainActivity.this, getString(R.string.join_group), Toast.LENGTH_LONG).show();
}
// else
// makeText(MainActivity.this, getString(R.string.already_in_group) + "\""+currentUser.getUserGroups().get(groupToBeAddedID).getName()+"\"", Toast.LENGTH_LONG).show();
}
if (startingIntent.hasExtra("notificationTitle")) {
Intent notificationIntent = null;
switch(startingIntent.getStringExtra("notificationTitle")) {
case "notification_invite":
if (startingIntent.hasExtra("groupID")) {
notificationIntent = new Intent(getApplicationContext(), GroupDetailActivity.class);
notificationIntent.putExtra("groupID", startingIntent.getStringExtra("groupID"));
}
break;
case "notification_expense_added":
if (startingIntent.hasExtra("groupID")) {
notificationIntent = new Intent(getApplicationContext(), ExpenseDetailActivity.class);
notificationIntent.putExtra("groupID", startingIntent.getStringExtra("groupID"));
if (startingIntent.hasExtra("expenseID")) {
notificationIntent.putExtra("expenseID", startingIntent.getStringExtra("expenseID"));
}
}
break;
case "notification_expense_removed":
if (startingIntent.hasExtra("groupID")) {
notificationIntent = new Intent(getApplicationContext(), GroupDetailActivity.class);
notificationIntent.putExtra("groupID", startingIntent.getStringExtra("groupID"));
}
break;
case "notification_proposalExpense_added":
if (startingIntent.hasExtra("expenseID")) {
notificationIntent = new Intent(getApplicationContext(), PendingExpenseDetailActivity.class);
notificationIntent.putExtra("expenseID", startingIntent.getStringExtra("expenseID"));
}
break;
}
if (notificationIntent != null) {
notificationIntent.putExtra("userID", currentUID);
startingIntent.removeExtra("notificationTitle");
startActivityForResult(notificationIntent, REQUEST_NOTIFICATION);
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
// TODO: come gestire?
Log.d(TAG, "getting current user failed");
}
};
// end of listener declaration on all the current user data
authListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
Log.d(TAG, "onAuthStateChanged");
currentFirebaseUser = firebaseAuth.getCurrentUser();
if (currentFirebaseUser != null && currentFirebaseUser.isEmailVerified()) {
// getting reference to the user from db
currentUID = currentFirebaseUser.getUid();
currentUserRef = usersRef.child(currentUID);
// take refreshed toked and save it to use FCM
currentUserRef.child("token").setValue(FirebaseInstanceId.getInstance().getToken());
Log.d(TAG, "device token: " + FirebaseInstanceId.getInstance().getToken());
// attach a listener on all the current user data
currentUserRef.addValueEventListener(currentUserListener);
} else {
Log.d(TAG, "current user is null, so go to login activity");
Intent goToLogin = new Intent(getApplicationContext(), LoginSignUpActivity.class);
// currentUser = null;
startActivity(goToLogin);
auth.removeAuthStateListener(authListener);
finish();
}
}
};
// attach the listener to the FirebaseAuth instance
auth.addAuthStateListener(authListener);
}
use of com.polito.mad17.madmax.entities.Group in project MadMax by deviz92.
the class FirebaseUtils method leaveGroupFirebase.
public Integer leaveGroupFirebase(String userID, final String groupID) {
Group g = GroupsFragment.groups.get(groupID);
if (g != null) {
HashMap<String, Double> balances = g.getCurrencyBalances();
Boolean debt = false;
Log.d(TAG, g.getName() + " contains those balances: ");
if (balances != null) {
for (Map.Entry<String, Double> entry : balances.entrySet()) {
Log.d(TAG, entry.getKey() + " " + entry.getValue());
if (entry.getValue() < 0)
debt = true;
}
}
if (debt) {
Log.d(TAG, "Cannot leave group");
return 0;
} else {
Log.d(TAG, "Nessuno debito, abbandono in corso");
// Elimino gruppo da lista gruppi dello user
databaseReference.child("users").child(userID).child("groups").child(groupID).setValue(false);
// Elimino user dalla lista dei members del gruppo
databaseReference.child("groups").child(groupID).child("members").child(userID).child("deleted").setValue(true);
// Elimino gruppo da cache
GroupsFragment.groups.remove(groupID);
// Elimino gruppo dagli shared groups tra me e ogni membro del gruppo
deleteSharedGroup(MainActivity.getCurrentUID(), groupID);
return 2;
}
} else {
Log.d(TAG, "Bilancio del gruppo: " + groupID + " non disponibile adesso. Riprovare.");
return null;
}
}
use of com.polito.mad17.madmax.entities.Group in project MadMax by deviz92.
the class MainActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate");
FirebaseUtils.getInstance().setUp();
firebaseDatabase = FirebaseUtils.getFirebaseDatabase();
databaseReference = FirebaseUtils.getDatabaseReference();
usersRef = databaseReference.child("users");
groupRef = databaseReference.child("groups");
auth = FirebaseAuth.getInstance();
// get data from firebase invite, if present
startingIntent = getIntent();
Uri data = startingIntent.getData();
if (data != null) {
inviterID = data.getQueryParameter("inviterID");
groupToBeAddedID = data.getQueryParameter("groupToBeAddedID");
} else {
// nretrieving 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");
} else
inviterID = null;
if (startingIntent.hasExtra("groupToBeAddedID")) {
groupToBeAddedID = startingIntent.getStringExtra("groupToBeAddedID");
startingIntent.removeExtra("groupToBeAddedID");
} else
groupToBeAddedID = null;
}
currentFragment = startingIntent.getIntExtra("currentFragment", 1);
if (auth.getCurrentUser() == null || !auth.getCurrentUser().isEmailVerified()) {
if (auth.getCurrentUser() != null) {
auth.signOut();
}
Intent doLogin = new Intent(getApplicationContext(), LoginSignUpActivity.class).setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
if (inviterID != null)
doLogin.putExtra("inviterID", inviterID);
if (groupToBeAddedID != null)
doLogin.putExtra("groupToBeAddedID", groupToBeAddedID);
startActivity(doLogin);
// 0 for no animation
overridePendingTransition(0, 0);
finish();
}
if (auth.getCurrentUser() != null) {
currentUID = auth.getCurrentUser().getUid();
}
// in the main we don't want an expansible bar
AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.app_bar);
appBarLayout.setExpanded(false);
// todo: capire come bloccare la barra nel main
// insert tabs and current fragment in the main layout
mainView.addView(getLayoutInflater().inflate(R.layout.skeleton_tab, null));
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText(friends));
tabLayout.addTab(tabLayout.newTab().setText(R.string.groups));
tabLayout.addTab(tabLayout.newTab().setText(R.string.pending));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
Log.d(TAG, String.valueOf(tab.getPosition()));
updateFab(tab.getPosition());
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager = (ViewPager) findViewById(R.id.main_view_pager);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
adapter = new MainActivityPagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
progressDialog = new ProgressDialog(this);
}
Aggregations