-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move to SQLDelight for offline tag & bookmark caching
- Loading branch information
1 parent
b57775c
commit 5ae7d3d
Showing
23 changed files
with
724 additions
and
244 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
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
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
40 changes: 40 additions & 0 deletions
40
app/src/main/java/org/yrovas/linklater/data/LocalBookmark.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,40 @@ | ||
package org.yrovas.linklater.data | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
// A datatype representing bookmarks which will be sent to the remote API. | ||
@Serializable | ||
data class LocalBookmark( | ||
var url: String, | ||
var title: String? = null, | ||
var description: String? = null, | ||
var notes: String? = null, | ||
var is_archived: Boolean = false, | ||
var unread: Boolean = false, | ||
var shared: Boolean = false, | ||
@SerialName("tag_names") var tags: List<String> = emptyList(), | ||
) { | ||
/// Returns a new local bookmark with the new values | ||
fun withUpdates( | ||
url: String? = null, | ||
title: String? = null, | ||
description: String? = null, | ||
notes: String? = null, | ||
is_archived: Boolean? = null, | ||
unread: Boolean? = null, | ||
shared: Boolean? = null, | ||
tags: List<String>? = null, | ||
): LocalBookmark { | ||
return LocalBookmark( | ||
url = url?.ifBlank { null } ?: url ?: this.url, | ||
title = title?.ifBlank { null } ?: title ?: this.title, | ||
description = description?.ifBlank { null } ?: description ?: this.description, | ||
notes = notes?.ifBlank { null } ?: notes ?: this.notes, | ||
is_archived = is_archived ?: this.is_archived, | ||
unread = unread ?: this.unread, | ||
shared = shared ?: this.shared, | ||
tags = tags ?: this.tags | ||
) | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
app/src/main/java/org/yrovas/linklater/data/local/BookmarkDataSource.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,12 @@ | ||
package org.yrovas.linklater.data.local | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
import org.yrovas.linklater.data.Bookmark | ||
|
||
interface BookmarkDataSource { | ||
suspend fun getBookmark(id: Long): Bookmark? | ||
fun getBookmarks(): Flow<List<Bookmark>> | ||
suspend fun insertBookmark(bookmark: Bookmark) | ||
suspend fun insertBookmarks(bookmarks: List<Bookmark>) | ||
suspend fun deleteBookmark(id: Long) | ||
} |
78 changes: 78 additions & 0 deletions
78
app/src/main/java/org/yrovas/linklater/data/local/BookmarkDataSourceImpl.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,78 @@ | ||
package org.yrovas.linklater.data.local | ||
|
||
import android.util.Log | ||
import app.cash.sqldelight.coroutines.asFlow | ||
import app.cash.sqldelight.coroutines.mapToList | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.withContext | ||
import org.yrovas.linklater.Database | ||
import org.yrovas.linklater.data.Bookmark | ||
import org.yrovas.linklater.data.toBookmark | ||
|
||
const val TAG = "DEBUG" | ||
|
||
class BookmarkDataSourceImpl(db: Database) : BookmarkDataSource { | ||
private val q = db.bookmarkTagsQueries | ||
|
||
override suspend fun getBookmark(id: Long): Bookmark? { | ||
return withContext(Dispatchers.IO) { | ||
q.getBookmarkByIDwithTags(id).executeAsOneOrNull()?.toBookmark() | ||
} | ||
} | ||
|
||
override fun getBookmarks(): Flow<List<Bookmark>> { | ||
return q.getBookmarksWithTags().asFlow().mapToList(Dispatchers.IO).map { list -> | ||
list.map { it.toBookmark() } | ||
} | ||
} | ||
|
||
override suspend fun insertBookmarks(bookmarks: List<Bookmark>) { | ||
bookmarks.forEach { | ||
insertBookmark(it) | ||
} | ||
} | ||
|
||
override suspend fun insertBookmark(bookmark: Bookmark) { | ||
Log.d( | ||
TAG, "insertBOOKMARK: ${bookmark.title} ${bookmark.website_title}" | ||
) | ||
Log.d( | ||
TAG, "insertTAGS: ${bookmark.tags}" | ||
) | ||
withContext(Dispatchers.IO) { | ||
q.transaction { | ||
val id = q.insertBookmark( | ||
id = bookmark.id, | ||
url = bookmark.url, | ||
title = bookmark.title, | ||
description = bookmark.description, | ||
notes = bookmark.notes, | ||
website_title = bookmark.website_title, | ||
website_description = bookmark.website_description, | ||
is_archived = bookmark.is_archived, | ||
unread = bookmark.unread, | ||
shared = bookmark.shared, | ||
date_added = bookmark.date_added, | ||
date_modified = bookmark.date_modified | ||
).executeAsOneOrNull()!! | ||
bookmark.tags.forEach { | ||
q.insertTag(name = it) | ||
val tagID = q.getTagByName(it).executeAsOneOrNull()!! | ||
Log.d(TAG, "inserted TAG: $it,$tagID") | ||
q.insertTagForBookmark( | ||
bookmarkID = id, | ||
tagID = tagID, | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
override suspend fun deleteBookmark(id: Long) { | ||
withContext(Dispatchers.IO) { | ||
q.deleteBookmarkByID(id) | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
app/src/main/java/org/yrovas/linklater/data/local/EmptyBookmarkSource.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,19 @@ | ||
package org.yrovas.linklater.data.local | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.asFlow | ||
import org.yrovas.linklater.data.Bookmark | ||
|
||
class EmptyBookmarkSource : BookmarkDataSource { | ||
override suspend fun getBookmark(id: Long): Bookmark? { | ||
return null | ||
} | ||
|
||
override fun getBookmarks(): Flow<List<Bookmark>> { | ||
return emptyList<List<Bookmark>>().asFlow() | ||
} | ||
|
||
override suspend fun insertBookmark(bookmark: Bookmark) {} | ||
override suspend fun insertBookmarks(bookmarks: List<Bookmark>) {} | ||
override suspend fun deleteBookmark(id: Long) {} | ||
} |
12 changes: 12 additions & 0 deletions
12
app/src/main/java/org/yrovas/linklater/data/local/TagDataSource.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,12 @@ | ||
package org.yrovas.linklater.data.local | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
import org.yrovas.linklater.data.Bookmark | ||
|
||
interface TagDataSource { | ||
suspend fun getTag(id: Long): String? | ||
fun getTags(): Flow<List<String>> | ||
// suspend fun insertTag(name: String) | ||
// suspend fun insertTags(names: List<String>) | ||
// suspend fun deleteTag(id: Long) | ||
} |
Oops, something went wrong.