MainActivity
package com.firebaseintegration.activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ProgressBar;
import com.firebaseintegration.R;
import com.firebaseintegration.adapter.FriendsAdapter;
import com.firebaseintegration.controller.ConstantData;
import com.firebaseintegration.controller.RecyclerItemClickListener;
import com.firebaseintegration.controller.Utils;
import com.firebaseintegration.conversation.ConversationActivity;
import com.google.firebase.crash.FirebaseCrash;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private SharedPreferences preferences;
private RecyclerView recyclerView;
private CoordinatorLayout coordinatorLayout;
private DatabaseReference databaseReference;
private Boolean isBoolean = false;
private List<HashMap<String, String>> listOfAllFriends = new ArrayList<>();
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setTitle("All Friends");
preferences = getSharedPreferences(ConstantData.PREFERENCES, MODE_PRIVATE);
FirebaseCrash.report(new Exception("MainActivity"));
databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl(ConstantData.URL);
initUI();
isBoolean = true;
getAllFriends();
onLineOffLine(true);
}
private void initUI() {
coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Intent intent = new Intent(MainActivity.this, ConversationActivity.class);
intent.putExtra("map", listOfAllFriends.get(position));
startActivity(intent);
}
})
);
}
private void getAllFriends() {
if (Utils.isNetworkAvailable(this)) {
progressBar.setVisibility(View.VISIBLE);
databaseReference.child("user").addValueEventListener(
new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (isBoolean) {
isBoolean = false;
if (dataSnapshot.getValue() != null) {
for (DataSnapshot messageSnapshot : dataSnapshot.getChildren()) {
HashMap<String, String> map = new HashMap<>();
map.put("dob", (String) messageSnapshot.child("dob").getValue());
map.put("email", (String) messageSnapshot.child("email").getValue());
map.put("gender", (String) messageSnapshot.child("gender").getValue());
map.put("name", (String) messageSnapshot.child("name").getValue());
map.put("phone", (String) messageSnapshot.child("phone").getValue());
map.put("profile_picture", (String) messageSnapshot.child("profile_picture").getValue());
map.put("status", (String) messageSnapshot.child("status").getValue());
String userEmail = preferences.getString("email", "");
String email = (String) messageSnapshot.child("email").getValue();
if (!userEmail.equals(email)) {
listOfAllFriends.add(map);
}
}
setData();
} else {
progressBar.setVisibility(View.GONE);
Utils.showSnackbar(coordinatorLayout, "Friends not found.");
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
progressBar.setVisibility(View.GONE);
Utils.showSnackbar(coordinatorLayout, "" + databaseError);
}
});
} else {
Utils.showSnackbar(coordinatorLayout, getResources().getString(R.string.internet_error));
}
}
public void setData() {
progressBar.setVisibility(View.GONE);
if (listOfAllFriends.size() != 0) {
FriendsAdapter friendsAdapter = new FriendsAdapter(this, listOfAllFriends);
recyclerView.setAdapter(friendsAdapter);
} else {
Utils.showSnackbar(coordinatorLayout, "Friends not found.");
}
}
@Override
public void onBackPressed() {
onLineOffLine(false);
super.onBackPressed();
}
public void onLineOffLine(boolean isBoolean) {
String key = preferences.getString("key", "");
if (!Utils.isStringNull(key)) {
if (isBoolean) {
databaseReference.child("user").child(key).child("status").setValue("1");
} else {
databaseReference.child("user").child(key).child("status").setValue("0");
}
}
}
}
activity_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:id="@+id/rlMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:visibility="gone" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:clipToPadding="false"
android:divider="@null"
android:dividerHeight="5dp"
android:overScrollMode="never"
android:scrollbars="vertical" />
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.firebaseintegration">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:name=".activity.App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme.NoActionBar">
<activity
android:name=".activity.SplashScreen"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activity.LoginActivity"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar"
android:windowSoftInputMode="stateHidden" />
<activity
android:name=".activity.RegisterActivity"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar"
android:windowSoftInputMode="stateHidden" />
<activity
android:name=".activity.ForgotPasswordActivity"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar"
android:windowSoftInputMode="stateHidden" />
<activity
android:name=".activity.MainActivity"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar"
android:windowSoftInputMode="stateHidden" />
<activity
android:name=".conversation.ConversationActivity"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar"
android:windowSoftInputMode="stateHidden" />
<activity
android:name=".conversation.FullScreenImageActivity"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar"
android:windowSoftInputMode="stateHidden" />
<!-- Firebase push notification -->
<service android:name=".pushnotification.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".pushnotification.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
</application>
</manifest>
Full Source Code Download Here
No comments:
Post a Comment