Clean some duplicate code.

This commit is contained in:
Thomas Andres Gomez 2022-10-18 15:04:30 +02:00
parent 46a1f568f1
commit c864c4b8e4
11 changed files with 121 additions and 126 deletions

View file

@ -0,0 +1,16 @@
package com.pixelized.biblib.database.factory
import com.pixelized.biblib.database.data.AuthorDbo
import com.pixelized.biblib.model.book.Author
fun AuthorDbo.toAuthor() = Author(
id = id,
name = name,
sort = sort,
)
fun Author.toDbo() = AuthorDbo(
id = id,
name = name,
sort = sort,
)

View file

@ -0,0 +1,36 @@
package com.pixelized.biblib.database.factory
import com.pixelized.biblib.database.data.BookDbo
import com.pixelized.biblib.database.relation.BookRelation
import com.pixelized.biblib.model.book.Book
fun BookRelation.toBook(): Book = Book(
id = book.id,
title = book.title,
sort = book.sort,
author = authors.map { it.toAuthor() },
haveCover = book.haveCover,
releaseDate = book.releaseDate,
language = language?.toLanguage(),
rating = book.rating,
genre = genres?.map { it.toGenre() },
series = series?.toSeries(),
seriesIndex = book.seriesIndex,
synopsis = book.synopsis,
isNew = book.isNew,
)
fun Book.toDbo() = BookDbo(
id = id,
title = title,
sort = sort,
haveCover = haveCover,
releaseDate = releaseDate,
language = language?.id,
rating = rating,
series = series?.id,
seriesIndex = seriesIndex,
synopsis = synopsis,
isNew = isNew,
newOrder = newOrder,
)

View file

@ -0,0 +1,14 @@
package com.pixelized.biblib.database.factory
import com.pixelized.biblib.database.data.GenreDbo
import com.pixelized.biblib.model.book.Genre
fun GenreDbo.toGenre() = Genre(
id = id,
name = name,
)
fun Genre.toDbo() = GenreDbo(
id = id,
name = name,
)

View file

@ -0,0 +1,14 @@
package com.pixelized.biblib.database.factory
import com.pixelized.biblib.database.data.LanguageDbo
import com.pixelized.biblib.model.book.Language
fun LanguageDbo.toLanguage() = Language(
id = id,
code = code,
)
fun Language.toDbo() = LanguageDbo(
id = id,
code = code
)

View file

@ -0,0 +1,16 @@
package com.pixelized.biblib.database.factory
import com.pixelized.biblib.database.data.SeriesDbo
import com.pixelized.biblib.model.book.Series
fun SeriesDbo.toSeries() = Series(
id = id,
name = name,
sort = sort,
)
fun Series.toDbo(id: Int) = SeriesDbo(
id = id,
name = name,
sort = sort,
)

View file

@ -6,6 +6,7 @@ import com.google.gson.GsonBuilder
import com.pixelized.biblib.network.client.BibLibClient
import com.pixelized.biblib.network.client.IBibLibClient
import com.pixelized.biblib.repository.connectivity.ConnectivityRepository
import com.pixelized.biblib.repository.connectivity.IConnectivityRepository
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
@ -37,7 +38,7 @@ class NetworkModule {
@Singleton
fun provideConnectivityRepository(
@ApplicationContext context: Context,
): ConnectivityRepository {
): IConnectivityRepository {
return ConnectivityRepository(context)
}
}

View file

@ -6,8 +6,8 @@ import com.pixelized.biblib.database.BibLibDatabase
import com.pixelized.biblib.database.crossref.BookAuthorCrossRef
import com.pixelized.biblib.database.crossref.BookGenreCrossRef
import com.pixelized.biblib.database.data.*
import com.pixelized.biblib.database.relation.BookRelation
import com.pixelized.biblib.model.book.*
import com.pixelized.biblib.database.factory.*
import javax.inject.Inject
class BookRepository @Inject constructor(
@ -75,79 +75,4 @@ class BookRepository @Inject constructor(
database.crossRefDao().insert(*bookAuthorCrossRef.toTypedArray())
database.crossRefDao().insert(*bookGenreCrossRef.toTypedArray())
}
private fun Author.toDbo() = AuthorDbo(
id = id,
name = name,
sort = sort,
)
private fun Genre.toDbo() = GenreDbo(
id = id,
name = name,
)
private fun Series.toDbo(id: Int) = SeriesDbo(
id = id,
name = name,
sort = sort,
)
private fun Language.toDbo() = LanguageDbo(
id = id,
code = code
)
private fun Book.toDbo() = BookDbo(
id = id,
title = title,
sort = sort,
haveCover = haveCover,
releaseDate = releaseDate,
language = language?.id,
rating = rating,
series = series?.id,
seriesIndex = seriesIndex,
synopsis = synopsis,
isNew = isNew,
newOrder = newOrder,
)
private fun BookRelation.toBook(): Book = Book(
id = book.id,
title = book.title,
sort = book.sort,
author = authors.map { it.toAuthor() },
haveCover = book.haveCover,
releaseDate = book.releaseDate,
language = language?.toLanguage(),
rating = book.rating,
genre = genres?.map { it.toGenre() },
series = series?.toSeries(),
seriesIndex = book.seriesIndex,
synopsis = book.synopsis,
isNew = book.isNew,
)
private fun AuthorDbo.toAuthor() = Author(
id = id,
name = name,
sort = sort,
)
private fun LanguageDbo.toLanguage() = Language(
id = id,
code = code,
)
private fun GenreDbo.toGenre() = Genre(
id = id,
name = name,
)
private fun SeriesDbo.toSeries() = Series(
id = id,
name = name,
sort = sort,
)
}

