-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_java_com_example_harsh_freechat_MainActivity.java
110 lines (87 loc) · 3.31 KB
/
main_java_com_example_harsh_freechat_MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.example.harsh.freechat;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toolbar;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ServerValue;
public class MainActivity extends AppCompatActivity {
private FirebaseAuth mAuth;
private android.support.v7.widget.Toolbar mToolbar;
private ViewPager mViewPager;
private SectionsPagerAdapter mSectionsPagerAdapter;
private DatabaseReference mUserRef;
private TabLayout mTablayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
mToolbar=findViewById(R.id.main_page_toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("Free Chat");
if(mAuth.getCurrentUser()!=null) {
mUserRef = FirebaseDatabase.getInstance().getReference().child("Users").child(mAuth.getCurrentUser().getUid());
}
mViewPager=findViewById(R.id.main_tabPager);
mSectionsPagerAdapter= new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mSectionsPagerAdapter);
mTablayout=findViewById(R.id.main_tabs);
mTablayout.setupWithViewPager(mViewPager);
}
@Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser == null) {
sendtoStart();
} else{
mUserRef.child("online").setValue("true");
}
}
protected void onStop() {
super.onStop();
FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser!=null) {
mUserRef.child("online").setValue(ServerValue.TIMESTAMP);
}
}
private void sendtoStart() {
Intent startIntent=new Intent(MainActivity.this,StartActivity.class);
startActivity(startIntent);
finish();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.main_menu,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
if(item.getItemId()==R.id.main_logout_btn){
FirebaseAuth.getInstance().signOut();
sendtoStart();
}
if(item.getItemId()==R.id.main_settings_btn){
Intent settingsintent=new Intent(MainActivity.this,SettingsActivity.class);
startActivity(settingsintent);
}
if(item.getItemId()==R.id.main_all_btn)
{
Intent settingsintent=new Intent(MainActivity.this,UsersActivity.class);
startActivity(settingsintent);
}
return true;
}
}