Skip to content

Commit d2ca43a

Browse files
committed
feat: Add import and export option
1 parent 5947b27 commit d2ca43a

File tree

16 files changed

+75
-31
lines changed

16 files changed

+75
-31
lines changed

.idea/workspace.xml

Lines changed: 2 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/java/com/opennotes/feature_node/data/data_source/NoteDatabase.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import com.opennotes.feature_node.domain.model.Note
77

88
@Database(
99
entities=[Note::class],
10-
version=1
10+
version=2
1111
)
1212
abstract class NoteDatabase :RoomDatabase(){
1313
abstract val noteDao: NoteDao

app/src/main/java/com/opennotes/feature_node/data/repository/FileHandler.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ interface FileHandler{
1616
suspend fun saveToFile(filename:String,content:String):Uri?
1717

1818

19-
2019
}
2120

2221

@@ -30,9 +29,18 @@ class AndroidFileHandler(private val application: Application):FileHandler{
3029

3130
override suspend fun saveToFile(filename:String,content:String):Uri?=withContext(Dispatchers.IO)
3231
{
33-
val notesDir= File(application.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS),"notes_backup")
34-
if(!notesDir.exists()) notesDir.mkdirs()
32+
val notesDir = application.getExternalFilesDir(null)
33+
34+
35+
if(notesDir==null){
36+
return@withContext null
37+
}
38+
39+
3540

41+
if(!notesDir.exists() && !notesDir.mkdirs()){
42+
return@withContext null
43+
}
3644

3745
val file=File(notesDir,filename)
3846
file.writeText(content)

app/src/main/java/com/opennotes/feature_node/data/repository/JsonHandler.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.opennotes.feature_node.data.repository
22

33
import com.google.gson.Gson
4+
import com.google.gson.GsonBuilder
45

56
// In your data/repository directory
67

@@ -13,7 +14,7 @@ interface JsonHandler {
1314
}
1415

1516
class GsonJsonHandler : JsonHandler {
16-
private val gson = Gson()
17+
private val gson = GsonBuilder().excludeFieldsWithoutExposeAnnotation().create()
1718

1819
override fun <T> toJson(data: T): String {
1920
return gson.toJson(data)

app/src/main/java/com/opennotes/feature_node/data/repository/NoteRepositoryImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ private val dao: NoteDao
1111

1212

1313
{
14-
override fun getNotes(): Flow<List<Note>> {
14+
override fun getAllNotes(): Flow<List<Note>> {
1515
return dao.getNotes()
1616
}
1717

app/src/main/java/com/opennotes/feature_node/domain/model/Note.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.opennotes.feature_node.domain.model
22

33
import androidx.room.Entity
44
import androidx.room.PrimaryKey
5+
import com.google.gson.annotations.Expose
56
import com.opennotes.ui.theme.BabyBlue
67
import com.opennotes.ui.theme.LightGreen
78

@@ -11,10 +12,10 @@ import com.opennotes.ui.theme.Violet
1112

1213
@Entity
1314
data class Note(
14-
val title:String,
15-
val content:String,
16-
val timestamp: String,
17-
val color:Int,
15+
@Expose val title:String,
16+
@Expose val content:String,
17+
@Expose val timestamp: Long,
18+
@Expose val color:Int,
1819
@PrimaryKey val id:Int?=null
1920
){
2021
companion object{

app/src/main/java/com/opennotes/feature_node/domain/repository/NoteRepository.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import com.opennotes.feature_node.domain.model.Note
55
import kotlinx.coroutines.flow.Flow
66

77
interface NoteRepository {
8-
fun getNotes():Flow<List<Note>>
8+
fun getAllNotes():Flow<List<Note>>
99
suspend fun getNodeById(id:Int): Note?
1010
suspend fun insertNote(note: Note)
1111
suspend fun deleteNote(note: Note)

app/src/main/java/com/opennotes/feature_node/domain/use_case/ExportUseCases.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.opennotes.feature_node.domain.use_case
22

3+
import android.util.Log
34
import com.opennotes.feature_node.data.repository.FileHandler
45
import com.opennotes.feature_node.data.repository.GsonJsonHandler
56
import com.opennotes.feature_node.data.repository.JsonHandler
67

78
import com.opennotes.feature_node.domain.repository.NoteRepository
89
import com.opennotes.feature_node.domain.util.ExportResult
10+
import kotlinx.coroutines.flow.filter
11+
import kotlinx.coroutines.flow.first
912

1013
class ExportUseCases(
1114
private val repository: NoteRepository,
@@ -14,13 +17,12 @@ class ExportUseCases(
1417
) {
1518
suspend operator fun invoke(): ExportResult {
1619
return try {
20+
val allNotes = repository.getAllNotes()
21+
.filter{it.isNotEmpty()}
22+
.first()
1723

1824

1925

20-
21-
val allNotes = repository.getNotes()
22-
23-
2426
val notesJson = jsonHandler.toJson(allNotes)
2527

2628

@@ -31,7 +33,7 @@ class ExportUseCases(
3133
return ExportResult.Error("Could not save the file to a URI")
3234
}
3335

34-
ExportResult.Success
36+
ExportResult.Success(fileUri)
3537
} catch (e: Exception) {
3638
ExportResult.Error("Failed to export notes: ${e.message}")
3739
}

app/src/main/java/com/opennotes/feature_node/domain/use_case/GetNotes.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class GetNotes(
1313
operator fun invoke(
1414
noteOrder: NoteOrder = NoteOrder.Date(OrderType.Descending)
1515
): Flow<List<Note>> {
16-
return repsoitory.getNotes().map{ notes->
16+
return repsoitory.getAllNotes().map{ notes->
1717
when(noteOrder.orderType){
1818
is OrderType.Ascending-> {
1919
when (noteOrder) {

app/src/main/java/com/opennotes/feature_node/domain/use_case/SearchNotesUseCase.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import com.opennotes.feature_node.domain.repository.NoteRepository
55
import kotlinx.coroutines.flow.Flow
66
import javax.inject.Inject
77

8+
9+
10+
11+
812
class SearchNotesUseCase @Inject constructor(
913
private val repository: NoteRepository
1014
) {

0 commit comments

Comments
 (0)