View file

@ -16,12 +16,11 @@ import kotlinx.coroutines.flow.MutableStateFlow
*/
class ConnectivityRepository constructor(
@ApplicationContext context: Context,
) {
) : IConnectivityRepository {
private val _networkAvailabilityFlow = MutableStateFlow(true)
val networkAvailabilityFlow: Flow<Boolean> get() = _networkAvailabilityFlow
val isNetworkAvailable: Boolean get() = _networkAvailabilityFlow.value
override val networkAvailabilityFlow: Flow<Boolean> get() = _networkAvailabilityFlow
override val isNetworkAvailable: Boolean get() = _networkAvailabilityFlow.value
init {
context.getSystemService<ConnectivityManager>()?.let { connectivityManager ->

View file

@ -0,0 +1,14 @@
package com.pixelized.biblib.repository.connectivity
import kotlinx.coroutines.flow.Flow
/**
* This class is responsible to check for internet availability.
* https://developer.android.com/training/monitoring-device-state/connectivity-status-type
*/
interface IConnectivityRepository {
val networkAvailabilityFlow: Flow<Boolean>
val isNetworkAvailable: Boolean
}

View file

@ -5,9 +5,9 @@ import com.pixelized.biblib.database.BibLibDatabase
import com.pixelized.biblib.database.crossref.BookAuthorCrossRef
import com.pixelized.biblib.database.crossref.BookGenreCrossRef
import com.pixelized.biblib.database.data.*
import com.pixelized.biblib.database.relation.BookRelation
import com.pixelized.biblib.model.book.*
import com.pixelized.biblib.model.search.SortType
import com.pixelized.biblib.database.factory.*
import javax.inject.Inject
class SearchRepository @Inject constructor(
@ -20,7 +20,7 @@ class SearchRepository @Inject constructor(
seriesId: Int?,
genreId: Int?,
languageId: Int?,
sortBy : SortType,
sortBy: SortType,
limit: Int,
offset: Int
): List<Book> {
@ -132,44 +132,4 @@ private fun MutableList<Any>.where(
} else {
""
}
}
// TODO Duplicate
private fun BookRelation.toBook(): Book = Book(
id = book.id,
title = book.title,
sort = book.sort,
author = authors.map { it.toAuthor() },
haveCover = book.haveCover,
releaseDate = book.releaseDate,
language = language?.toLanguage(),
rating = book.rating,
genre = genres?.map { it.toGenre() },
series = series?.toSeries(),
seriesIndex = book.seriesIndex,
synopsis = book.synopsis,
isNew = book.isNew,
)
private fun AuthorDbo.toAuthor() = Author(
id = id,
name = name,
sort = sort,
)
private fun LanguageDbo.toLanguage() = Language(
id = id,
code = code,
)
private fun GenreDbo.toGenre() = Genre(
id = id,
name = name,
)
private fun SeriesDbo.toSeries() = Series(
id = id,
name = name,
sort = sort,
)
}

View file

@ -4,14 +4,14 @@ import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.pixelized.biblib.repository.connectivity.ConnectivityRepository
import com.pixelized.biblib.repository.connectivity.IConnectivityRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject
@HiltViewModel
class ConnectivityViewModel @Inject constructor(
private val connectivityRepository: ConnectivityRepository,
private val connectivityRepository: IConnectivityRepository,
) : ViewModel() {
private val _isNetworkAvailable = mutableStateOf(connectivityRepository.isNetworkAvailable)