Search in sources :

Example 1 with Message

use of garbagecollectors.com.unipool.Message in project UniPool by divya21raj.

the class BaseActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    try {
        super.onCreate(savedInstanceState);
        setContentView(getContentViewId());
        mAuth = FirebaseAuth.getInstance();
        currentUser = mAuth.getCurrentUser();
        userDatabaseReference = FirebaseDatabase.getInstance().getReference("users/" + finalCurrentUser.getUserId());
        userMessageDatabaseReference = FirebaseDatabase.getInstance().getReference("messages/" + finalCurrentUser.getUserId());
        MessageDBTask.addOnCompleteListener(task -> {
            DataSnapshot messageData = (DataSnapshot) MessageDBTask.getResult();
            for (DataSnapshot dataSnapshot : messageData.getChildren()) {
                Message message = dataSnapshot.getValue(Message.class);
                assert message != null;
                if (!(message.getMessageId().equals("def@ult")))
                    UtilityMethods.putMessageInMap(messages, message);
            }
        });
        entryDatabaseReference.addChildEventListener(new ChildEventListener() {

            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                TripEntry tripEntry = dataSnapshot.getValue(TripEntry.class);
                UtilityMethods.updateTripList(tripEntryList, tripEntry);
                HomeActivity.updateRecycleAdapter();
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                TripEntry tripEntry = dataSnapshot.getValue(TripEntry.class);
                UtilityMethods.updateTripList(tripEntryList, tripEntry);
                HomeActivity.updateRecycleAdapter();
            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {
                TripEntry tripEntry = dataSnapshot.getValue(TripEntry.class);
                if (tripEntry != null) {
                    UtilityMethods.removeFromList(tripEntryList, tripEntry.getEntry_id());
                    HomeActivity.updateRecycleAdapter();
                }
            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {
            // IDK
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                // Failed to read value
                Log.w("Hello", "Failed to read value.", databaseError.toException());
                Toast.makeText(getApplicationContext(), "Network Issues!", Toast.LENGTH_SHORT).show();
            }
        });
        userDatabaseReference.addValueEventListener(new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                // whenever data at this location is updated.
                try {
                    finalCurrentUser = dataSnapshot.getValue(User.class);
                    UtilityMethods.populateChatMap(dataSnapshot);
                    ReceivedRequestsFragment.refreshRecycler();
                    SentRequestsFragment.refreshRecycler();
                    ChatFragment.refreshRecycler();
                } catch (DatabaseException dbe) {
                    Toast.makeText(getApplicationContext(), "Some problems, mind restarting the app?", Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onCancelled(DatabaseError error) {
                // Failed to read value
                Log.w("UserDB", "Failed to read userDB value.", error.toException());
                Toast.makeText(getApplicationContext(), "Network Issues!", Toast.LENGTH_SHORT).show();
            }
        });
        bottomNavigationView = findViewById(R.id.bottom_navigation);
        bottomNavigationView.setOnNavigationItemSelectedListener(this);
    } catch (NullPointerException nlp) {
        finish();
        startActivity(new Intent(getApplicationContext(), LoginActivity.class));
    }
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) Message(garbagecollectors.com.unipool.Message) TripEntry(garbagecollectors.com.unipool.TripEntry) Intent(android.content.Intent) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot) DatabaseException(com.google.firebase.database.DatabaseException) ChildEventListener(com.google.firebase.database.ChildEventListener)

Example 2 with Message

use of garbagecollectors.com.unipool.Message in project UniPool by divya21raj.

the class MessageListActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_message_list);
    messagesLayout = findViewById(R.id.layout1);
    sendButton = findViewById(R.id.sendButton);
    messageArea = findViewById(R.id.message_edit_text);
    scrollView = findViewById(R.id.scrollView);
    personalMessageMap = new TreeMap<>();
    messagesOnScreen = new ArrayList<>();
    messageProgressDialog = new ProgressDialog(this);
    messageProgressDialog.setMessage("Fetching your messages...");
    messageProgressDialog.show();
    setScrollViewToBottom();
    DatabaseReference userMessageDatabaseReference = FirebaseDatabase.getInstance().getReference("messages/" + BaseActivity.getFinalCurrentUser().getUserId());
    // load from local
    userMessageDatabaseReference.addChildEventListener(new ChildEventListener() {

        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            Message message = dataSnapshot.getValue(Message.class);
            if (message != null) {
                UtilityMethods.putMessageInMap(BaseActivity.getMessages(), message);
                if (!message.getMessageId().equals("def@ult") && (message.getSenderId().equals(chatUser.getUserId()) || message.getReceiverId().equals(chatUser.getUserId()))) {
                    personalMessageMap.containsKey(message.getCreatedAtTime());
                    if (!messagesOnScreen.contains(message.getMessageId())) {
                        showMessage(message);
                    }
                } else if (message.getMessageId().equals("def@ult"))
                    messageProgressDialog.dismiss();
            }
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            // Failed to read value
            Log.w("Hello", "Failed to read value.", databaseError.toException());
            Toast.makeText(getApplicationContext(), "Problems! Couldn't fetch messages...", Toast.LENGTH_LONG).show();
        }
    });
    sendButton.setOnClickListener(view -> {
        String typedMessage = messageArea.getText().toString();
        if (!typedMessage.isEmpty()) {
            Message message = new Message("", pairUp.getPairUpId(), typedMessage, BaseActivity.getFinalCurrentUser().getUserId(), chatUser.getUserId(), UtilityMethods.getCurrentTime());
            // online update
            UtilityMethods.putMessageOnDB(message, chatUser, BaseActivity.getFinalCurrentUser());
            // local update
            UtilityMethods.putMessageInMap(BaseActivity.getMessages(), message);
            HashMap<String, String> notificationObject = new HashMap<>();
            notificationObject.put("from", BaseActivity.getFinalCurrentUser().getUserId());
            notificationObject.put("type", "chat");
            notificationDatabaseReference.child(chatUser.getUserId()).push().setValue(notificationObject);
            messageArea.setText("");
            personalMessageMap.put(message.getCreatedAtTime(), message);
            showMessage(message);
        }
    });
    // detecting if keyboard on-screen
    final View activityRootView = findViewById(R.id.activity_message_list);
    activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
        int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
        if (heightDiff > UtilityMethods.dpToPx(MessageListActivity.this, 200))
            setScrollViewToBottom();
    });
}
Also used : Message(garbagecollectors.com.unipool.Message) DatabaseReference(com.google.firebase.database.DatabaseReference) BaseActivity.notificationDatabaseReference(garbagecollectors.com.unipool.activities.BaseActivity.notificationDatabaseReference) HashMap(java.util.HashMap) ProgressDialog(android.app.ProgressDialog) DataSnapshot(com.google.firebase.database.DataSnapshot) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) ScrollView(android.widget.ScrollView) DatabaseError(com.google.firebase.database.DatabaseError) ChildEventListener(com.google.firebase.database.ChildEventListener)

