Search in sources :

Example 76 with ValueEventListener

use of com.google.firebase.database.ValueEventListener in project Team-Plant-Power by Alexander1994.

the class MyServiceNotification method LightNotification.

// notification LIGHT
public void LightNotification() {
    // get range
    firebaseReference = database.getReference("range/light");
    firebaseReference.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // This method is called once with the initial value and again
            // whenever data at this location is updated.
            lightRange = dataSnapshot.getValue(Range.class);
        }

        @Override
        public void onCancelled(DatabaseError error) {
        // Failed to read value
        // Log.w(TAG, "Failed to read value.", error.toException());
        }
    });
    // get light
    firebaseReference = database.getReference("currentLight");
    firebaseReference.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // This method is called once with the initial value and again
            // whenever data at this location is updated.
            String value = dataSnapshot.getValue(String.class);
            // remove any characters like letters or symbols
            double lightx = Double.parseDouble(value.replaceAll("[^\\d.]", ""));
            lightUI.setPercentLight(lightx);
        }

        @Override
        public void onCancelled(DatabaseError error) {
        // Failed to read value
        // Log.w(TAG, "Failed to read value.", error.toException());
        }
    });
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    if (lightRange.isRangeSet() && !lightRange.isInRange(lightUI.getPercentLight())) {
        android.support.v4.app.NotificationCompat.Builder mBuilder2 = new NotificationCompat.Builder(this).setSmallIcon(sun).setContentTitle("warning").setContentText("Light out of Range  " + lightUI.getPercentLight());
        // Sets an ID for the notification
        int mNotificationId = 002;
        // Gets an instance of the NotificationManager service
        NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // Builds the notification and issues it.
        mNotifyMgr.notify(mNotificationId, mBuilder2.build());
    } else {
    }
}
Also used : NotificationManager(android.app.NotificationManager) DataSnapshot(com.google.firebase.database.DataSnapshot) DatabaseError(com.google.firebase.database.DatabaseError) NotificationCompat(android.support.v7.app.NotificationCompat) ValueEventListener(com.google.firebase.database.ValueEventListener)

Example 77 with ValueEventListener

use of com.google.firebase.database.ValueEventListener in project Team-Plant-Power by Alexander1994.

the class MyServiceNotification method HumidityNotification.

// notification HUMIDITY
public void HumidityNotification() {
    // get range
    firebaseReference = database.getReference("range/humidity");
    firebaseReference.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // This method is called once with the initial value and again
            // whenever data at this location is updated.
            humidityRange = dataSnapshot.getValue(Range.class);
        }

        @Override
        public void onCancelled(DatabaseError error) {
        // Failed to read value
        // Log.w(TAG, "Failed to read value.", error.toException());
        }
    });
    // get humidity
    firebaseReference = database.getReference("currentHumidity");
    firebaseReference.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // This method is called once with the initial value and again
            // whenever data at this location is updated.
            String value = dataSnapshot.getValue(String.class);
            // remove any characters like letters or symbols
            double humidityx = Double.parseDouble(value.replaceAll("[^\\d.]", ""));
            humidityUI.setPercentHumidity(humidityx);
        }

        @Override
        public void onCancelled(DatabaseError error) {
        // Failed to read value
        // Log.w(TAG, "Failed to read value.", error.toException());
        }
    });
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    if (humidityRange.isRangeSet() && !humidityRange.isInRange(humidityUI.getPercentHumidity())) {
        android.support.v4.app.NotificationCompat.Builder mBuilder3 = new NotificationCompat.Builder(this).setSmallIcon(water).setContentTitle("warning").setContentText("Humidity out of Range  " + humidityUI.getPercentHumidity());
        // Sets an ID for the notification
        int mNotificationId = 003;
        // Gets an instance of the NotificationManager service
        NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // Builds the notification and issues it.
        mNotifyMgr.notify(mNotificationId, mBuilder3.build());
    } else {
    }
}
Also used : NotificationManager(android.app.NotificationManager) DataSnapshot(com.google.firebase.database.DataSnapshot) DatabaseError(com.google.firebase.database.DatabaseError) NotificationCompat(android.support.v7.app.NotificationCompat) ValueEventListener(com.google.firebase.database.ValueEventListener)

Example 78 with ValueEventListener

use of com.google.firebase.database.ValueEventListener in project MadMax by deviz92.

the class PayExpenseActivity method payDebtForExpense.

