Skip to content

Commit

Permalink
use event -> effect model for home screen
Browse files Browse the repository at this point in the history
  • Loading branch information
danielyrovas committed Apr 20, 2024
1 parent 15d7a4b commit 9e80b2e
Show file tree
Hide file tree
Showing 22 changed files with 478 additions and 294 deletions.
42 changes: 32 additions & 10 deletions app/src/main/java/org/yrovas/linklater/AppComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,25 @@ import kotlinx.serialization.json.Json
import me.tatarka.inject.annotations.Component
import me.tatarka.inject.annotations.Provides
import me.tatarka.inject.annotations.Scope
import org.yrovas.linklater.data.local.BookmarkDataSource
import org.yrovas.linklater.data.local.BookmarkDataSourceImpl
import org.yrovas.linklater.data.local.PrefDataStore
import org.yrovas.linklater.data.local.PrefStore
import org.yrovas.linklater.data.local.TagDataSource
import org.yrovas.linklater.data.local.TagDataSourceImpl
import org.yrovas.linklater.data.remote.BookmarkAPI
import org.yrovas.linklater.data.remote.LinkDingAPI
import org.yrovas.linklater.ui.activity.DestinationHost
import org.yrovas.linklater.domain.BookmarkAPI
import org.yrovas.linklater.domain.BookmarkDataSource
import org.yrovas.linklater.domain.TagDataSource
import org.yrovas.linklater.ui.common.DestinationHost

const val TAG = "DEBUG/create"
val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "preferences")

@Scope
@Target(
AnnotationTarget.CLASS,
AnnotationTarget.FUNCTION,
AnnotationTarget.PROPERTY_GETTER
)
annotation class AppScope