Example 3 with Message

use of garbagecollectors.com.unipool.Message in project UniPool by divya21raj.

the class LoginActivity method dummyInitFinalCurrentUser.

private void dummyInitFinalCurrentUser(FirebaseUser user) throws ParseException {
    HashMap<String, Float> dummyLambdaMap = new HashMap<>();
    dummyLambdaMap.put("123", 0f);
    GenLocation dummyGenLocation = new GenLocation("dummy", "dummy", 0d, 0d);
    TripEntry dummyTripEntry = new TripEntry("dummy", "dummyId", "DummyUser", "12:00", "1/11/12", dummyGenLocation, dummyGenLocation, dummyLambdaMap);
    HashMap<String, TripEntry> dummyUserEntries = new HashMap<>();
    dummyUserEntries.put("dummy", dummyTripEntry);
    HashMap<String, TripEntry> dummyRequestSent = new HashMap<>();
    dummyRequestSent.put(dummyTripEntry.getEntry_id(), dummyTripEntry);
    ArrayList<String> dummyUserIdList = new ArrayList<>();
    dummyUserIdList.add("dummy");
    HashMap<String, ArrayList<String>> dummyRequestReceived = new HashMap<>();
    dummyRequestReceived.put("dummy", dummyUserIdList);
    Message dummyMessage = new Message("dummy", "", "", "dummy", "dummy", 1L);
    ArrayList<Message> dummyMessages = new ArrayList<>();
    dummyMessages.add(dummyMessage);
    PairUp dummyPairUp = new PairUp("dummydummy", "dummy", "dummy", "dummy", dummyUserIdList);
    HashMap<String, PairUp> dummyPairUps = new HashMap<>();
    dummyPairUps.put("dummy", dummyPairUp);
    String deviceToken = FirebaseInstanceId.getInstance().getToken();
    String url = "";
    Uri photoUrl = user.getPhotoUrl();
    if (photoUrl != null)
        url = photoUrl.toString();
    finalCurrentUser = new User(user.getUid(), user.getDisplayName(), url, dummyUserEntries, dummyRequestSent, dummyRequestReceived, deviceToken, true, dummyPairUps);
}
Also used : User(garbagecollectors.com.unipool.User) FirebaseUser(com.google.firebase.auth.FirebaseUser) BaseActivity.finalCurrentUser(garbagecollectors.com.unipool.activities.BaseActivity.finalCurrentUser) Message(garbagecollectors.com.unipool.Message) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TripEntry(garbagecollectors.com.unipool.TripEntry) Uri(android.net.Uri) PairUp(garbagecollectors.com.unipool.PairUp) GenLocation(garbagecollectors.com.unipool.GenLocation)

Aggregations

Message (garbagecollectors.com.unipool.Message)3 ChildEventListener (com.google.firebase.database.ChildEventListener)2 DataSnapshot (com.google.firebase.database.DataSnapshot)2 DatabaseError (com.google.firebase.database.DatabaseError)2 TripEntry (garbagecollectors.com.unipool.TripEntry)2 HashMap (java.util.HashMap)2 ProgressDialog (android.app.ProgressDialog)1 Intent (android.content.Intent)1 Uri (android.net.Uri)1 View (android.view.View)1 ImageView (android.widget.ImageView)1 ScrollView (android.widget.ScrollView)1 TextView (android.widget.TextView)1 FirebaseUser (com.google.firebase.auth.FirebaseUser)1 DatabaseException (com.google.firebase.database.DatabaseException)1 DatabaseReference (com.google.firebase.database.DatabaseReference)1 ValueEventListener (com.google.firebase.database.ValueEventListener)1 GenLocation (garbagecollectors.com.unipool.GenLocation)1 PairUp (garbagecollectors.com.unipool.PairUp)1 User (garbagecollectors.com.unipool.User)1