// money = cifra che ho a disposizione per ripianare i debiti
void payDebtForExpense(final String userID, final String expenseID, Double money) {
    myMoney = money;
    databaseReference.child("expenses").child(expenseID).addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String creatorID = dataSnapshot.child("creatorID").getValue(String.class);
            Double alreadyPaidByCreator = dataSnapshot.child("participants").child(creatorID).child("alreadyPaid").getValue(Double.class);
            // dice se user contribuisce o no a quella spesa
            Boolean involved = false;
            for (DataSnapshot participantSnapshot : dataSnapshot.child("participants").getChildren()) {
                // todo poi gestire caso in cui utente viene tolto dai participant alla spesa
                if (participantSnapshot.getKey().equals(userID))
                    involved = true;
            }
            // se user ha partecipato alla spesa
            if (involved) {
                // alreadyPaid = soldi già messi dallo user per quella spesa
                // dueImport = quota che user deve mettere per quella spesa
                Double alreadyPaid = dataSnapshot.child("participants").child(userID).child("alreadyPaid").getValue(Double.class);
                Log.d(TAG, "Fraction: " + Double.parseDouble(String.valueOf(dataSnapshot.child("participants").child(userID).child("fraction").getValue())));
                Double amount = dataSnapshot.child("amount").getValue(Double.class);
                Double dueImport = Double.parseDouble(String.valueOf(dataSnapshot.child("participants").child(userID).child("fraction").getValue())) * amount;
                Double stillToPay = dueImport - alreadyPaid;
                // Se questa spesa non è già stata ripagata in toto
                if (stillToPay > 0) {
                    // Se ho ancora abbastanza soldi per ripagare in toto questa spesa, la ripago in toto AL CREATOR!!
                    if (myMoney >= stillToPay) {
                        // Quota già pagata DA ME per questa spesa aumenta
                        databaseReference.child("expenses").child(expenseID).child("participants").child(userID).child("alreadyPaid").setValue(dueImport);
                        // Quota già pagata DAL CREATOR per questa spesa diminuisce, perchè gli sto dando dei soldi
                        databaseReference.child("expenses").child(expenseID).child("participants").child(creatorID).child("alreadyPaid").setValue(alreadyPaidByCreator - stillToPay);
                        // Adesso ho meno soldi a disposizione, perchè li ho usati in parte per ripagare questa spesa
                        myMoney -= stillToPay;
                    } else // Altrimenti la ripago solo in parte
                    {
                        databaseReference.child("expenses").child(expenseID).child("participants").child(userID).child("alreadyPaid").setValue(alreadyPaid + myMoney);
                        databaseReference.child("expenses").child(expenseID).child("participants").child(creatorID).child("alreadyPaid").setValue(alreadyPaidByCreator - myMoney);
                        myMoney = 0d;
                    }
                }
            }
        }

        @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 79 with ValueEventListener

use of com.google.firebase.database.ValueEventListener in project MadMax by deviz92.

the class PendingExpenseDetailFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    Log.i(TAG, "onCreateView");
    // Read expenseID from ExpenseDetailPagerAdapter
    Bundle b = this.getArguments();
    expenseID = b.getString("expenseID");
    final View view = inflater.inflate(R.layout.skeleton_list, container, false);
    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);
    // todo mettere a posto
    votersViewAdapter = new VotersViewAdapter(voters, getContext());
    recyclerView.setAdapter(votersViewAdapter);
    // Retrieve data of this pending expense
    databaseReference.child("proposedExpenses").child(expenseID).addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // Show list of voters for this pending expense
            for (DataSnapshot voterSnap : dataSnapshot.child("participants").getChildren()) {
                String vote = voterSnap.child("vote").getValue(String.class);
                FirebaseUtils.getInstance().getVoter(voterSnap.getKey(), vote, voters, votersViewAdapter);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
    return view;
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) Bundle(android.os.Bundle) RecyclerView(android.support.v7.widget.RecyclerView) ValueEventListener(com.google.firebase.database.ValueEventListener) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) DataSnapshot(com.google.firebase.database.DataSnapshot) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View)

Example 80 with ValueEventListener

use of com.google.firebase.database.ValueEventListener in project MadMax by deviz92.

the class SplitPolicyActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_split_policy);
    // somma quote già splittate
    totalTextView = (TextView) findViewById(R.id.total);
    // costo della spesa
    amountTextView = (TextView) findViewById(R.id.tv_amount);
    currencyTextView = (TextView) findViewById(R.id.currency);
    currencyAmountTextView = (TextView) findViewById(R.id.currency_amount);
    Intent intent = getIntent();
    amount = intent.getDoubleExtra("amount", 0);
    currency = intent.getStringExtra("currency");
    groupID = intent.getStringExtra("groupID");
    amountsList = (HashMap<String, Double>) intent.getSerializableExtra("participants");
    Log.d(TAG, "I just entered SplitPolicyActivity. amountsList contains: ");
    for (Map.Entry<String, Double> entry : amountsList.entrySet()) {
        Log.d(TAG, entry.getKey() + " " + entry.getValue());
    }
    totalSplit = intent.getDoubleExtra("totalSplit", 0d);
    totalTextView.setText(df.format(totalSplit));
    amountTextView.setText(df.format(amount));
    currencyTextView.setText(currency);
    currencyAmountTextView.setText(currency);
    participants.clear();
    // Retrieve info about members for this expense
    for (final Map.Entry<String, Double> entry : amountsList.entrySet()) {
        databaseReference.child("users").child(entry.getKey()).addListenerForSingleValueEvent(new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                User u = new User();
                u.setName(dataSnapshot.child("name").getValue(String.class));
                u.setSurname(dataSnapshot.child("surname").getValue(String.class));
                u.setProfileImage(dataSnapshot.child("image").getValue(String.class));
                u.setSplitPart(entry.getValue());
                u.setExpenseCurrency(currency);
                participants.put(entry.getKey(), u);
                splittersViewAdapter.update(participants);
                splittersViewAdapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }
    RecyclerView.ItemDecoration divider = new InsetDivider.Builder(SplitPolicyActivity.this).orientation(InsetDivider.VERTICAL_LIST).dividerHeight(getResources().getDimensionPixelSize(R.dimen.divider_height)).color(ContextCompat.getColor(SplitPolicyActivity.this, R.color.colorDivider)).insets(getResources().getDimensionPixelSize(R.dimen.divider_inset), 0).overlay(true).build();
    recyclerView = (RecyclerView) findViewById(R.id.rv_skeleton);
    layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.addItemDecoration(divider);
    splittersViewAdapter = new SplittersViewAdapter(participants, SplitPolicyActivity.this, this);
    recyclerView.setAdapter(splittersViewAdapter);
}
Also used : User(com.polito.mad17.madmax.entities.User) Intent(android.content.Intent) DataSnapshot(com.google.firebase.database.DataSnapshot) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) DatabaseError(com.google.firebase.database.DatabaseError) RecyclerView(android.support.v7.widget.RecyclerView) ValueEventListener(com.google.firebase.database.ValueEventListener) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

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