Skip to content

Commit

Permalink
WIP. add MVVM's & load stuff from repos to avoid passing too big bund…
Browse files Browse the repository at this point in the history
…les.

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
  • Loading branch information
mahibi committed Jun 26, 2023
1 parent 49ebadc commit 7b39ea8
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 150 deletions.
138 changes: 7 additions & 131 deletions app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ import com.nextcloud.talk.api.NcApi
import com.nextcloud.talk.application.NextcloudTalkApplication
import com.nextcloud.talk.callbacks.MentionAutocompleteCallback
import com.nextcloud.talk.chat.data.ChatRepository
import com.nextcloud.talk.chat.data.ChatRepositoryImpl
import com.nextcloud.talk.chat.viewmodels.ChatViewModel
import com.nextcloud.talk.conversationinfo.ConversationInfoActivity
import com.nextcloud.talk.conversationlist.ConversationsListActivity
Expand Down Expand Up @@ -395,7 +396,6 @@ class ChatActivity :
handleIntent(intent)

chatViewModel = ViewModelProvider(this, viewModelFactory)[ChatViewModel::class.java]
// chatViewModel.getRoom(conversationUser!!, roomToken)

binding.progressBar.visibility = View.VISIBLE

Expand Down Expand Up @@ -436,11 +436,6 @@ class ChatActivity :
Log.d(TAG, " roomToken was null or empty!")
}

// if (intent.hasExtra(KEY_ACTIVE_CONVERSATION)) {
// currentConversation = Parcels.unwrap<Conversation>(extras?.getParcelable(KEY_ACTIVE_CONVERSATION))
// participantPermissions = ParticipantPermissions(conversationUser!!, currentConversation!!)
// }

roomPassword = extras?.getString(BundleKeys.KEY_CONVERSATION_PASSWORD).orEmpty()

credentials = if (conversationUser?.userId == "?") {
Expand Down Expand Up @@ -594,7 +589,6 @@ class ChatActivity :

cancelNotificationsForCurrentConversation()

// getRoomInfo() // remove
chatViewModel.getRoom(conversationUser!!, roomToken)

actionBar?.show()
Expand Down Expand Up @@ -1186,68 +1180,6 @@ class ChatActivity :
!CapabilitiesUtilNew.isTypingStatusPrivate(conversationUser!!)
}

// private fun getRoomInfo() {
// logConversationInfos("getRoomInfo")
//
// conversationUser?.let {
// val apiVersion = ApiUtils.getConversationApiVersion(conversationUser, intArrayOf(ApiUtils.APIv4, 1))
//
// val startNanoTime = System.nanoTime()
// Log.d(TAG, "getRoomInfo - getRoom - calling: $startNanoTime")
// ncApi.getRoom(credentials, ApiUtils.getUrlForRoom(apiVersion, it.baseUrl, roomToken))
// ?.subscribeOn(Schedulers.io())
// ?.observeOn(AndroidSchedulers.mainThread())
// ?.subscribe(object : Observer<RoomOverall> {
// override fun onSubscribe(d: Disposable) {
// disposables.add(d)
// }
//
// @Suppress("Detekt.TooGenericExceptionCaught")
// override fun onNext(roomOverall: RoomOverall) {
// Log.d(TAG, "getRoomInfo - getRoom - got response: $startNanoTime")
// currentConversation = roomOverall.ocs!!.data
//
// logConversationInfos("getRoomInfo#onNext")
//
// loadAvatarForStatusBar()
// setActionBarTitle()
// participantPermissions = ParticipantPermissions(it, currentConversation!!)
//
// setupSwipeToReply()
// setupMentionAutocomplete()
// checkShowCallButtons()
// checkShowMessageInputView()
// checkLobbyState()
//
// if (!validSessionId()) {
// joinRoomWithPassword()
// } else {
// Log.d(TAG, "already inConversation. joinRoomWithPassword is skipped")
// }
// }
//
// override fun onError(e: Throwable) {
// Log.e(TAG, "getRoomInfo - getRoom - ERROR", e)
// }
//
// override fun onComplete() {
// Log.d(TAG, "getRoomInfo - getRoom - onComplete: $startNanoTime")
//
// val delayForRecursiveCall = if (shouldShowLobby()) {
// GET_ROOM_INFO_DELAY_LOBBY
// } else {
// GET_ROOM_INFO_DELAY_NORMAL
// }
//
// if (getRoomInfoTimerHandler == null) {
// getRoomInfoTimerHandler = Handler()
// }
// getRoomInfoTimerHandler?.postDelayed({ getRoomInfo() }, delayForRecursiveCall)
// }
// })
// }
// }

