Skip to content

Commit

Permalink
WIP. add viewModel to ChatActivity
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
  • Loading branch information
mahibi committed Jun 22, 2023
1 parent 9d66e8f commit dc81aa0
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 1 deletion.
34 changes: 34 additions & 0 deletions app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ import androidx.core.text.bold
import androidx.core.widget.doAfterTextChanged
import androidx.emoji2.text.EmojiCompat
import androidx.emoji2.widget.EmojiTextView
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
Expand Down Expand Up @@ -128,6 +129,7 @@ import com.nextcloud.talk.adapters.messages.VoiceMessageInterface
import com.nextcloud.talk.api.NcApi
import com.nextcloud.talk.application.NextcloudTalkApplication
import com.nextcloud.talk.callbacks.MentionAutocompleteCallback
import com.nextcloud.talk.chat.viewmodels.ChatViewModel
import com.nextcloud.talk.conversationinfo.ConversationInfoActivity
import com.nextcloud.talk.conversationlist.ConversationsListActivity
import com.nextcloud.talk.data.user.model.User
Expand Down Expand Up @@ -256,6 +258,11 @@ class ChatActivity :
@Inject
lateinit var dateUtils: DateUtils

@Inject
lateinit var viewModelFactory: ViewModelProvider.Factory

lateinit var chatViewModel: ChatViewModel

override val view: View
get() = binding.root

Expand Down Expand Up @@ -386,12 +393,17 @@ class ChatActivity :

handleIntent(intent)

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

binding.progressBar.visibility = View.VISIBLE

initAdapter()
binding.messagesListView.setAdapter(adapter)

onBackPressedDispatcher.addCallback(this, onBackPressedCallback)

initObservers()
}

override fun onNewIntent(intent: Intent) {
Expand Down Expand Up @@ -455,6 +467,28 @@ class ChatActivity :
active = false
}

private fun initObservers() {
chatViewModel.viewState.observe(this) { state ->
when (state) {
is ChatViewModel.FetchConversationStartState -> {
Toast.makeText(context, "fetch room...", Toast.LENGTH_LONG).show()
}
is ChatViewModel.FetchConversationSuccessState -> {
Toast.makeText(
context,
"fetched room " +
state.conversationModel.displayName,
Toast.LENGTH_LONG
).show()
}
is ChatViewModel.FetchConversationErrorState -> {
Toast.makeText(context, R.string.nc_common_error_sorry, Toast.LENGTH_LONG).show()
}
else -> {}
}
}
}

@Suppress("Detekt.TooGenericExceptionCaught")
override fun onResume() {
super.onResume()
Expand Down
29 changes: 29 additions & 0 deletions app/src/main/java/com/nextcloud/talk/chat/data/ChatRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Nextcloud Talk application
*
* @author Marcel Hibbe
* Copyright (C) 2023 Marcel Hibbe <dev@mhibbe.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.nextcloud.talk.chat.data

import com.nextcloud.talk.data.user.model.User
import io.reactivex.Observable

interface ChatRepository {

fun fetchConversation(user: User, token: String): Observable<ConversationModel>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Nextcloud Talk application
*
* @author Marcel Hibbe
* Copyright (C) 2023 Marcel Hibbe <dev@mhibbe.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.nextcloud.talk.chat.data

import com.nextcloud.talk.api.NcApi
import com.nextcloud.talk.data.user.model.User
import com.nextcloud.talk.models.json.conversations.Conversation
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 fetchConversation(
user: User,
token: String
): Observable<ConversationModel> {
val credentials: String = ApiUtils.getCredentials(user.username, user.token)
val apiVersion = ApiUtils.getConversationApiVersion(user, intArrayOf(ApiUtils.APIv4, ApiUtils.APIv3, 1))

return ncApi.getRoom(
credentials,
ApiUtils.getUrlForRoom(apiVersion, user.baseUrl, token)
).map { mapToConversationModel(it.ocs?.data!!) }
}

private fun mapToConversationModel(
conversation: Conversation
): ConversationModel {
return ConversationModel(
conversation.roomId!!,
conversation.token!!,
conversation.name!!
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Nextcloud Talk application
*
* @author Marcel Hibbe
* Copyright (C) 2023 Marcel Hibbe <dev@mhibbe.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.nextcloud.talk.chat.data

data class ConversationModel(
var roomId: String,
var roomToken: String,
var displayName: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Nextcloud Talk application
*
* @author Marcel Hibbe
* Copyright (C) 2023 Marcel Hibbe <dev@mhibbe.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.nextcloud.talk.chat.viewmodels

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.nextcloud.talk.chat.data.ChatRepository
import com.nextcloud.talk.chat.data.ConversationModel
import com.nextcloud.talk.data.user.model.User
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import javax.inject.Inject

class ChatViewModel @Inject constructor(private val repository: ChatRepository) :
ViewModel() {

sealed interface ViewState

object FetchConversationStartState : ViewState
object FetchConversationErrorState : ViewState
open class FetchConversationSuccessState(val conversationModel: ConversationModel) : ViewState

private val _viewState: MutableLiveData<ViewState> = MutableLiveData(FetchConversationStartState)
val viewState: LiveData<ViewState>
get() = _viewState

fun fetchConversation(user: User, token: String) {
_viewState.value = FetchConversationStartState
repository.fetchConversation(user, token)
.subscribeOn(Schedulers.io())
?.observeOn(AndroidSchedulers.mainThread())
?.subscribe(RoomObserver())
}

inner class RoomObserver : Observer<ConversationModel> {
override fun onSubscribe(d: Disposable) {
// unused atm
}

override fun onNext(conversationModel: ConversationModel) {
_viewState.value = FetchConversationSuccessState(conversationModel)
}

override fun onError(e: Throwable) {
Log.e(TAG, "Error when fetching room")
_viewState.value = FetchConversationErrorState
}

override fun onComplete() {
// unused atm
}
}

companion object {
private val TAG = ChatViewModel::class.simpleName
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
package com.nextcloud.talk.dagger.modules

import com.nextcloud.talk.api.NcApi
import com.nextcloud.talk.chat.data.ChatRepository
import com.nextcloud.talk.chat.data.ChatRepositoryImpl
import com.nextcloud.talk.data.source.local.TalkDatabase
import com.nextcloud.talk.data.storage.ArbitraryStoragesRepository
import com.nextcloud.talk.data.storage.ArbitraryStoragesRepositoryImpl
Expand Down Expand Up @@ -123,4 +125,10 @@ class RepositoryModule {
TranslateRepository {
return TranslateRepositoryImpl(ncApi)
}

@Provides
fun provideChatRepository(ncApi: NcApi):
ChatRepository {
return ChatRepositoryImpl(ncApi)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package com.nextcloud.talk.dagger.modules

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.nextcloud.talk.chat.viewmodels.ChatViewModel
import com.nextcloud.talk.messagesearch.MessageSearchViewModel
import com.nextcloud.talk.openconversations.viewmodels.OpenConversationsViewModel
import com.nextcloud.talk.polls.viewmodels.PollCreateViewModel
Expand Down Expand Up @@ -112,5 +113,10 @@ abstract class ViewModelModule {
@Binds
@IntoMap
@ViewModelKey(OpenConversationsViewModel::class)
abstract fun openConversationsViewModelModel(viewModel: OpenConversationsViewModel): ViewModel
abstract fun openConversationsViewModel(viewModel: OpenConversationsViewModel): ViewModel

@Binds
@IntoMap
@ViewModelKey(ChatViewModel::class)
abstract fun chatViewModel(viewModel: ChatViewModel): ViewModel
}

0 comments on commit dc81aa0

Please sign in to comment.