Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat code cleanup #40

Merged
merged 4 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.mumbicodes.projectie.data.db

import androidx.room.*
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Query
import androidx.room.Upsert
import com.mumbicodes.projectie.domain.model.Milestone
import com.mumbicodes.projectie.domain.relations.MilestoneWithTasks
import kotlinx.coroutines.flow.Flow

@Dao
interface MilestonesDao {

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertMilestone(milestone: Milestone)
@Upsert
suspend fun insertOrUpdateMilestone(milestone: Milestone)

/** Fetch */
@Query("SELECT * from milestones_table WHERE milestoneId = :milestoneId")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package com.mumbicodes.projectie.data.db

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Upsert
import com.mumbicodes.projectie.domain.model.Task

@Dao
interface TasksDao {

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertTask(tasks: List<Task>)
@Upsert
suspend fun insertOrUpdateTask(tasks: List<Task>)

@Delete
suspend fun deleteTask(task: Task)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,20 @@ import com.mumbicodes.projectie.domain.model.DataResult
import com.mumbicodes.projectie.domain.model.Milestone
import com.mumbicodes.projectie.domain.relations.MilestoneWithTasks
import com.mumbicodes.projectie.domain.repository.MilestonesRepository
import com.mumbicodes.projectie.domain.util.AllMilestonesOrder
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

class MilestonesRepositoryImpl(
private val milestonesDao: MilestonesDao,
) : MilestonesRepository {

override suspend fun insertMilestone(milestone: Milestone) {
milestonesDao.insertMilestone(milestone)
override suspend fun insertOrUpdateMilestone(milestone: Milestone) {
milestonesDao.insertOrUpdateMilestone(milestone)
}

override suspend fun getMilestoneByIdWithTasks(milestoneId: Int): DataResult<Flow<MilestoneWithTasks?>> = safeTransaction {
milestonesDao.getMilestoneByIdWithTasks(milestoneId)
}.toDataResult()
override suspend fun getMilestoneByIdWithTasks(milestoneId: Int): DataResult<Flow<MilestoneWithTasks?>> =
safeTransaction {
milestonesDao.getMilestoneByIdWithTasks(milestoneId)
}.toDataResult()

override suspend fun getAllMilestonesBasedOnProjIdAndStatus(
projectId: Int,
Expand All @@ -30,19 +29,8 @@ class MilestonesRepositoryImpl(
milestonesDao.getAllMilestonesBasedOnProjIdAndStatus(projectId)
}.toDataResult()

override suspend fun getAllMilestones(milestonesOrder: AllMilestonesOrder): DataResult<Flow<List<MilestoneWithTasks>>> =
safeTransaction {
milestonesDao.getAllMilestones().map { milestonesWithTasks ->
when (milestonesOrder) {
is AllMilestonesOrder.MostUrgent -> milestonesWithTasks.sortedBy {
it.milestone.milestoneEndDate
}
is AllMilestonesOrder.LeastUrgent -> milestonesWithTasks.sortedByDescending {
it.milestone.milestoneEndDate
}
}
}
}.toDataResult()
override suspend fun getAllMilestones(): DataResult<Flow<List<MilestoneWithTasks>>> =
safeTransaction { milestonesDao.getAllMilestones() }.toDataResult()

override suspend fun deleteMilestone(milestone: Milestone) {
milestonesDao.deleteMilestone(milestone)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ 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 Down Expand Up @@ -45,28 +42,8 @@ class ProjectsRepositoryImpl(
*
* 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 }
}
}

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 getAllProjects(): DataResult<Flow<List<Project>>> =
safeTransaction { projectsDao.getAllProjects() }.toDataResult()

override suspend fun getProjectNameAndId(): DataResult<Flow<List<ProjectName>>> =
safeTransaction { projectsDao.getProjectNameAndId() }.toDataResult()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import com.mumbicodes.projectie.domain.model.Task
import com.mumbicodes.projectie.domain.repository.TasksRepository

class TaskRepositoryImpl(private val tasksDao: TasksDao) : TasksRepository {
override suspend fun insertTask(tasks: List<Task>) {
tasksDao.insertTask(tasks)
override suspend fun insertOrUpdateTask(tasks: List<Task>) {
tasksDao.insertOrUpdateTask(tasks)
}

override suspend fun deleteTask(task: Task) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import com.mumbicodes.projectie.domain.use_case.onBoarding.OnBoardingUseCases
import com.mumbicodes.projectie.domain.use_case.onBoarding.ReadOnBoardingStateUseCase
import com.mumbicodes.projectie.domain.use_case.onBoarding.SaveOnBoardingStateUseCase
import com.mumbicodes.projectie.domain.use_case.projects.*
import com.mumbicodes.projectie.domain.use_case.tasks.AddTasksUseCase
import com.mumbicodes.projectie.domain.use_case.tasks.DeleteTaskUseCase
import com.mumbicodes.projectie.domain.use_case.tasks.InsertOrUpdateTasksUseCase
import com.mumbicodes.projectie.domain.use_case.tasks.TasksUseCases
import com.mumbicodes.projectie.domain.use_case.tasks.TransformTasksUseCase
import com.mumbicodes.projectie.domain.use_case.workers.CancelWorkerUseCase
Expand Down Expand Up @@ -62,7 +62,7 @@ object UseCaseModule {
app: Application,
): MilestonesUseCases {
return MilestonesUseCases(
addMilestoneUseCase = AddMilestoneUseCase(repository),
insertOrUpdateMilestoneUseCase = InsertOrUpdateMilestoneUseCase(repository),
getMilestoneByIdWithTasksUseCase = GetMilestoneByIdWithTasksUseCase(repository),
getMilestonesUseCase = GetMilestonesUseCase(repository),
deleteMilestoneUseCase = DeleteMilestoneUseCase(repository),
Expand All @@ -79,7 +79,7 @@ object UseCaseModule {
repository: TasksRepository,
): TasksUseCases {
return TasksUseCases(
addTasksUseCase = AddTasksUseCase(repository),
insertOrUpdateTasksUseCase = InsertOrUpdateTasksUseCase(repository),
deleteTaskUseCase = DeleteTaskUseCase(repository),
transformTasksUseCase = TransformTasksUseCase(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package com.mumbicodes.projectie.domain.repository
import com.mumbicodes.projectie.domain.model.DataResult
import com.mumbicodes.projectie.domain.model.Milestone
import com.mumbicodes.projectie.domain.relations.MilestoneWithTasks
import com.mumbicodes.projectie.domain.util.AllMilestonesOrder
import kotlinx.coroutines.flow.Flow

interface MilestonesRepository {

suspend fun insertMilestone(milestone: Milestone)
suspend fun insertOrUpdateMilestone(milestone: Milestone)

suspend fun getMilestoneByIdWithTasks(milestoneId: Int): DataResult <Flow<MilestoneWithTasks?>>

Expand All @@ -17,7 +16,7 @@ interface MilestonesRepository {
status: String?,
): DataResult<List<Milestone>>

suspend fun getAllMilestones(milestonesOrder: AllMilestonesOrder): DataResult<Flow<List<MilestoneWithTasks>>>
suspend fun getAllMilestones(): DataResult<Flow<List<MilestoneWithTasks>>>

suspend fun deleteMilestone(milestone: Milestone)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ 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 @@ -17,7 +16,7 @@ interface ProjectsRepository {

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.mumbicodes.projectie.domain.model.Task

interface TasksRepository {

suspend fun insertTask(tasks: List<Task>)
suspend fun insertOrUpdateTask(tasks: List<Task>)

suspend fun deleteTask(task: Task)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ package com.mumbicodes.projectie.domain.use_case.milestones
import com.mumbicodes.projectie.domain.model.DataResult
import com.mumbicodes.projectie.domain.relations.MilestoneWithTasks
import com.mumbicodes.projectie.domain.repository.MilestonesRepository
import com.mumbicodes.projectie.domain.util.AllMilestonesOrder
import kotlinx.coroutines.flow.Flow

class GetAllMilestonesUseCase(private val repository: MilestonesRepository) {

suspend operator fun invoke(
milestonesOrder: AllMilestonesOrder = AllMilestonesOrder.MostUrgent,
): DataResult<Flow<List<MilestoneWithTasks>>> {
return repository.getAllMilestones(milestonesOrder)
suspend operator fun invoke(): DataResult<Flow<List<MilestoneWithTasks>>> {
return repository.getAllMilestones()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package com.mumbicodes.projectie.domain.use_case.milestones
import com.mumbicodes.projectie.domain.model.Milestone
import com.mumbicodes.projectie.domain.repository.MilestonesRepository

class AddMilestoneUseCase(
class InsertOrUpdateMilestoneUseCase(
private val repository: MilestonesRepository
) {
suspend operator fun invoke(milestone: Milestone) {
repository.insertMilestone(milestone)
repository.insertOrUpdateMilestone(milestone)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.mumbicodes.projectie.domain.use_case.milestones

data class MilestonesUseCases(
val addMilestoneUseCase: AddMilestoneUseCase,
val insertOrUpdateMilestoneUseCase: InsertOrUpdateMilestoneUseCase,
val getMilestoneByIdWithTasksUseCase: GetMilestoneByIdWithTasksUseCase,
val getMilestonesUseCase: GetMilestonesUseCase,
val deleteMilestoneUseCase: DeleteMilestoneUseCase,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@ 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

class GetProjectsUseCase(
private val repository: ProjectsRepository,
) {
suspend operator fun invoke(
projectOrder: ProjectsOrder = ProjectsOrder.DateAdded(OrderType.Descending),
): DataResult<Flow<List<Project>>> =
repository.getAllProjects(projectOrder = projectOrder)
suspend operator fun invoke(): DataResult<Flow<List<Project>>> =
repository.getAllProjects()
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package com.mumbicodes.projectie.domain.use_case.tasks
import com.mumbicodes.projectie.domain.model.Task
import com.mumbicodes.projectie.domain.repository.TasksRepository

class AddTasksUseCase(
class InsertOrUpdateTasksUseCase(
private val repository: TasksRepository,
) {
suspend operator fun invoke(tasks: List<Task>) {
repository.insertTask(tasks)
repository.insertOrUpdateTask(tasks)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.mumbicodes.projectie.domain.use_case.tasks

class TasksUseCases(
val addTasksUseCase: AddTasksUseCase,
val insertOrUpdateTasksUseCase: InsertOrUpdateTasksUseCase,
val deleteTaskUseCase: DeleteTaskUseCase,
val transformTasksUseCase: TransformTasksUseCase,
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.mumbicodes.projectie.domain.model.DataResult
import com.mumbicodes.projectie.domain.relations.MilestoneWithTasks
import com.mumbicodes.projectie.domain.repository.MilestonesRepository
import com.mumbicodes.projectie.domain.repository.WorkersRepository
import com.mumbicodes.projectie.domain.util.AllMilestonesOrder
import com.mumbicodes.projectie.presentation.util.toLong
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
Expand All @@ -35,7 +34,7 @@ class CheckMilestoneDeadlineWorker @AssistedInject constructor(
return withContext(Dispatchers.IO) {
return@withContext try {
Log.e("Reached 2", "It has been reached ")
val allMilestones = milestonesRepository.getAllMilestones(AllMilestonesOrder.MostUrgent)
val allMilestones = milestonesRepository.getAllMilestones()

CoroutineScope(Dispatchers.IO).launch {
when (allMilestones) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ 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
import kotlinx.coroutines.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.time.LocalDate

private const val TAG = "ProjectWorkerInTwoDays"
Expand All @@ -34,9 +37,7 @@ class CheckProjectDeadlineIsInTwoDaysWorker @AssistedInject constructor(
return@withContext try {
Log.e("Reached 3", "It has been reached - 2 days ")

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

CoroutineScope(Dispatchers.IO).launch {
when (allProjects) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ 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
import kotlinx.coroutines.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.time.LocalDate

private const val TAG = "ProjectWorkerIsToday"
Expand All @@ -30,11 +32,7 @@ class CheckProjectDeadlineWorker @AssistedInject constructor(

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

CoroutineScope(Dispatchers.IO).launch {
when (allProjects) {
Expand Down
Loading
Loading