Skip to content

Commit 240fd86

Browse files
committed
Added delete feature to the app
1 parent 2cb446e commit 240fd86

File tree

40 files changed

+1235
-283
lines changed

40 files changed

+1235
-283
lines changed

common/data/local/src/main/java/com/viplearner/common/data/local/dao/NotesDao.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import kotlinx.coroutines.flow.Flow
88

99
@Dao
1010
interface NotesDao {
11-
@Query("SELECT * from Notes")
11+
@Query("SELECT * from Notes" +
12+
" ORDER BY timeLastEdited DESC")
1213
fun getAll(): Flow<List<Note>>
1314

1415
@Query("SELECT * " +

common/presentation/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ dependencies {
4949
implementation(libs.lifecycle.runtime.ktx)
5050
implementation(platform(libs.compose.bom))
5151
implementation(libs.ui)
52+
implementation(libs.localizationManager)
5253
implementation(libs.ui.graphics)
5354
implementation(libs.ui.tooling.preview)
5455
implementation(libs.material3)

common/presentation/src/main/java/com/viplearner/common/presentation/component/Template.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import androidx.compose.ui.unit.dp
1414
@Composable
1515
fun Template(
1616
modifier: Modifier = Modifier,
17+
bottomBar: @Composable () -> Unit = {},
1718
topBar: @Composable () -> Unit = {},
1819
floatingActionButton: @Composable () -> Unit = {},
1920
content: @Composable (PaddingValues) -> Unit
@@ -26,6 +27,7 @@ fun Template(
2627
Scaffold(
2728
content = content,
2829
topBar = topBar,
30+
bottomBar = bottomBar,
2931
floatingActionButton = floatingActionButton,
3032
modifier = Modifier
3133
.fillMaxSize()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.viplearner.common.presentation.util
2+
3+
import androidx.compose.runtime.Composable
4+
import androidx.compose.runtime.remember
5+
import androidx.compose.ui.platform.LocalContext
6+
import com.eemmez.localization.LocalizationManager
7+
8+
@Composable
9+
public fun rememberLocalizationManager(): LocalizationManager {
10+
val context = LocalContext.current
11+
return remember {
12+
LocalizationManager(context)
13+
}
14+
}

feature/home/data/src/main/java/com/viplearner/feature/home/data/repository/HomeRepositoryImpl.kt

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
package com.viplearner.feature.home.data.repository
22

3+
import com.viplearner.common.data.local.mapper.toNoteEntity
34
import com.viplearner.common.data.remote.di.IoDispatcher
45
import com.viplearner.common.domain.Result
5-
import com.viplearner.common.data.local.mapper.toNoteEntity
6+
import com.viplearner.common.domain.entity.NoteEntity
67
import com.viplearner.feature.home.data.service.HomeService
7-
import com.viplearner.feature.home.domain.entity.GetNoteEntityListResponse
88
import com.viplearner.feature.home.domain.entity.HomeError
9-
import com.viplearner.common.domain.entity.NoteEntity
109
import com.viplearner.feature.home.domain.repository.HomeRepository
1110
import kotlinx.coroutines.CoroutineDispatcher
11+
import kotlinx.coroutines.ExperimentalCoroutinesApi
1212
import kotlinx.coroutines.flow.Flow
1313
import kotlinx.coroutines.flow.catch
14-
import kotlinx.coroutines.flow.collectLatest
14+
import kotlinx.coroutines.flow.flatMapConcat
15+
import kotlinx.coroutines.flow.flow
1516
import kotlinx.coroutines.flow.flowOn
1617
import kotlinx.coroutines.flow.onStart
1718
import javax.inject.Inject
@@ -20,24 +21,39 @@ class HomeRepositoryImpl @Inject constructor(
2021
private val homeService: HomeService,
2122
@IoDispatcher private val ioDispatcher: CoroutineDispatcher
2223
) : HomeRepository {
23-
override suspend fun getList(searchText: String, result: (Result<GetNoteEntityListResponse, HomeError>) -> Unit){
24-
homeService
25-
.getListBySearchText(searchText)
26-
.onStart {
27-
result(Result.Loading())
24+
@OptIn(ExperimentalCoroutinesApi::class)
25+
override suspend fun getListBySearchText(searchText: String): Flow<Result<List<NoteEntity>, HomeError>> =
26+
homeService.getListBySearchText(searchText).flatMapConcat { list ->
27+
flow<Result<List<NoteEntity>, HomeError>> {
28+
emit(Result.Success(list.map { it.toNoteEntity() }))
29+
}.onStart {
30+
emit(Result.Loading())
2831
}.catch {
29-
result(Result.Error(HomeError.AddNoteError))
30-
}.flowOn(ioDispatcher)
31-
.collectLatest {
32-
result(
33-
Result.Success(
34-
GetNoteEntityListResponse(
35-
list = it.map { note -> note.toNoteEntity() }
36-
)
37-
)
38-
)
32+
emit(Result.Error(HomeError.GetListBySearchTextError))
3933
}
40-
}
34+
}
35+
36+
@OptIn(ExperimentalCoroutinesApi::class)
37+
override suspend fun getList(): Flow<Result<List<NoteEntity>, HomeError>> =
38+
homeService.getList().flatMapConcat { list ->
39+
flow<Result<List<NoteEntity>, HomeError>> {
40+
emit(Result.Success(list.map { it.toNoteEntity() }))
41+
}.onStart {
42+
emit(Result.Loading())
43+
}.catch {
44+
emit(Result.Error(HomeError.GetListError))
45+
}
46+
}
47+
48+
override suspend fun deleteNotes(uuidList: List<String>): Flow<Result<Unit, HomeError>> =
49+
flow<Result<Unit, HomeError>> {
50+
homeService.deleteNotes(uuidList)
51+
emit(Result.Success(Unit))
52+
}.onStart {
53+
emit(Result.Loading())
54+
}.catch {
55+
emit(Result.Error(HomeError.DeleteNoteError))
56+
}.flowOn(ioDispatcher)
4157

4258
override suspend fun addNote(noteEntity: NoteEntity): Flow<Result<Unit, HomeError>> {
4359
TODO("Not yet implemented")

feature/home/data/src/main/java/com/viplearner/feature/home/data/service/HomeService.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ class HomeService @Inject constructor(
1717
): Flow<List<Note>> =
1818
notesDatabase.notesDao().getNotesBySearchText(searchText)
1919

20+
fun deleteNotes(
21+
uuidList: List<String>,
22+
) = uuidList.forEach{ uuid -> notesDatabase.notesDao().delete(uuid) }
23+
2024
fun addNote(note: Note) {
2125
notesDatabase.notesDao().upsert(note)
2226
}

feature/home/domain/src/main/java/com/viplearner/feature/home/domain/entity/HomeError.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package com.viplearner.feature.home.domain.entity
22

33
sealed class HomeError {
44
data object GetListError : HomeError()
5+
data object GetListBySearchTextError : HomeError()
56
data object AddNoteError : HomeError()
7+
data object DeleteNoteError : HomeError()
68
data object UnknownError : HomeError()
79
}

feature/home/domain/src/main/java/com/viplearner/feature/home/domain/repository/HomeRepository.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import com.viplearner.common.domain.entity.NoteEntity
77
import kotlinx.coroutines.flow.Flow
88

99
interface HomeRepository {
10-
suspend fun getList(searchText: String, result: (Result<GetNoteEntityListResponse, HomeError>) -> Unit)
10+
suspend fun getListBySearchText(searchText: String): Flow <Result<List<NoteEntity>, HomeError>>
11+
suspend fun getList(): Flow <Result<List<NoteEntity>, HomeError>>
12+
suspend fun deleteNotes(uuidList: List<String>): Flow<Result<Unit, HomeError>>
1113
suspend fun addNote(noteEntity: NoteEntity): Flow<Result<Unit, HomeError>>
1214
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.viplearner.feature.home.domain.usecase
2+
3+
import com.viplearner.feature.home.domain.repository.HomeRepository
4+
import javax.inject.Inject
5+
6+
class DeleteNoteUseCase @Inject constructor(
7+
private val homeRepository: HomeRepository
8+
) {
9+
suspend operator fun invoke(uuidList: List<String>)=
10+
homeRepository.deleteNotes(uuidList)
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.viplearner.feature.home.domain.usecase
2+
3+
import com.viplearner.feature.home.domain.repository.HomeRepository
4+
import javax.inject.Inject
5+
6+
class GetListBySearchTextUseCase @Inject constructor(
7+
private val homeRepository: HomeRepository
8+
) {
9+
suspend operator fun invoke(searchText: String) =
10+
homeRepository.getListBySearchText(searchText)
11+
}

0 commit comments

Comments
 (0)