@Component
Expand All @@ -47,14 +53,19 @@ abstract class AppComponent(
abstract val prefStore: PrefDataStore

val store: DataStore<Preferences>
@Provides get() = context.dataStore
@AppScope @Provides get() = context.dataStore

@AppScope
@Provides
fun providePrefStore(store: DataStore<Preferences>): PrefDataStore =
PrefStore(store)
fun providePrefStore(store: DataStore<Preferences>): PrefDataStore {
Log.d(TAG, "providePrefStore: CREATE")
return PrefStore(store)
}

@AppScope
@Provides
fun provideHttpClient(): HttpClient = HttpClient(Android) {
Log.d(TAG, "provideHttpClient: CREATE")
install(Logging) {
logger = object : Logger {
override fun log(message: String) {
Expand All @@ -77,28 +88,39 @@ abstract class AppComponent(

abstract val linkDingAPI: LinkDingAPI
val bookmarkAPI: BookmarkAPI
@AppScope
@Provides get() = linkDingAPI

abstract val bookmarkDataSource: BookmarkDataSource

@AppScope
@Provides
fun provideBookmarkDataSource(db: Database): BookmarkDataSource =
BookmarkDataSourceImpl(db)

@AppScope
@Provides
fun provideTagDataSource(db: Database): TagDataSource = TagDataSourceImpl(db)

@AppScope
@Provides
fun provideSQLDriver(context: Context): SqlDriver =
AndroidSqliteDriver(schema = Database.Schema,
fun provideSQLDriver(context: Context): SqlDriver {
Log.d(TAG, "provideSQLDriver: CREATE")
return AndroidSqliteDriver(
schema = Database.Schema,
context = context,
name = "linklater.db",
callback = object : AndroidSqliteDriver.Callback(Database.Schema) {
override fun onOpen(db: SupportSQLiteDatabase) {
db.setForeignKeyConstraintsEnabled(true)
}
})
}

@AppScope
@Provides
fun provideDB(driver: SqlDriver): Database = Database(driver)
fun provideDB(driver: SqlDriver): Database {
Log.d(TAG, "provideDB: CREATE")
return Database(driver)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@ package org.yrovas.linklater.data.local
import android.util.Log
import app.cash.sqldelight.coroutines.asFlow
import app.cash.sqldelight.coroutines.mapToList
import app.cash.sqldelight.coroutines.mapToOne
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
import org.yrovas.linklater.domain.BookmarkDataSource
import kotlin.math.log

const val TAG = "DEBUG"

class BookmarkDataSourceImpl(db: Database) : BookmarkDataSource {
init {
Log.d("DEBUG/create", "BookmarkDataSourceImpl: CREATE")
}
private val q = db.bookmarkTagsQueries

override suspend fun getBookmark(id: Long): Bookmark? {
Expand All @@ -29,9 +35,36 @@ class BookmarkDataSourceImpl(db: Database) : BookmarkDataSource {
}
}

override fun getBookmarkCount(): Int {
return q.countBookmarks().executeAsOneOrNull()?.toInt() ?: -1
// .map { Log.d(TAG, "getBookmarkCount: EMIT $it"); it.toInt() }
}

override suspend fun insertBookmarks(bookmarks: List<Bookmark>) {
bookmarks.forEach {
insertBookmark(it)
withContext(Dispatchers.IO) {
q.transaction {
bookmarks.forEach { bookmark ->
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()!!
q.insertTagForBookmark(bookmarkID = id, tagID = tagID)
}
}
}
}
}

Expand Down Expand Up @@ -62,10 +95,7 @@ class BookmarkDataSourceImpl(db: Database) : BookmarkDataSource {
q.insertTag(name = it)
val tagID = q.getTagByName(it).executeAsOneOrNull()!!
Log.d(TAG, "inserted TAG: $it,$tagID")
q.insertTagForBookmark(
bookmarkID = id,
tagID = tagID,
)
q.insertTagForBookmark(bookmarkID = id, tagID = tagID)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package org.yrovas.linklater.data.local

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.flow
import org.yrovas.linklater.data.Bookmark
import org.yrovas.linklater.domain.BookmarkDataSource

class EmptyBookmarkSource : BookmarkDataSource {
override suspend fun getBookmark(id: Long): Bookmark? {
Expand All @@ -13,6 +15,10 @@ class EmptyBookmarkSource : BookmarkDataSource {
return emptyList<List<Bookmark>>().asFlow()
}

override fun getBookmarkCount(): Int {
return 0
}

override suspend fun insertBookmark(bookmark: Bookmark) {}
override suspend fun insertBookmarks(bookmarks: List<Bookmark>) {}
override suspend fun deleteBookmark(id: Long) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package org.yrovas.linklater.data.local

import androidx.datastore.preferences.core.Preferences
import kotlinx.coroutines.flow.Flow
import org.yrovas.linklater.AppScope

@AppScope
interface PrefDataStore {
suspend fun <T> getPrefs(key: Preferences.Key<T>, default: T): Flow<T>
suspend fun <T> getPref(key: Preferences.Key<T>, default: T): T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.withContext
import org.yrovas.linklater.Database
import org.yrovas.linklater.domain.TagDataSource

class TagDataSourceImpl(db: Database) : TagDataSource {
private val q = db.bookmarkTagsQueries
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
package org.yrovas.linklater.data.remote

import android.content.Context
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import org.yrovas.linklater.data.Bookmark
import org.yrovas.linklater.data.LocalBookmark
import org.yrovas.linklater.domain.APIError
import org.yrovas.linklater.domain.BookmarkAPI
import org.yrovas.linklater.domain.Err
import org.yrovas.linklater.domain.Ok
import org.yrovas.linklater.domain.Res

class EmptyBookmarkAPI : BookmarkAPI {
override suspend fun authenticate(
override fun authenticate(
endpoint: String?,
token: String?,
validate: Boolean,
): Res<Unit, APIError> {
return Ok(Unit)
}

override suspend fun checkConnection(): Res<Unit, APIError> {
return Ok(Unit)
}

override val authProvided: StateFlow<Boolean>
get() = MutableStateFlow(false)

override suspend fun getBookmarks(
page: Int,
query: String?,
Expand All @@ -28,7 +36,7 @@ class EmptyBookmarkAPI : BookmarkAPI {
}

override suspend fun saveBookmark(bookmark: LocalBookmark): Res<Bookmark, APIError> {
return Err(APIError.CONNECTION)
return Err(APIError.NO_CONNECTION)
}

override suspend fun getTags(page: Int): Res<List<String>, APIError> {
Expand Down
Loading

0 comments on commit 9e80b2e

Please sign in to comment.