-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from janewaitara/feat_code_cleanup
Feat code cleanup
- Loading branch information
Showing
26 changed files
with
535 additions
and
273 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
6 changes: 6 additions & 0 deletions
6
app/src/main/java/com/mumbicodes/projectie/data/helpers/LocalResult.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,6 @@ | ||
package com.mumbicodes.projectie.data.helpers | ||
|
||
sealed interface LocalResult<out T> { | ||
data class Success<T>(val data: T) : LocalResult<T> | ||
data class Error(val errorMessage: String) : LocalResult<Nothing> | ||
} |
8 changes: 8 additions & 0 deletions
8
app/src/main/java/com/mumbicodes/projectie/data/helpers/LocalResultMapper.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,8 @@ | ||
package com.mumbicodes.projectie.data.helpers | ||
|
||
import com.mumbicodes.projectie.domain.model.DataResult | ||
|
||
fun <T> LocalResult<T>.toDataResult(): DataResult<T> = when (this) { | ||
is LocalResult.Error -> DataResult.Error(this.errorMessage) | ||
is LocalResult.Success -> DataResult.Success(this.data) | ||
} |
10 changes: 10 additions & 0 deletions
10
app/src/main/java/com/mumbicodes/projectie/data/helpers/SafeTransaction.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,10 @@ | ||
package com.mumbicodes.projectie.data.helpers | ||
|
||
suspend fun <T> safeTransaction( | ||
block: suspend () -> T, | ||
): LocalResult<T> = | ||
try { | ||
LocalResult.Success(block()) | ||
} catch (exception: Exception) { | ||
LocalResult.Error(exception.localizedMessage ?: "There was an error received") | ||
} |
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
6 changes: 6 additions & 0 deletions
6
app/src/main/java/com/mumbicodes/projectie/domain/model/DataResult.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,6 @@ | ||
package com.mumbicodes.projectie.domain.model | ||
|
||
sealed interface DataResult<out T> { | ||
data class Success<T>(val data: T) : DataResult<T> | ||
data class Error(val errorMessage: String) : DataResult<Nothing> | ||
} |
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
4 changes: 3 additions & 1 deletion
4
app/src/main/java/com/mumbicodes/projectie/domain/use_case/projects/GetProjectByIdUseCase.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 |
---|---|---|
@@ -1,11 +1,13 @@ | ||
package com.mumbicodes.projectie.domain.use_case.projects | ||
|
||
import com.mumbicodes.projectie.domain.model.DataResult | ||
import com.mumbicodes.projectie.domain.model.Project | ||
import com.mumbicodes.projectie.domain.repository.ProjectsRepository | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
class GetProjectByIdUseCase( | ||
private val repository: ProjectsRepository | ||
) { | ||
suspend operator fun invoke(projectId: Int): Project = | ||
suspend operator fun invoke(projectId: Int): DataResult<Flow<Project>> = | ||
repository.getProjectById(projectId) | ||
} |
5 changes: 3 additions & 2 deletions
5
.../com/mumbicodes/projectie/domain/use_case/projects/GetProjectByIdWithMilestonesUseCase.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 |
---|---|---|
@@ -1,14 +1,15 @@ | ||
package com.mumbicodes.projectie.domain.use_case.projects | ||
|
||
import com.mumbicodes.projectie.domain.model.DataResult | ||
import com.mumbicodes.projectie.domain.relations.ProjectWithMilestones | ||
import com.mumbicodes.projectie.domain.repository.ProjectsRepository | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
class GetProjectByIdWithMilestonesUseCase( | ||
private val repository: ProjectsRepository, | ||
) { | ||
operator fun invoke( | ||
suspend operator fun invoke( | ||
projectId: Int, | ||
): Flow<ProjectWithMilestones?> = | ||
): DataResult<Flow<ProjectWithMilestones?>> = | ||
repository.getProjectByIdWithMilestones(projectId) | ||
} |
3 changes: 2 additions & 1 deletion
3
...main/java/com/mumbicodes/projectie/domain/use_case/projects/GetProjectNameAndIdUseCase.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 |
---|---|---|
@@ -1,12 +1,13 @@ | ||
package com.mumbicodes.projectie.domain.use_case.projects | ||
|
||
import com.mumbicodes.projectie.domain.model.DataResult | ||
import com.mumbicodes.projectie.domain.model.ProjectName | ||
import com.mumbicodes.projectie.domain.repository.ProjectsRepository | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
class GetProjectNameAndIdUseCase( | ||
private val repository: ProjectsRepository, | ||
) { | ||
operator fun invoke(): Flow<List<ProjectName>> = | ||
suspend operator fun invoke(): DataResult<Flow<List<ProjectName>>> = | ||
repository.getProjectNameAndId() | ||
} |
33 changes: 4 additions & 29 deletions
33
app/src/main/java/com/mumbicodes/projectie/domain/use_case/projects/GetProjectsUseCase.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 |
---|---|---|
@@ -1,42 +1,17 @@ | ||
package com.mumbicodes.projectie.domain.use_case.projects | ||
|
||
import com.mumbicodes.projectie.domain.model.DataResult | ||
import com.mumbicodes.projectie.domain.model.Project | ||
import com.mumbicodes.projectie.domain.repository.ProjectsRepository | ||
import com.mumbicodes.projectie.domain.util.OrderType | ||
import com.mumbicodes.projectie.domain.util.ProjectsOrder | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
|
||
class GetProjectsUseCase( | ||
private val repository: ProjectsRepository, | ||
) { | ||
/** | ||
* Added logic to get projects and sort the projects | ||
* | ||
* By default, the order is the date added | ||
* */ | ||
operator fun invoke( | ||
suspend operator fun invoke( | ||
projectOrder: ProjectsOrder = ProjectsOrder.DateAdded(OrderType.Descending), | ||
): Flow<List<Project>> { | ||
|
||
return repository.getAllProjects() | ||
.map { projects -> | ||
when (projectOrder.orderType) { | ||
is OrderType.Ascending -> { | ||
when (projectOrder) { | ||
is ProjectsOrder.Name -> projects.sortedBy { it.projectName.lowercase() } | ||
is ProjectsOrder.Deadline -> projects.sortedBy { it.projectDeadline } | ||
is ProjectsOrder.DateAdded -> projects.sortedBy { it.timeStamp } | ||
} | ||
} | ||
is OrderType.Descending -> { | ||
when (projectOrder) { | ||
is ProjectsOrder.Name -> projects.sortedByDescending { it.projectName.lowercase() } | ||
is ProjectsOrder.Deadline -> projects.sortedByDescending { it.projectDeadline } | ||
is ProjectsOrder.DateAdded -> projects.sortedByDescending { it.timeStamp } | ||
} | ||
} | ||
} | ||
} | ||
} | ||
): DataResult<Flow<List<Project>>> = | ||
repository.getAllProjects(projectOrder = projectOrder) | ||
} |
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
Oops, something went wrong.