-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add: Create group feature in android app (#285)
- Loading branch information
Showing
17 changed files
with
687 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
TalkIn/app/src/main/java/com/example/talk_in/CreateGroupActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.example.talk_in | ||
|
||
import android.content.Intent | ||
import android.os.Bundle | ||
import android.text.TextUtils | ||
import android.util.Log | ||
import android.widget.Toast | ||
import androidx.appcompat.app.AppCompatActivity | ||
import com.example.talk_in.databinding.ActivityCreateGroupBinding | ||
import com.google.firebase.database.DatabaseReference | ||
import com.google.firebase.database.FirebaseDatabase | ||
|
||
class CreateGroupActivity : AppCompatActivity() { | ||
private lateinit var mDbRef: DatabaseReference | ||
private lateinit var binding: ActivityCreateGroupBinding | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
binding = ActivityCreateGroupBinding.inflate(layoutInflater) | ||
setContentView(binding.root) | ||
|
||
supportActionBar?.hide() | ||
|
||
val groupMembers: ArrayList<String>? = intent.getStringArrayListExtra("GROUP_MEMBERS") | ||
binding.confirmButton.setOnClickListener { | ||
val groupName = binding.groupNameTextfield.text.toString().trim() | ||
var groupDescription = binding.groupDescriptionField.text.toString().trim() | ||
|
||
if (TextUtils.isEmpty(groupName)) | ||
Toast.makeText(this, "Please enter group name", Toast.LENGTH_SHORT).show() | ||
else{ | ||
if (TextUtils.isEmpty(groupDescription)) | ||
groupDescription = "Hey, Welcome to " + groupName | ||
createGroup(groupName, groupDescription, groupMembers!!) | ||
} | ||
} | ||
} | ||
private fun createGroup(groupName: String, groupDescription: String?, groupMembers: ArrayList<String>) { | ||
mDbRef = FirebaseDatabase.getInstance().reference | ||
val groupId = mDbRef.child("groups").push().key ?: "" | ||
|
||
val group = Group(groupName, groupDescription, groupMembers, groupId) | ||
|
||
mDbRef.child("groups").child(groupId).setValue(group) | ||
.addOnSuccessListener { | ||
Toast.makeText(this, "Group created successfully", Toast.LENGTH_SHORT).show() | ||
val intent = Intent(this@CreateGroupActivity, GroupChatActivity::class.java) | ||
intent.putExtra("NAME", groupName) | ||
intent.putExtra("GROUP_ID", groupId) | ||
|
||
finish() | ||
startActivity(intent) | ||
} | ||
.addOnFailureListener { exception -> | ||
Log.e("CreateGroupActivity", "Error creating group", exception) | ||
Toast.makeText(this, "Failed to create group", Toast.LENGTH_SHORT).show() | ||
val intent = Intent(this@CreateGroupActivity, Groups::class.java) | ||
finish() | ||
startActivity(intent) | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
TalkIn/app/src/main/java/com/example/talk_in/DisplayGroupAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.example.talk_in | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.TextView | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.google.firebase.database.* | ||
|
||
class DisplayGroupAdapter(val context: Context, val groupList: ArrayList<Group>) : | ||
RecyclerView.Adapter<DisplayGroupAdapter.UserViewHolder>() { | ||
private lateinit var mDbRef: DatabaseReference | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserViewHolder { | ||
mDbRef = FirebaseDatabase.getInstance().getReference() | ||
|
||
val view: View = | ||
LayoutInflater.from(context).inflate(R.layout.user_layout, parent, false) | ||
return UserViewHolder(view) | ||
} | ||
|
||
override fun onBindViewHolder(holder: UserViewHolder, position: Int) { | ||
val currentGroup = groupList[position] | ||
|
||
var name = currentGroup.groupName!!.toString().trim() | ||
if (currentGroup.groupName?.length!! > 25) | ||
name = name.substring(0, minOf(name.length, 25)) + "..." | ||
|
||
val groupDescriptionObj = mDbRef.child("groups").child(currentGroup.groupId!!).child("groupDescription") | ||
groupDescriptionObj.addListenerForSingleValueEvent(object : ValueEventListener{ | ||
override fun onDataChange(snapshot: DataSnapshot) { | ||
val groupDescriptionValue = snapshot.getValue(String::class.java) | ||
holder.groupDescription.text = groupDescriptionValue | ||
} | ||
|
||
override fun onCancelled(error: DatabaseError) { | ||
// Handle possible errors | ||
} | ||
}) | ||
|
||
|
||
holder.groupName.text = name | ||
holder.itemView.setOnClickListener { | ||
val intent = Intent(context, GroupChatActivity::class.java) | ||
intent.putExtra("NAME", currentGroup.groupName) | ||
intent.putExtra("GROUP_ID", currentGroup.groupId) | ||
context.startActivity(intent) | ||
} | ||
} | ||
|
||
override fun getItemCount(): Int { | ||
return groupList.size | ||
} | ||
|
||
class UserViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { | ||
val groupName: TextView = itemView.findViewById(R.id.txt_name) | ||
val groupDescription: TextView = itemView.findViewById(R.id.txt_last_message) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.example.talk_in | ||
|
||
class Group { | ||
var groupName: String? = null | ||
var groupDescription: String? = null | ||
var groupId: String? = null | ||
var groupMembers: ArrayList<String>? = null | ||
|
||
constructor(){} | ||
|
||
constructor(groupName: String?, groupDescription: String?, groupMembers: ArrayList<String>?, groupId: String?){ | ||
this.groupName = groupName | ||
this.groupDescription = groupDescription | ||
this.groupMembers = groupMembers | ||
this.groupId = groupId | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
TalkIn/app/src/main/java/com/example/talk_in/GroupChatActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.example.talk_in | ||
|
||
import android.content.Intent | ||
import androidx.appcompat.app.AppCompatActivity | ||
import android.os.Bundle | ||
import android.util.Log | ||
import android.view.MenuInflater | ||
import android.view.View | ||
import android.widget.ImageView | ||
import android.widget.PopupMenu | ||
import android.widget.TextView | ||
import android.widget.Toast | ||
import androidx.recyclerview.widget.RecyclerView | ||
|
||
class GroupChatActivity : AppCompatActivity() { | ||
private lateinit var groupChatRecyclerView: RecyclerView | ||
private lateinit var nameOfGroup: TextView | ||
private lateinit var backbtnImage: ImageView | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_chat) | ||
Log.d("GroupChatActivity", "onCreate() method called") | ||
|
||
val name = intent.getStringExtra("NAME") | ||
val groupId = intent.getStringExtra("GROUP_ID") | ||
supportActionBar?.hide() | ||
|
||
groupChatRecyclerView = findViewById(R.id.chatRecyclerView) | ||
nameOfGroup = findViewById(R.id.nameOfUser) | ||
backbtnImage = findViewById(R.id.backbtnImage) | ||
|
||
nameOfGroup.text = name | ||
|
||
backbtnImage.setOnClickListener { | ||
val intent = Intent(this@GroupChatActivity, Groups::class.java) | ||
finish() | ||
startActivity(intent) | ||
} | ||
|
||
val popupMenuBtn: ImageView = findViewById(R.id.popupMenuBtn) | ||
popupMenuBtn.setOnClickListener { view -> | ||
showPopupMenu(view) | ||
} | ||
} | ||
|
||
private fun showPopupMenu(view: View) { | ||
val popupMenu = PopupMenu(this, view) | ||
val inflater: MenuInflater = popupMenu.menuInflater | ||
inflater.inflate(R.menu.user_chat_menu, popupMenu.menu) | ||
popupMenu.setOnMenuItemClickListener { menuItem -> | ||
when (menuItem.itemId) { | ||
R.id.viewProfile -> { | ||
Toast.makeText(this, "View Profile Clicked", Toast.LENGTH_SHORT).show() | ||
true | ||
} | ||
R.id.sharedMedia -> { | ||
Toast.makeText(this, "Shared Media Clicked", Toast.LENGTH_SHORT).show() | ||
true | ||
} | ||
R.id.search -> { | ||
Toast.makeText(this, "Search Clicked", Toast.LENGTH_SHORT).show() | ||
true | ||
} | ||
R.id.locateNow -> { | ||
Toast.makeText(this, "Locate Now Clicked", Toast.LENGTH_SHORT).show() | ||
true | ||
} | ||
else -> false | ||
} | ||
} | ||
popupMenu.show() | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
TalkIn/app/src/main/java/com/example/talk_in/GroupMemberSelection.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.example.talk_in | ||
|
||
import GroupMemberSelectionAdapter | ||
import android.content.Intent | ||
import android.os.Bundle | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.recyclerview.widget.LinearLayoutManager | ||
import com.example.talk_in.databinding.ActivityGroupMemberSelectionBinding | ||
import com.google.firebase.auth.FirebaseAuth | ||
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 | ||
|
||
class GroupMemberSelection : AppCompatActivity() { | ||
private lateinit var binding: ActivityGroupMemberSelectionBinding | ||
private lateinit var userList: ArrayList<User> | ||
private lateinit var adapter: GroupMemberSelectionAdapter | ||
private lateinit var mAuth: FirebaseAuth | ||
private lateinit var mDbRef: DatabaseReference | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
binding = ActivityGroupMemberSelectionBinding.inflate(layoutInflater) | ||
setContentView(binding.root) | ||
|
||
supportActionBar?.hide() | ||
|
||
mAuth = FirebaseAuth.getInstance() | ||
mDbRef = FirebaseDatabase.getInstance().getReference() | ||
|
||
userList = ArrayList() | ||
adapter = GroupMemberSelectionAdapter(this, userList) | ||
|
||
binding.userRecyclerView.layoutManager = LinearLayoutManager(this) | ||
binding.userRecyclerView.adapter = adapter | ||
|
||
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) | ||
if (currentUser != null && mAuth.currentUser?.uid != currentUser.uid && currentUser.verified == true) { | ||
userList.add(currentUser) | ||
} | ||
} | ||
adapter.notifyDataSetChanged() | ||
} | ||
|
||
override fun onCancelled(error: DatabaseError) { | ||
// Handle possible errors. | ||
} | ||
}) | ||
|
||
binding.confirmButton.setOnClickListener { | ||
// Get the list of selected users from the adapter | ||
val selectedUsers = adapter.getSelectedUsers() | ||
val uidOfUsers = ArrayList<String>() | ||
for (user in selectedUsers){ | ||
uidOfUsers.add(user.uid.toString()) | ||
} | ||
val intent = Intent(this@GroupMemberSelection, CreateGroupActivity::class.java) | ||
intent.putStringArrayListExtra("GROUP_MEMBERS", uidOfUsers) | ||
finish() | ||
startActivity(intent) | ||
} | ||
} | ||
} |
Oops, something went wrong.