Skip to content

Commit

Permalink
Merge pull request #37 from janewaitara/feat_code_cleanup
Browse files Browse the repository at this point in the history
Feat code cleanup
  • Loading branch information
janewaitara committed Mar 13, 2024
2 parents 94bd034 + 9c9c37c commit 63040c0
Show file tree
Hide file tree
Showing 26 changed files with 535 additions and 273 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface ProjectsDao {

/** Fetch */
@Query("SELECT * from projects_table WHERE projectId = :projectId")
suspend fun getProjectById(projectId: Int): Project
fun getProjectById(projectId: Int): Flow<Project>

@Transaction
@Query("SELECT * FROM projects_table WHERE projectId = :projectId")
Expand Down
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>
}
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)
}
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")
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package com.mumbicodes.projectie.data.repository

import com.mumbicodes.projectie.data.db.ProjectsDao
import com.mumbicodes.projectie.data.helpers.LocalResult
import com.mumbicodes.projectie.data.helpers.safeTransaction
import com.mumbicodes.projectie.data.helpers.toDataResult
import com.mumbicodes.projectie.domain.model.DataResult
import com.mumbicodes.projectie.domain.model.Project
import com.mumbicodes.projectie.domain.model.ProjectName
import com.mumbicodes.projectie.domain.relations.ProjectWithMilestones
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 ProjectsRepositoryImpl(
private val projectsDao: ProjectsDao,
Expand All @@ -18,17 +25,51 @@ class ProjectsRepositoryImpl(
projectsDao.updateProject(project = project)
}

override suspend fun getProjectById(projectId: Int): Project =
projectsDao.getProjectById(projectId)
override suspend fun getProjectById(projectId: Int): DataResult<Flow<Project>> =
when (val localResult = safeTransaction { projectsDao.getProjectById(projectId) }) {
is LocalResult.Error -> DataResult.Error(localResult.errorMessage)
is LocalResult.Success -> DataResult.Success(localResult.data)
}

override fun getProjectByIdWithMilestones(projectId: Int): Flow<ProjectWithMilestones?> =
projectsDao.getProjectByIdWithMilestones(projectId)
override suspend fun getProjectByIdWithMilestones(projectId: Int): DataResult<Flow<ProjectWithMilestones?>> =
when (
val localResult =
safeTransaction { projectsDao.getProjectByIdWithMilestones(projectId) }
) {
is LocalResult.Error -> DataResult.Error(localResult.errorMessage)
is LocalResult.Success -> DataResult.Success(localResult.data)
}

override fun getAllProjects(): Flow<List<Project>> =
projectsDao.getAllProjects()
/**
* Added logic to get projects and sort the projects
*
* By default, the order is the date added
* */
override suspend fun getAllProjects(projectOrder: ProjectsOrder): DataResult<Flow<List<Project>>> =
safeTransaction {
projectsDao.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 }
}
}

override fun getProjectNameAndId(): Flow<List<ProjectName>> =
projectsDao.getProjectNameAndId()
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 }
}
}
}
}
}.toDataResult()

override suspend fun getProjectNameAndId(): DataResult<Flow<List<ProjectName>>> =
safeTransaction { projectsDao.getProjectNameAndId() }.toDataResult()

