Second commit

This commit is contained in:
tonywesoly 2022-10-27 20:48:23 +02:00
parent 4a71378e39
commit ec8f6bbf54
17 changed files with 281 additions and 8 deletions

View File

@ -7,6 +7,8 @@
<entry key="app/src/main/res/layout/activity_login.xml" value="0.37083333333333335" />
<entry key="app/src/main/res/layout/activity_main.xml" value="0.35625" />
<entry key="app/src/main/res/layout/activity_sign_up.xml" value="0.4375" />
<entry key="app/src/main/res/layout/user_layout.xml" value="0.25" />
<entry key="app/src/main/res/menu/main_menu.xml" value="0.4375" />
</map>
</option>
</component>

View File

@ -1,5 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GitSharedSettings">
<option name="synchronizeBranchProtectionRules" value="false" />
</component>
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>

View File

@ -42,6 +42,7 @@ dependencies {
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'com.google.firebase:firebase-auth:19.2.0'
implementation 'com.google.firebase:firebase-database-ktx:20.1.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

View File

@ -1,6 +1,7 @@
{
"project_info": {
"project_number": "233851758467",
"firebase_url": "https://chatapplication-4eb49-default-rtdb.europe-west1.firebasedatabase.app",
"project_id": "chatapplication-4eb49",
"storage_bucket": "chatapplication-4eb49.appspot.com"
},

View File

@ -14,11 +14,15 @@
android:theme="@style/Theme.ChatApplication"
tools:targetApi="31">
<activity
android:name=".SignUp"
android:name=".ChatActivity"
android:exported="false" />
<activity
android:name=".SignUp"
android:exported="false"
android:parentActivityName=".LogIn" />
<activity
android:name=".LogIn"
android:exported="true" >
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
@ -27,7 +31,7 @@
</activity>
<activity
android:name=".MainActivity"
android:exported="false"/>
android:exported="false" />
</application>
</manifest>

View File

@ -0,0 +1,11 @@
package com.lsm.chatapplication
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class ChatActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_chat)
}
}

View File

@ -3,6 +3,7 @@ package com.lsm.chatapplication
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.google.firebase.auth.FirebaseAuth
import com.lsm.chatapplication.databinding.ActivityLogInBinding
@ -17,6 +18,8 @@ class LogIn : AppCompatActivity() {
val view = binding.root
setContentView(view)
// supportActionBar?.hide()
mAuth = FirebaseAuth.getInstance()
binding.logInBtn.setOnClickListener{
@ -31,6 +34,17 @@ class LogIn : AppCompatActivity() {
}
}
private fun logIn(email: String, password: String){
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
// Move to home
val intent = Intent(this@LogIn, MainActivity::class.java)
finish()
startActivity(intent)
} else {
// Logging error message
Toast.makeText(this@LogIn,"User doest not exits",Toast.LENGTH_SHORT).show()
}
}
}
}

View File

@ -1,11 +1,82 @@
package com.lsm.chatapplication
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.database.DataSnapshot
import com.google.firebase.database.DatabaseError
import com.google.firebase.database.ValueEventListener
import com.google.firebase.database.ktx.database
import com.google.firebase.ktx.Firebase
import com.lsm.chatapplication.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var userList: ArrayList<User>
private lateinit var adapter: UserAdapter
private lateinit var mAuth: FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
mAuth = FirebaseAuth.getInstance()
userList = ArrayList()
adapter = UserAdapter()
// adapter.submitList(userList)
binding.userRecyclerView.layoutManager = LinearLayoutManager(this)
binding.userRecyclerView.adapter = adapter
readUsersFromDatabase()
}
private fun readUsersFromDatabase(){
val mDbRef = Firebase.database("https://chatapplication-4eb49-default-rtdb.europe-west1.firebasedatabase.app/").reference
mDbRef.child("user").addValueEventListener(object: ValueEventListener{
override fun onDataChange(snapshot: DataSnapshot) {
userList.clear()
// for(postSnapshot in snapshot.children){
// val currentUser = postSnapshot.getValue(User::class.java)
// userList.add(currentUser!!)
// }
snapshot.children.forEach {
val currentUser = it.getValue(User::class.java)
userList.add(currentUser!!)
}
userList.removeAll(
userList.filter{it.uid == mAuth.currentUser?.uid}.toSet()
)
// userList.removeIf{it.uid == mAuth.currentUser?.uid}
adapter.submitList(userList)
}
override fun onCancelled(error: DatabaseError) {
TODO("Not yet implemented")
}
})
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.main_menu,menu)
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if(item.itemId == R.id.Logout){
mAuth.signOut()
finish()
val intent = Intent(this, LogIn::class.java)
startActivity(intent)
}
return true
}
}

View File

