This repository has been archived by the owner on Jun 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 552
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3b97275
commit 34d3de7
Showing
13 changed files
with
604 additions
and
7 deletions.
There are no files selected for viewing
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
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
17 changes: 17 additions & 0 deletions
17
app/src/main/java/chat/rocket/android/inviteusers/di/InviteUsersFragmentModule.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,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 | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
app/src/main/java/chat/rocket/android/inviteusers/di/InviteUsersFragmentProvider.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,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 | ||
} |
85 changes: 85 additions & 0 deletions
85
app/src/main/java/chat/rocket/android/inviteusers/presentation/InviteUsersPresenter.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,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 ?: "" } | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
app/src/main/java/chat/rocket/android/inviteusers/presentation/InviteUsersView.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,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() | ||
} |
Oops, something went wrong.