-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from WonderWoman-Team/feature/11-chatting-ui
Feat : #11 채팅 UI 만들기
- Loading branch information
Showing
31 changed files
with
757 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 @@ | ||
{ | ||
"project_info": { | ||
"project_number": "491831579256", | ||
"firebase_url": "https://wonderwoman-eeff2-default-rtdb.firebaseio.com", | ||
"project_id": "wonderwoman-eeff2", | ||
"storage_bucket": "wonderwoman-eeff2.appspot.com" | ||
}, | ||
"client": [ | ||
{ | ||
"client_info": { | ||
"mobilesdk_app_id": "1:491831579256:android:ce8f3851848cedfc9e538d", | ||
"android_client_info": { | ||
"package_name": "com.example.wonderwoman" | ||
} | ||
}, | ||
"oauth_client": [ | ||
{ | ||
"client_id": "491831579256-og7o8s2d5ejtmkdgujsevqpau9ovpos7.apps.googleusercontent.com", | ||
"client_type": 3 | ||
} | ||
], | ||
"api_key": [ | ||
{ | ||
"current_key": "AIzaSyDaZ4_qjv-AYez5zqGqTMVkVAelJvGpgd4" | ||
} | ||
], | ||
"services": { | ||
"appinvite_service": { | ||
"other_platform_oauth_client": [ | ||
{ | ||
"client_id": "491831579256-og7o8s2d5ejtmkdgujsevqpau9ovpos7.apps.googleusercontent.com", | ||
"client_type": 3 | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
{ | ||
"client_info": { | ||
"mobilesdk_app_id": "1:491831579256:android:b73ae3733bdc9d449e538d", | ||
"android_client_info": { | ||
"package_name": "com.soyaa.wonderwoman" | ||
} | ||
}, | ||
"oauth_client": [ | ||
{ | ||
"client_id": "491831579256-og7o8s2d5ejtmkdgujsevqpau9ovpos7.apps.googleusercontent.com", | ||
"client_type": 3 | ||
} | ||
], | ||
"api_key": [ | ||
{ | ||
"current_key": "AIzaSyDaZ4_qjv-AYez5zqGqTMVkVAelJvGpgd4" | ||
} | ||
], | ||
"services": { | ||
"appinvite_service": { | ||
"other_platform_oauth_client": [ | ||
{ | ||
"client_id": "491831579256-og7o8s2d5ejtmkdgujsevqpau9ovpos7.apps.googleusercontent.com", | ||
"client_type": 3 | ||
} | ||
] | ||
} | ||
} | ||
} | ||
], | ||
"configuration_version": "1" | ||
} |
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
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,95 @@ | ||
package com.example.wonderwoman | ||
|
||
|
||
import android.os.Bundle | ||
import android.os.Binder | ||
import android.os.PersistableBundle | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.recyclerview.widget.LinearLayoutManager | ||
import com.example.wonderwoman.databinding.ActivityChatBinding | ||
import com.google.firebase.auth.FirebaseAuth | ||
import com.google.firebase.database.* | ||
|
||
class ChatActivity : AppCompatActivity() { | ||
private lateinit var receiverName: String | ||
private lateinit var receiverUid: String | ||
|
||
//바인딩 객체 | ||
private lateinit var binding: ActivityChatBinding | ||
|
||
lateinit var mAuth: FirebaseAuth | ||
lateinit var mDbRef: DatabaseReference | ||
|
||
private lateinit var receiverRoom:String//받는 대화방 | ||
private lateinit var senderRoom: String//보내는 대화방 | ||
|
||
private lateinit var messageList: ArrayList<Message> | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
binding= ActivityChatBinding.inflate(layoutInflater) | ||
setContentView(binding.root) | ||
|
||
//초기화 | ||
messageList=ArrayList() | ||
val messageAdapter: MessageAdapter = MessageAdapter(this, messageList) | ||
|
||
//RecyclerView | ||
binding.chatRecyclerview.layoutManager = LinearLayoutManager(this) | ||
binding.chatRecyclerview.adapter=messageAdapter | ||
|
||
//넘어온 데이터 변수에 담기 | ||
receiverName=intent.getStringExtra("name").toString() | ||
receiverUid=intent.getStringExtra("uId").toString() | ||
|
||
mAuth=FirebaseAuth.getInstance() | ||
mDbRef=FirebaseDatabase.getInstance().reference | ||
|
||
//접속자 Uid | ||
val senderUid=mAuth.currentUser?.uid | ||
|
||
//보낸이 방 | ||
senderRoom=receiverUid+senderUid | ||
|
||
//받는이 방 | ||
receiverRoom=senderUid+receiverUid | ||
|
||
//액션바에 상대방 이름 보여주기 | ||
supportActionBar?.title=receiverName | ||
|
||
//메세지전송 | ||
binding.sendBtn.setOnClickListener{ | ||
val message = binding.messageEdit.text.toString() | ||
val messageObject=Message(message,senderUid) | ||
|
||
//데이터 저장 | ||
mDbRef.child("chats").child(senderRoom).child("messages").push() | ||
.setValue(messageObject).addOnSuccessListener { | ||
//저장 성공하면 | ||
mDbRef.child("chats").child(receiverRoom).child("messages").push() | ||
.setValue(messageObject) | ||
} | ||
//입력 부분 초기화 | ||
binding.messageEdit.setText("") | ||
} | ||
//메세지 가져오기 | ||
mDbRef.child("chats").child(senderRoom).child("messages") | ||
.addValueEventListener(object : ValueEventListener{ | ||
override fun onDataChange(snapshot: DataSnapshot) { | ||
messageList.clear() | ||
|
||
for(postSnapshat in snapshot.children){ | ||
val message=postSnapshat.getValue(Message::class.java) | ||
messageList.add(message!!) | ||
} | ||
//적용 화면에 보이게 하기 | ||
messageAdapter.notifyDataSetChanged() | ||
} | ||
|
||
override fun onCancelled(error: DatabaseError) { | ||
|
||
} | ||
|
||
}) | ||
} | ||
} |
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,79 @@ | ||
package com.example.wonderwoman | ||
|
||
import android.content.Intent | ||
import android.os.Bundle | ||
import android.widget.Toast | ||
import androidx.appcompat.app.AppCompatActivity | ||
import com.example.wonderwoman.databinding.ActivityLoginBinding | ||
import com.google.firebase.auth.FirebaseAuth | ||
import com.google.firebase.auth.ktx.auth | ||
import com.google.firebase.database.DatabaseReference | ||
import com.google.firebase.database.ktx.database | ||
import com.google.firebase.ktx.Firebase | ||
|
||
class Login : AppCompatActivity() { | ||
lateinit var mAuth: FirebaseAuth | ||
|
||
private lateinit var mDbRef: DatabaseReference | ||
|
||
val name = "somin333" | ||
val email = "[email protected]" | ||
val password = "somin455" | ||
|
||
private lateinit var binding: ActivityLoginBinding | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
binding= ActivityLoginBinding.inflate(layoutInflater) | ||
val view=binding.root | ||
setContentView(view) | ||
|
||
//인증초기화 | ||
mAuth = Firebase.auth | ||
|
||
//db초기화 | ||
mDbRef = Firebase.database.reference | ||
|
||
binding.button.setOnClickListener{ | ||
signup(name,email,password) | ||
} | ||
binding.button2.setOnClickListener{ | ||
login(email, password) | ||
} | ||
|
||
} | ||
//로그인 | ||
|
||
private fun login(email: String,password:String){ | ||
mAuth.signInWithEmailAndPassword(email, password) | ||
.addOnCompleteListener(this) { task -> | ||
if (task.isSuccessful) { | ||
val intent2: Intent = Intent(this,UserList::class.java) | ||
startActivity(intent2) | ||
finish() | ||
Toast.makeText(this,"로그인성공", Toast.LENGTH_SHORT).show() | ||
} else { | ||
Toast.makeText(this,"로그인실패", Toast.LENGTH_SHORT).show() | ||
} | ||
} | ||
} | ||
|
||
//회원가입 | ||
private fun signup(name: String, email: String,password: String) { | ||
mAuth.createUserWithEmailAndPassword(email, password) | ||
.addOnCompleteListener(this) { task -> | ||
if (task.isSuccessful) { | ||
val intent: Intent = Intent(this, Login::class.java) | ||
startActivity(intent) | ||
addUserToDatabase(name, email, mAuth.currentUser?.uid!!) | ||
Toast.makeText(this,"회원가입성공", Toast.LENGTH_SHORT).show() | ||
} else { | ||
Toast.makeText(this,"회원가입실패",Toast.LENGTH_SHORT).show() | ||
} | ||
} | ||
} | ||
private fun addUserToDatabase(name: String,email: String,uId: String){ | ||
mDbRef.child("user").child(uId).setValue(User(name, email, uId)) | ||
Toast.makeText(this,"db성공", Toast.LENGTH_SHORT).show() | ||
} | ||
} |
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,8 @@ | ||
package com.example.wonderwoman | ||
|
||
data class Message( | ||
var message: String?, | ||
var sendId: String? | ||
){ | ||
constructor():this("","") | ||
} |
66 changes: 66 additions & 0 deletions
66
app/src/main/java/com/example/wonderwoman/MessageAdapter.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,66 @@ | ||
package com.example.wonderwoman | ||
|
||
import android.content.Context | ||
import android.os.Message | ||
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.auth.FirebaseAuth | ||
|
||
class MessageAdapter(private val context: Context, private val messageList: ArrayList<com.example.wonderwoman.Message>): | ||
RecyclerView.Adapter<RecyclerView.ViewHolder>(){ | ||
|
||
private val receive=1 | ||
private val send=2 | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { | ||
return if(viewType==1){//받는 화면 | ||
val view: View=LayoutInflater.from(context).inflate(R.layout.activity_receive,parent,false) | ||
ReceiveViewHolder(view) | ||
}else{//보내는 화면 | ||
val view: View=LayoutInflater.from(context).inflate(R.layout.activity_send,parent,false) | ||
SendViewHolder(view) | ||
} | ||
} | ||
|
||
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { | ||
//현재 메세지 | ||
val currentMessage = messageList[position] | ||
|
||
//보내는 데이터 | ||
if(holder.javaClass==SendViewHolder::class.java){ | ||
val viewHolder = holder as SendViewHolder | ||
viewHolder.sendMessage.text=currentMessage.message | ||
}else{//받는 데이터 | ||
val viewHolder = holder as ReceiveViewHolder | ||
viewHolder.receiveMessage.text=currentMessage.message | ||
|
||
} | ||
} | ||
override fun getItemCount(): Int { | ||
return messageList.size | ||
} | ||
|
||
override fun getItemViewType(position: Int): Int { | ||
//메세지 값 | ||
val currentMessage = messageList[position] | ||
|
||
return if(FirebaseAuth.getInstance().currentUser?.uid.equals(currentMessage.sendId)){ | ||
send | ||
}else{ | ||
receive | ||
} | ||
} | ||
|
||
|
||
//보낸쪽 | ||
class SendViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){ | ||
val sendMessage: TextView= itemView.findViewById(R.id.send_message_text) | ||
} | ||
//받는쪽 | ||
class ReceiveViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){ | ||
val receiveMessage: TextView=itemView.findViewById(R.id.receive_message_text) | ||
} | ||
} |
Oops, something went wrong.