Skip to content
This repository has been archived by the owner on Jun 7, 2020. It is now read-only.

Commit

Permalink
Add Invite Users feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Shailesh351 committed May 30, 2019
1 parent 3b97275 commit 34d3de7
Show file tree
Hide file tree
Showing 13 changed files with 604 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ sealed class ScreenViewEvent(val screenName: String) {
object FavoriteMessages : ScreenViewEvent("FavoriteMessagesFragment")
object Files : ScreenViewEvent("FilesFragment")
object Members : ScreenViewEvent("MembersFragment")
object InviteUsers : ScreenViewEvent("InviteUsersFragment")
object Mentions : ScreenViewEvent("MentionsFragment")
object MessageInfo : ScreenViewEvent("MessageInfoFragment")
object Password : ScreenViewEvent("PasswordFragment")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import chat.rocket.android.chatroom.ui.ChatRoomActivity
import chat.rocket.android.chatroom.ui.chatRoomIntent
import chat.rocket.android.favoritemessages.ui.TAG_FAVORITE_MESSAGES_FRAGMENT
import chat.rocket.android.files.ui.TAG_FILES_FRAGMENT
import chat.rocket.android.inviteusers.ui.TAG_INVITE_USERS_FRAGMENT
import chat.rocket.android.members.ui.TAG_MEMBERS_FRAGMENT
import chat.rocket.android.mentions.ui.TAG_MENTIONS_FRAGMENT
import chat.rocket.android.pinnedmessages.ui.TAG_PINNED_MESSAGES_FRAGMENT
Expand Down Expand Up @@ -87,6 +88,12 @@ class ChatRoomNavigator(internal val activity: ChatRoomActivity) {
}
}

fun toInviteUsers(chatRoomId: String) {
activity.addFragmentBackStack(TAG_INVITE_USERS_FRAGMENT, R.id.fragment_container) {
chat.rocket.android.inviteusers.ui.newInstance(chatRoomId)
}
}

fun toMemberDetails(userId: String, chatRoomId: String) {
activity.addFragmentBackStack(TAG_USER_DETAILS_FRAGMENT, R.id.fragment_container) {
chat.rocket.android.userdetails.ui.newInstance(userId, chatRoomId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import chat.rocket.android.draw.main.di.DrawModule
import chat.rocket.android.draw.main.ui.DrawingActivity
import chat.rocket.android.favoritemessages.di.FavoriteMessagesFragmentProvider
import chat.rocket.android.files.di.FilesFragmentProvider
import chat.rocket.android.inviteusers.di.InviteUsersFragmentProvider
import chat.rocket.android.main.di.MainModule
import chat.rocket.android.main.ui.MainActivity
import chat.rocket.android.members.di.MembersFragmentProvider
Expand Down Expand Up @@ -84,6 +85,7 @@ abstract class ActivityBuilder {
UserDetailsFragmentProvider::class,
ChatDetailsFragmentProvider::class,
MembersFragmentProvider::class,
InviteUsersFragmentProvider::class,
MentionsFragmentProvider::class,
PinnedMessagesFragmentProvider::class,
FavoriteMessagesFragmentProvider::class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package chat.rocket.android.inviteusers.di

import chat.rocket.android.dagger.scope.PerFragment
import chat.rocket.android.inviteusers.presentation.InviteUsersView
import chat.rocket.android.inviteusers.ui.InviteUsersFragment
import dagger.Module
import dagger.Provides

@Module
class InviteUsersFragmentModule {

@Provides
@PerFragment
fun inviteUsersView(frag: InviteUsersFragment): InviteUsersView {
return frag
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package chat.rocket.android.inviteusers.di

import chat.rocket.android.dagger.scope.PerFragment
import chat.rocket.android.inviteusers.ui.InviteUsersFragment
import dagger.Module
import dagger.android.ContributesAndroidInjector

@Module
abstract class InviteUsersFragmentProvider {

@ContributesAndroidInjector(modules = [InviteUsersFragmentModule::class])
@PerFragment
abstract fun provideInviteUsersFragment(): InviteUsersFragment
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package chat.rocket.android.inviteusers.presentation

import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.db.DatabaseManager
import chat.rocket.android.members.uimodel.MemberUiModelMapper
import chat.rocket.android.server.infrastructure.RocketChatClientFactory
import chat.rocket.android.util.extension.launchUI
import chat.rocket.common.RocketChatException
import chat.rocket.common.model.roomTypeOf
import chat.rocket.common.util.ifNull
import chat.rocket.core.RocketChatClient
import chat.rocket.core.internal.rest.invite
import chat.rocket.core.internal.rest.spotlight
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import javax.inject.Inject
import javax.inject.Named

class InviteUsersPresenter @Inject constructor(
private val view: InviteUsersView,
private val dbManager: DatabaseManager,
@Named("currentServer") private val currentServer: String,
private val strategy: CancelStrategy,
private val mapper: MemberUiModelMapper,
val factory: RocketChatClientFactory
) {
private val client: RocketChatClient = factory.get(currentServer)

fun inviteUsers(chatRoomId: String, usersList: List<Pair<String, String>>) {
launchUI(strategy) {
view.disableUserInput()
view.showLoading()

val stringBuilder = StringBuilder()

try {
for (user in usersList) {
try {
client.invite(chatRoomId, roomTypeOf(getChatRoomType(chatRoomId)), user.first)
stringBuilder.append("Invited : ${user.second}\n")
} catch (exception: RocketChatException) {
exception.message?.let {
stringBuilder.append("Exception : ${user.second} : $it\n")
}.ifNull {
stringBuilder.append("Error : ${user.second} : Try again later\n")
}
}
}
} finally {
view.showMessage(stringBuilder.toString())
view.hideLoading()
view.enableUserInput()
view.usersInvitedSuccessfully()
}
}
}

fun searchUser(query: String) {
launchUI(strategy) {
view.showSuggestionViewInProgress()
try {
val users = client.spotlight(query).users
if (users.isEmpty()) {
view.showNoUserSuggestion()
} else {
view.showUserSuggestion(mapper.mapToUiModelList(users))
}
} catch (ex: RocketChatException) {
ex.message?.let {
view.showMessage(it)
}.ifNull {
view.showGenericErrorMessage()
}
} finally {
view.hideSuggestionViewInProgress()
}
}
}

private suspend fun getChatRoomType(chatRoomId: String): String {
return withContext(Dispatchers.IO + strategy.jobs) {
return@withContext dbManager.getRoom(chatRoomId)?.chatRoom.let { it?.type ?: "" }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package chat.rocket.android.inviteusers.presentation

import chat.rocket.android.core.behaviours.LoadingView
import chat.rocket.android.core.behaviours.MessageView
import chat.rocket.android.members.uimodel.MemberUiModel

interface InviteUsersView : LoadingView, MessageView {

/**
* Shows the server's users suggestion (on the basis of the user typing - the query).
*
* @param dataSet The list of server's users to show.
*/
fun showUserSuggestion(dataSet: List<MemberUiModel>)

/**
* Shows no server's users suggestion.
*/
fun showNoUserSuggestion()

/**
* Shows the SuggestionView in progress.
*/
fun showSuggestionViewInProgress()

/**
* Hides the progress shown in the SuggestionView.
*/
fun hideSuggestionViewInProgress()

/**
* Take actions after users are successfully invited.
*/
fun usersInvitedSuccessfully()

/**
* Enables the user input.
*/
fun enableUserInput()

/**
* Disables the user input.
*/
fun disableUserInput()
}
Loading

0 comments on commit 34d3de7

Please sign in to comment.