override suspend fun deleteProject(project: Project) {
projectsDao.deleteProject(project)
Expand Down
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>
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.mumbicodes.projectie.domain.repository

import com.mumbicodes.projectie.domain.model.DataResult
import com.mumbicodes.projectie.domain.model.Project
import com.mumbicodes.projectie.domain.model.ProjectName
import com.mumbicodes.projectie.domain.relations.ProjectWithMilestones
import com.mumbicodes.projectie.domain.util.ProjectsOrder
import kotlinx.coroutines.flow.Flow

interface ProjectsRepository {
Expand All @@ -11,13 +13,13 @@ interface ProjectsRepository {

suspend fun updateProject(project: Project)

suspend fun getProjectById(projectId: Int): Project
suspend fun getProjectById(projectId: Int): DataResult<Flow<Project>>

fun getProjectByIdWithMilestones(projectId: Int): Flow<ProjectWithMilestones?>
suspend fun getProjectByIdWithMilestones(projectId: Int): DataResult <Flow<ProjectWithMilestones?>>

fun getAllProjects(): Flow<List<Project>>
suspend fun getAllProjects(projectOrder: ProjectsOrder): DataResult<Flow<List<Project>>>

fun getProjectNameAndId(): Flow<List<ProjectName>>
suspend fun getProjectNameAndId(): DataResult<Flow<List<ProjectName>>>

suspend fun deleteProject(project: Project)

Expand Down
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)
}
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)
}
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()
}
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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import androidx.hilt.work.HiltWorker
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import com.mumbicodes.projectie.R
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 com.mumbicodes.projectie.presentation.util.toLocalDate
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
Expand All @@ -31,27 +34,34 @@ class CheckProjectDeadlineIsInTwoDaysWorker @AssistedInject constructor(
return@withContext try {
Log.e("Reached 3", "It has been reached - 2 days ")

val allProjects = projectsRepository.getAllProjects()
val allProjects = projectsRepository.getAllProjects(
projectOrder = ProjectsOrder.DateAdded(OrderType.Descending)
)

CoroutineScope(Dispatchers.IO).launch {
allProjects.collectLatest { projects ->
val deadlineIsInTwoDaysProjects =
async { checkDeadlineIsInTwoDays(projects) }
deadlineIsInTwoDaysProjects.await().let { deadlineProjects ->
if (deadlineProjects.size > 1) {
makeNotification(
notificationType = NotificationType.PROJECTS,
notificationId = deadlineProjects.first().projectId,
message = "You have ${deadlineProjects.size} projects ending in 2 days",
context = applicationContext,
)
} else if (deadlineProjects.size == 1) {
makeNotification(
notificationType = NotificationType.PROJECTS,
notificationId = deadlineProjects.first().projectId,
message = "${deadlineProjects.first().projectName} deadline is in 2 days and it's ${deadlineProjects.first().projectStatus}",
context = applicationContext,
)
when (allProjects) {
is DataResult.Error -> Result.failure()
is DataResult.Success -> {
allProjects.data.collectLatest { projects ->
val deadlineIsInTwoDaysProjects =
async { checkDeadlineIsInTwoDays(projects) }
deadlineIsInTwoDaysProjects.await().let { deadlineProjects ->
if (deadlineProjects.size > 1) {
makeNotification(
notificationType = NotificationType.PROJECTS,
notificationId = deadlineProjects.first().projectId,
message = "You have ${deadlineProjects.size} projects ending in 2 days",
context = applicationContext,
)
} else if (deadlineProjects.size == 1) {
makeNotification(
notificationType = NotificationType.PROJECTS,
notificationId = deadlineProjects.first().projectId,
message = "${deadlineProjects.first().projectName} deadline is in 2 days and it's ${deadlineProjects.first().projectStatus}",
context = applicationContext,
)
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import androidx.hilt.work.HiltWorker
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import com.mumbicodes.projectie.R
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 com.mumbicodes.projectie.presentation.util.toLocalDate
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
Expand All @@ -27,28 +30,38 @@ class CheckProjectDeadlineWorker @AssistedInject constructor(

return withContext(Dispatchers.IO) {
return@withContext try {
val allProjects = projectsRepository.getAllProjects()
val allProjects = projectsRepository.getAllProjects(
projectOrder = ProjectsOrder.DateAdded(
OrderType.Descending
)
)

CoroutineScope(Dispatchers.IO).launch {
allProjects.collectLatest { projects ->
val deadlineIsTodayProjects = async { checkDeadlineIsToday(projects) }
deadlineIsTodayProjects.await().let { deadlineProjects ->
if (deadlineProjects.isNotEmpty() && deadlineProjects.size > 1) {
// Grouped Notifications
makeNotification(
notificationType = NotificationType.PROJECTS,
notificationId = deadlineProjects.first().projectId,
message = "You have ${deadlineProjects.size} projects ending Today",
context = applicationContext,
)
} else if (deadlineProjects.size == 1) {
// One notification
makeNotification(
notificationType = NotificationType.PROJECTS,
notificationId = deadlineProjects.first().projectId,
message = "${deadlineProjects.first().projectName} deadline is today and it's ${deadlineProjects.first().projectStatus}",
context = applicationContext,
)
when (allProjects) {
is DataResult.Error -> Result.failure()
is DataResult.Success -> {
allProjects.data.collectLatest { projects ->
val deadlineIsTodayProjects =
async { checkDeadlineIsToday(projects) }
deadlineIsTodayProjects.await().let { deadlineProjects ->
if (deadlineProjects.isNotEmpty() && deadlineProjects.size > 1) {
// Grouped Notifications
makeNotification(
notificationType = NotificationType.PROJECTS,
notificationId = deadlineProjects.first().projectId,
message = "You have ${deadlineProjects.size} projects ending Today",
context = applicationContext,
)
} else if (deadlineProjects.size == 1) {
// One notification
makeNotification(
notificationType = NotificationType.PROJECTS,
notificationId = deadlineProjects.first().projectId,
message = "${deadlineProjects.first().projectName} deadline is today and it's ${deadlineProjects.first().projectStatus}",
context = applicationContext,
)
}
}
}
}
}
Expand Down
Loading

0 comments on commit 63040c0

Please sign in to comment.