generated from Bizyback/template-android
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
teams : adds joining to a separate screen
- Loading branch information
1 parent
a1dca90
commit db01f65
Showing
14 changed files
with
540 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
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 @@ | ||
plugins { | ||
id("bizilabs.convention.feature") | ||
} | ||
|
||
android { | ||
namespace = "com.bizilabs.streeek.feature.join" | ||
} | ||
|
||
dependencies { | ||
// paging | ||
implementation(libs.bundles.paging) | ||
// coil | ||
implementation(libs.bundles.coil) | ||
} |
Empty file.
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,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
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,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
||
</manifest> |
338 changes: 338 additions & 0 deletions
338
modules-features/join/src/main/java/com/bizilabs/streeek/feature/join/JoinScreen.kt
Large diffs are not rendered by default.
Oops, something went wrong.
153 changes: 153 additions & 0 deletions
153
modules-features/join/src/main/java/com/bizilabs/streeek/feature/join/JoinScreenModel.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,153 @@ | ||
package com.bizilabs.streeek.feature.join | ||
|
||
import cafe.adriel.voyager.core.model.StateScreenModel | ||
import cafe.adriel.voyager.core.model.screenModelScope | ||
import com.bizilabs.streeek.lib.common.models.FetchState | ||
import com.bizilabs.streeek.lib.design.components.DialogState | ||
import com.bizilabs.streeek.lib.domain.helpers.DataResult | ||
import com.bizilabs.streeek.lib.domain.models.TeamAndMembersDomain | ||
import com.bizilabs.streeek.lib.domain.repositories.TeamRepository | ||
import com.bizilabs.streeek.lib.domain.repositories.team.TeamRequestRepository | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.flow.update | ||
import kotlinx.coroutines.launch | ||
import org.koin.dsl.module | ||
|
||
val FeatureJoin = | ||
module { | ||
factory { JoinScreenModel(teamRepository = get(), teamRequestRepository = get()) } | ||
} | ||
|
||
data class RequestTeamState( | ||
val teamId: Long, | ||
val requestState: FetchState<Boolean> = FetchState.Loading, | ||
) | ||
|
||
data class JoinScreenState( | ||
val isSearching: Boolean = false, | ||
val query: String = "", | ||
val requestedTeamIds: List<Long> = emptyList(), | ||
val requestState: RequestTeamState? = null, | ||
val dialogState: DialogState? = null, | ||
val token: String = "", | ||
val teamId: Long? = null, | ||
val joiningWithCode: Boolean = false, | ||
) { | ||
val isJoinActionEnabled: Boolean | ||
get() = token.length == 6 && dialogState == null | ||
} | ||
|
||
class JoinScreenModel( | ||
private val teamRepository: TeamRepository, | ||
private val teamRequestRepository: TeamRequestRepository, | ||
) : StateScreenModel<JoinScreenState>(JoinScreenState()) { | ||
val teams = teamRepository.getTeamsAndMembers() | ||
|
||
fun onClickSearch() { | ||
mutableState.update { it.copy(isSearching = true) } | ||
} | ||
|
||
fun onClickDismissSearch() { | ||
mutableState.update { it.copy(isSearching = false) } | ||
} | ||
|
||
fun onClickDismissDialog() { | ||
mutableState.update { it.copy(dialogState = null) } | ||
} | ||
|
||
fun onClickTeamRequest(teamAndMembers: TeamAndMembersDomain) { | ||
requestToJoinTeam(teamId = teamAndMembers.team.id) | ||
} | ||
|
||
private fun requestToJoinTeam(teamId: Long) { | ||
screenModelScope.launch { | ||
mutableState.update { it.copy(requestState = RequestTeamState(teamId = teamId)) } | ||
val result = teamRequestRepository.requestToJoinTeam(teamId = teamId) | ||
when (result) { | ||
is DataResult.Error -> { | ||
mutableState.update { | ||
it.copy( | ||
dialogState = | ||
DialogState.Error( | ||
title = "Error", | ||
message = "Wasn't able to send request to join team", | ||
), | ||
requestState = | ||
RequestTeamState( | ||
teamId = teamId, | ||
requestState = FetchState.Error(result.message), | ||
), | ||
) | ||
} | ||
delay(2000) | ||
mutableState.update { it.copy(dialogState = null, requestState = null) } | ||
} | ||
|
||
is DataResult.Success -> { | ||
val requests = state.value.requestedTeamIds.toMutableList() | ||
requests.add(teamId) | ||
mutableState.update { | ||
it.copy( | ||
requestedTeamIds = requests, | ||
requestState = | ||
it.requestState?.copy( | ||
requestState = FetchState.Success(result.data), | ||
), | ||
) | ||
} | ||
delay(2000) | ||
mutableState.update { it.copy(dialogState = null, requestState = null) } | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun onValueChangeTeamCode(token: String) { | ||
mutableState.update { it.copy(token = token) } | ||
} | ||
|
||
fun onClickJoin() { | ||
joinTeam() | ||
} | ||
|
||
private fun joinTeam() { | ||
val token = state.value.token | ||
if (token.length != 6 && token.any { it.digitToIntOrNull() == null }) return | ||
mutableState.update { it.copy(dialogState = DialogState.Loading()) } | ||
screenModelScope.launch { | ||
when (val result = teamRepository.joinTeam(token = token)) { | ||
is DataResult.Error -> { | ||
mutableState.update { | ||
it.copy( | ||
dialogState = | ||
DialogState.Error( | ||
title = "Error", | ||
message = result.message, | ||
), | ||
) | ||
} | ||
} | ||
|
||
is DataResult.Success -> { | ||
mutableState.update { | ||
it.copy( | ||
dialogState = | ||
DialogState.Success( | ||
title = "Success", | ||
message = "Joined team successfully as a ${result.data.role}", | ||
), | ||
) | ||
} | ||
delay(500) | ||
mutableState.update { it.copy(teamId = result.data.teamId) } | ||
delay(2000) | ||
mutableState.update { it.copy(teamId = null, dialogState = null) } | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun onClickJoinWithCode(isVisible: Boolean) { | ||
mutableState.update { it.copy(joiningWithCode = isVisible) } | ||
} | ||
} |
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
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
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