@ -1,13 +1,20 @@
package com.lsm.chatapplication
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.database.DatabaseReference
import com.google.firebase.database.FirebaseDatabase
import com.google.firebase.database.ktx.database
import com.google.firebase.ktx.Firebase
import com.lsm.chatapplication.databinding.ActivitySignUpBinding
class SignUp : AppCompatActivity() {
private lateinit var binding: ActivitySignUpBinding
private lateinit var mAuth: FirebaseAuth
// private lateinit var mDbRef: DatabaseReference
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -15,16 +22,37 @@ class SignUp : AppCompatActivity() {
val view = binding.root
setContentView(view)
// supportActionBar?.hide()
mAuth = FirebaseAuth.getInstance()
// mDbRef = Firebase.database("https://chatapplication-4eb49-default-rtdb.europe-west1.firebasedatabase.app/").reference
binding.signUpBtn.setOnClickListener{
val usrName = binding.edtUserName.text.toString()
val email = binding.edtEmail.text.toString()
val password = binding.edtPassword.text.toString()
signUp(email,password)
signUp(usrName, email,password)
}
}
private fun signUp(email: String, password: String){
private fun signUp(name:String, email: String, password: String){
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
// Jumping to home
addUserToDatabase(name, email, mAuth.currentUser?.uid!!)
val intent = Intent(this@SignUp,MainActivity::class.java)
finish()
startActivity(intent)
} else {
// Account creation error message
Toast.makeText(this@SignUp,task.exception?.toString(),Toast.LENGTH_LONG).show()
//Toast.makeText(this@SignUp,"Some error occurred",Toast.LENGTH_SHORT).show()
}
}
}
private fun addUserToDatabase(name: String, email: String, uid: String){
val mDbRef = Firebase.database("https://chatapplication-4eb49-default-rtdb.europe-west1.firebasedatabase.app/").reference
mDbRef.child("user").child(uid).setValue(User(name,email,uid))
}
}

View File

@ -0,0 +1,17 @@
package com.lsm.chatapplication
class User (var name: String?, var email: String?, var uid: String?){
constructor() : this(null,null,null) {}
override fun equals(other: Any?): Boolean {
return super.equals(other)
}
override fun hashCode(): Int {
var result = name?.hashCode() ?: 0
result = 31 * result + (email?.hashCode() ?: 0)
result = 31 * result + (uid?.hashCode() ?: 0)
return result
}
}

View File

@ -0,0 +1,53 @@
package com.lsm.chatapplication
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.lsm.chatapplication.databinding.UserLayoutBinding
//class UserAdapter(val userList: ArrayList<User>):
// RecyclerView.Adapter<UserAdapter.UserViewHolder>() {
//
// class UserViewHolder(val binding: UserLayoutBinding): RecyclerView.ViewHolder(binding.root)
//
// override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserViewHolder {
// return UserViewHolder(UserLayoutBinding.inflate(LayoutInflater.from(parent.context), parent, false))
// }
//
// override fun onBindViewHolder(holder: UserViewHolder, position: Int) {
// val currentUser = userList[position]
// holder.binding.userName.text = currentUser.name
// }
//
// override fun getItemCount(): Int {
// return userList.size
// }
//}
class UserAdapter():
ListAdapter<User,UserAdapter.UserViewHolder>(TaskDiffCallBack()){
class TaskDiffCallBack: DiffUtil.ItemCallback<User>() {
override fun areItemsTheSame(oldItem: User, newItem: User): Boolean {
return oldItem.uid == newItem.uid
}
override fun areContentsTheSame(oldItem: User, newItem: User): Boolean {
return oldItem == newItem
}
}
class UserViewHolder(val binding: UserLayoutBinding): RecyclerView.ViewHolder(binding.root)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserViewHolder {
return UserViewHolder(UserLayoutBinding.inflate(LayoutInflater.from(parent.context), parent, false))
}
override fun onBindViewHolder(holder: UserViewHolder, position: Int) {
val currentUser = getItem(position)
holder.binding.userName.text = currentUser.name
}
}

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ChatActivity">
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -6,4 +6,13 @@
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/user_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@layout/user_layout"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
app:cardBackgroundColor="@color/white"
app:cardCornerRadius="10dp"
app:cardElevation="4dp"
app:contentPadding="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="TextView"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/Logout"
android:title="@string/logout_menu" />
</menu>

View File

@ -6,4 +6,5 @@
<string name="user_name_hint">User Name</string>
<string name="sign_up_btn_text">Create new account</string>
<string name="options_separator_text">Or</string>
<string name="logout_menu">Log Out</string>
</resources>

View File

@ -1,7 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
dependencies {
classpath 'com.google.gms:google-services:4.3.13'
classpath 'com.google.gms:google-services:4.3.14'
}
}