Skip to content

Commit

Permalink
fix: handle navigating chat from tutor
Browse files Browse the repository at this point in the history
  • Loading branch information
Veirt committed Dec 12, 2024
1 parent 67b956e commit 539e12b
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 14 deletions.
70 changes: 63 additions & 7 deletions app/src/main/java/com/tutortoise/tutortoise/domain/ChatManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,81 @@ object ChatManager {
}
}

suspend fun findOrCreateChatRoom(context: Context, tutorId: String): Result<ChatRoom> =
fun navigateToChatFromTutor(context: Context, learnerId: String, learnerName: String? = null) {
scope.launch {
try {
Log.d(TAG, "Starting navigation to chat with learner: $learnerId")
val tutorId = AuthManager.getCurrentUserId() ?: run {
Toast.makeText(context, "User not authenticated", Toast.LENGTH_SHORT).show()
return@launch
}

// Check for existing room first
val chatRepository = ChatRepository(context)
val roomsResult = chatRepository.getRooms()

roomsResult.fold(
onSuccess = { rooms ->
// Try to find existing room
val existingRoom = rooms.find { it.learnerId == learnerId }

val intent = Intent(context, ChatRoomActivity::class.java).apply {
if (existingRoom != null) {
// If room exists, pass all room info
putExtra("ROOM_ID", existingRoom.id)
putExtra("LEARNER_ID", existingRoom.learnerId)
putExtra("TUTOR_ID", existingRoom.tutorId)
putExtra("LEARNER_NAME", existingRoom.learnerName)
putExtra("TUTOR_NAME", existingRoom.tutorName)
} else {
// For new room, pass what we know
putExtra("LEARNER_ID", learnerId)
putExtra("TUTOR_ID", tutorId)
putExtra("LEARNER_NAME", learnerName)
}
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
context.startActivity(intent)
},
onFailure = { error ->
// If we fail to get rooms, still allow navigation but with basic info
val intent = Intent(context, ChatRoomActivity::class.java).apply {
putExtra("LEARNER_ID", learnerId)
putExtra("TUTOR_ID", tutorId)
putExtra("LEARNER_NAME", learnerName)
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
context.startActivity(intent)
Log.e(TAG, "Failed to check existing rooms", error)
}
)
} catch (e: Exception) {
Log.e(TAG, "Exception in navigateToChat", e)
Toast.makeText(context, "Failed to start chat", Toast.LENGTH_SHORT).show()
}
}
}

suspend fun findOrCreateChatRoom(
context: Context,
learnerId: String,
tutorId: String
): Result<ChatRoom> =
withContext(Dispatchers.IO) {
try {
val chatRepository = ChatRepository(context)
val learnerId = AuthManager.getCurrentUserId()
if (learnerId == null) {
Log.e(TAG, "User not authenticated")
return@withContext Result.failure(Exception("User not authenticated"))
}

// First try to find existing room
val roomsResult = chatRepository.getRooms()
roomsResult.fold(
onSuccess = { rooms ->
Log.d(TAG, "Found ${rooms.size} chat rooms")
rooms.find { it.tutorId == tutorId }?.let {

rooms.find { it.learnerId == learnerId && it.tutorId == tutorId }?.let {
Log.d(TAG, "Found existing chat room: ${it.id}")
return@withContext Result.success(it)
}

},
onFailure = {
Log.e(TAG, "Failed to get rooms", it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class ChatViewModel(private val context: Context) : ViewModel() {
return try {
_isLoading.value = true
val result = withContext(Dispatchers.IO) {
ChatManager.findOrCreateChatRoom(context, tutorId)
ChatManager.findOrCreateChatRoom(context, learnerId, tutorId)
}

result.fold(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ class PendingOrdersAdapter(
.into(ivProfilePicture)

btnChat.setOnClickListener {
ChatManager.navigateToChat(
ChatManager.navigateToChatFromTutor(
context = root.context,
tutorId = order.tutorId,
tutorName = order.tutorName
learnerId = order.learnerId,
learnerName = order.learnerName
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ class ScheduledOrdersAdapter(
.into(ivProfilePicture)

btnChat.setOnClickListener {
ChatManager.navigateToChat(
ChatManager.navigateToChatFromTutor(
context = root.context,
tutorId = order.tutorId,
tutorName = order.tutorName
learnerId = order.learnerId,
learnerName = order.learnerName
)
}

Expand Down

0 comments on commit 539e12b

Please sign in to comment.