Skip to content

Commit

Permalink
teams : adds joining to a separate screen
Browse files Browse the repository at this point in the history
  • Loading branch information
MamboBryan committed Jan 20, 2025
1 parent a1dca90 commit db01f65
Show file tree
Hide file tree
Showing 14 changed files with 540 additions and 4 deletions.
1 change: 1 addition & 0 deletions modules-features/join/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
14 changes: 14 additions & 0 deletions modules-features/join/build.gradle.kts
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.
21 changes: 21 additions & 0 deletions modules-features/join/proguard-rules.pro
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
4 changes: 4 additions & 0 deletions modules-features/join/src/main/AndroidManifest.xml
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>

Large diffs are not rendered by default.

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) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fun TeamsListScreenContent(
}

if (state.isJoining) {
navigate(rememberScreen(SharedScreen.Team(isJoining = true, teamId = null)))
navigate(rememberScreen(SharedScreen.Join))
}

if (state.teamId != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import com.bizilabs.streeek.lib.domain.repositories.TeamRepository
import com.bizilabs.streeek.lib.domain.repositories.team.TeamRequestRepository
import com.bizilabs.streeek.lib.domain.workers.startImmediateSyncTeamsWork
import kotlinx.coroutines.InternalCoroutinesApi
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
Expand Down Expand Up @@ -198,8 +197,6 @@ class TeamScreenModel(
private val clickedTeam =
combine(teamRepository.teamId, teamRepository.teams) { id, map -> map[id] }

private var processJobs: Job? = null

init {
observeTeamsSyncing()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ sealed class SharedScreen : ScreenProvider {
data class Leaderboard(val name: String) : SharedScreen()

object Points : SharedScreen()

object Join : SharedScreen()
}
1 change: 1 addition & 0 deletions modules-ui/presentation/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies {
implementation(projects.modulesFeatures.issue)
implementation(projects.modulesFeatures.leaderboard)
implementation(projects.modulesFeatures.points)
implementation(projects.modulesFeatures.join)

// splash screen
implementation(libs.androidx.core.splashscreen)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Application
import com.bizilabs.streeek.feature.authentication.authenticationModule
import com.bizilabs.streeek.feature.issue.FeatureIssueModule
import com.bizilabs.streeek.feature.issues.FeatureIssuesModule
import com.bizilabs.streeek.feature.join.FeatureJoin
import com.bizilabs.streeek.feature.landing.landingModule
import com.bizilabs.streeek.feature.leaderboard.FeatureLeaderboard
import com.bizilabs.streeek.feature.points.FeaturePoints
Expand Down Expand Up @@ -47,6 +48,7 @@ val PresentationModule =
FeatureIssueModule,
FeatureLeaderboard,
FeaturePoints,
FeatureJoin,
)
workerOf(::SyncTeamsWork)
workerOf(::SyncLevelsWork)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import cafe.adriel.voyager.core.registry.ScreenRegistry.invoke
import com.bizilabs.streeek.feature.authentication.featureAuthentication
import com.bizilabs.streeek.feature.issue.screenIssue
import com.bizilabs.streeek.feature.issues.screenIssues
import com.bizilabs.streeek.feature.join.ScreenJoinTeam
import com.bizilabs.streeek.feature.landing.featureLanding
import com.bizilabs.streeek.feature.leaderboard.ScreenLeaderboard
import com.bizilabs.streeek.feature.points.ScreenPoints
Expand All @@ -26,5 +27,6 @@ fun Application.initVoyager() {
screenIssue()
ScreenLeaderboard()
ScreenPoints()
ScreenJoinTeam()
}
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ include(":modules-features:issues")
include(":modules-features:issue")
include(":modules-features:leaderboard")
include(":modules-features:points")
include(":modules-features:join")

0 comments on commit db01f65

Please sign in to comment.