private fun setupSwipeToReply() {
if (this::participantPermissions.isInitialized &&
participantPermissions.hasChatPermission() &&
Expand Down Expand Up @@ -2346,66 +2278,6 @@ class ChatActivity :
Log.d(TAG, "joinRoomWithPassword - joinRoom - calling: $startNanoTime")

chatViewModel.joinRoom(conversationUser!!, roomToken, roomPassword)

// ncApi.joinRoom(
// credentials,
// ApiUtils.getUrlForParticipantsActive(apiVersion, conversationUser?.baseUrl, roomToken),
// roomPassword
// )
// ?.subscribeOn(Schedulers.io())
// ?.observeOn(AndroidSchedulers.mainThread())
// ?.retry(RETRIES)
// ?.subscribe(object : Observer<RoomOverall> {
// override fun onSubscribe(d: Disposable) {
// disposables.add(d)
// }
//
// @Suppress("Detekt.TooGenericExceptionCaught")
// override fun onNext(roomOverall: RoomOverall) {
// Log.d(TAG, "joinRoomWithPassword - joinRoom - got response: $startNanoTime")
//
// val conversation = roomOverall.ocs!!.data!!
// currentConversation = conversation
//
// sessionIdAfterRoomJoined = conversation.sessionId
// ApplicationWideCurrentRoomHolder.getInstance().session = conversation.sessionId
// ApplicationWideCurrentRoomHolder.getInstance().currentRoomId = conversation.roomId
// ApplicationWideCurrentRoomHolder.getInstance().currentRoomToken = conversation.token
// ApplicationWideCurrentRoomHolder.getInstance().userInRoom = conversationUser
//
// logConversationInfos("joinRoomWithPassword#onNext")
//
// if (isFirstMessagesProcessing) {
// pullChatMessages(false)
// } else {
// pullChatMessages(true, false)
// }
//
// if (webSocketInstance != null) {
// webSocketInstance?.joinRoomWithRoomTokenAndSession(
// roomToken!!,
// sessionIdAfterRoomJoined
// )
// }
// if (startCallFromNotification != null && startCallFromNotification ?: false) {
// startCallFromNotification = false
// startACall(voiceOnly, false)
// }
//
// if (startCallFromRoomSwitch) {
// startCallFromRoomSwitch = false
// startACall(voiceOnly, true)
// }
// }
//
// override fun onError(e: Throwable) {
// Log.e(TAG, "joinRoomWithPassword - joinRoom - ERROR", e)
// }
//
// override fun onComplete() {
// // unused atm
// }
// })
} else {
Log.d(TAG, "sessionID was valid -> skip joinRoom")

Expand Down Expand Up @@ -3753,8 +3625,12 @@ class ChatActivity :

// TODO: change return type of createRoom to ConversationModel.
// TODO: move createRoom to ChatRepository
// bundle.putBoolean(KEY_IS_MODERATOR,
// ConversationUtils.isParticipantOwnerOrModerator(roomOverall.ocs!!.data!!))
bundle.putBoolean(
KEY_IS_MODERATOR,
ConversationUtils.isParticipantOwnerOrModerator(
ChatRepositoryImpl.mapToConversationModel(roomOverall.ocs!!.data!!)
)
)

if (conversationUser != null) {
bundle.putParcelable(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ import com.nextcloud.talk.utils.ApiUtils
import io.reactivex.Observable

class ChatRepositoryImpl(private val ncApi: NcApi) : ChatRepository {

// val currentUser: User = currentUserProvider.currentUser.blockingGet()
// val credentials: String = ApiUtils.getCredentials(currentUser.username, currentUser.token)

override fun getRoom(
user: User,
roomToken: String
Expand Down Expand Up @@ -60,6 +56,9 @@ class ChatRepositoryImpl(private val ncApi: NcApi) : ChatRepository {
}

companion object {

// TODO: only use this method inside ChatRepositoryImpl. For now, it might be temporarily used outside for
// conversions.
fun mapToConversationModel(
conversation: Conversation
): ChatRepository.ConversationModel {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Parcel
import android.text.InputType
import android.text.TextUtils
import android.util.Log
Expand Down Expand Up @@ -1192,23 +1191,13 @@ class ConversationsListActivity :
selectedMessageId = null
}

Log.d(TAG, "bundle size: " + getBundleSizeInBytes(bundle))

val intent = Intent(context, ChatActivity::class.java)
intent.putExtras(bundle)
startActivity(intent)

clearIntentAction()
}

fun getBundleSizeInBytes(bundle: Bundle?): Int {
val parcel: Parcel = Parcel.obtain()
parcel.writeBundle(bundle)
val size: Int = parcel.dataSize()
parcel.recycle()
return size
}

@Subscribe(sticky = true, threadMode = ThreadMode.BACKGROUND)
fun onMessageEvent(eventStatus: EventStatus) {
if (currentUser != null && eventStatus.userId == currentUser!!.id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ abstract class TalkDatabase : RoomDatabase() {

return Room
.databaseBuilder(context.applicationContext, TalkDatabase::class.java, dbName)
// .openHelperFactory(factory)
// comment out openHelperFactory to view the database entries in Android Studio for debugging
.openHelperFactory(factory)
.addMigrations(Migrations.MIGRATION_6_8, Migrations.MIGRATION_7_8)
.allowMainThreadQueries()
.addCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,7 @@ class ListOpenConversationsActivity : BaseActivity() {
}

private fun adapterOnClick(conversation: OpenConversation) {
val user = userProvider.currentUser.blockingGet()

val bundle = Bundle()
bundle.putParcelable(BundleKeys.KEY_USER_ENTITY, user)
bundle.putString(BundleKeys.KEY_ROOM_TOKEN, conversation.roomToken)

val chatIntent = Intent(context, ChatActivity::class.java)
Expand Down

0 comments on commit 7b39ea8

Please sign in to comment.