Skip to content

Commit

Permalink
Merge pull request #231 from camina-apps/code_improvements
Browse files Browse the repository at this point in the history
Code improvements
  • Loading branch information
carmen-gh authored Apr 6, 2023
2 parents 4b8239b + 2286579 commit e60aaa1
Show file tree
Hide file tree
Showing 34 changed files with 243 additions and 114 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 45 additions & 29 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ plugins {

android {
namespace = "com.caminaapps.bookworm"
compileSdk = 33
compileSdk = libs.versions.compileSdk.get().toInt()

defaultConfig {
applicationId = "com.caminaapps.bookworm"
minSdk = 28
targetSdk = 33
minSdk = libs.versions.minSdk.get().toInt()
targetSdk = libs.versions.targetSdk.get().toInt()
versionCode = 1
versionName = "1.0"

Expand Down Expand Up @@ -123,6 +123,7 @@ dependencies {
implementation(libs.firebase.crashlytics)
implementation(libs.hilt.android)
implementation(libs.hilt.navigation.compose)
implementation(libs.kotlinx.coroutines.android)
implementation(libs.kotlinx.datetime)
implementation(libs.kotlinx.serialization.json)
implementation(libs.lifecycle.compose)
Expand Down Expand Up @@ -169,6 +170,9 @@ dependencies {

detektPlugins(libs.detekt.compose.plugin)
detektPlugins(libs.detekt.formatting.plugin)

testImplementation(project(":testing"))
androidTestImplementation(project(":testing"))
}

detekt {
Expand All @@ -184,33 +188,45 @@ tasks.detekt.configure {
}
}

kover {
instrumentation {
excludeTasks.add("testReleaseUnitTest")
}

filters {
classes {
excludes += listOf(
"dagger.hilt.internal.aggregatedroot.codegen.*",
"hilt_aggregated_deps.*",
"com.caminaapps.bookworm.di.*",
"com.caminaapps.bookworm.core.ui.theme.*",
"com.caminaapps.bookworm.core.ui.icon.*",
"com.caminaapps.bookworm.util.previewParameterProvider.*",
"com.caminaapps.bookworm.*.*_Impl*",
"com.caminaapps.bookworm.*.*_Factory*",
"*CrashlyticsLogging",
"*CoroutineScopeExt*",
"*BookwormDispatchers",
"*ComposableSingletons*",
"*_HiltModules*",
"*Hilt_*",
"*BuildConfig",
".*_Factory.*",
)
koverAndroid {
report("release") {
filters {
excludes {
classes(
"*Fragment",
"*Fragment\$*",
"*Activity",
"*Activity\$*",
"*.databinding.*",
"*.BuildConfig",
"dagger.hilt.internal.aggregatedroot.codegen.*",
"hilt_aggregated_deps.*",
"com.caminaapps.bookworm.di.*",
"com.caminaapps.bookworm.core.ui.theme.*",
"com.caminaapps.bookworm.core.ui.icon.*",
"com.caminaapps.bookworm.util.previewParameterProvider.*",
"com.caminaapps.bookworm.*.*_Impl*",
"com.caminaapps.bookworm.*.*_Factory*",
"*CrashlyticsLogging",
"*CoroutineScopeExt*",
"*BookwormDispatchers",
"*ComposableSingletons*",
"*_HiltModules*",
"*Hilt_*",
"*BuildConfig",
".*_Factory.*",
"*_MembersInjector",
"*AppDatabase"
)
annotatedBy("*Composable*")
}
}
html {
onCheck = true
}
verify {
onCheck = true
}

}
}

Expand Down
42 changes: 29 additions & 13 deletions app/src/androidTest/java/com/caminaapps/bookworm/BookDaoTest.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.caminaapps.bookworm

import android.content.Context
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import androidx.test.core.app.ApplicationProvider.getApplicationContext
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import assertk.assertThat
import assertk.assertions.containsExactly
import assertk.assertions.isEqualTo
Expand All @@ -11,34 +12,38 @@ import assertk.assertions.isNotZero
import assertk.assertions.isNull
import assertk.assertions.isZero
import com.caminaapps.bookworm.core.data.database.AppDatabase
import com.caminaapps.bookworm.core.data.database.BookDao
import com.caminaapps.bookworm.core.data.database.BookEntity
import com.caminaapps.bookworm.core.data.database.toBook
import com.caminaapps.bookworm.testing.MainDispatcherRule
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest
import kotlinx.datetime.LocalDate
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@OptIn(ExperimentalCoroutinesApi::class)
@RunWith(AndroidJUnit4::class)
@SmallTest
class BookDaoTest {

private lateinit var bookDao: BookDao
private lateinit var db: AppDatabase
@ExperimentalCoroutinesApi
@get:Rule
val mainCoroutineRule = MainDispatcherRule()

private val database = Room.inMemoryDatabaseBuilder(
getApplicationContext(),
AppDatabase::class.java
).allowMainThreadQueries().build()

@Before
fun createDb() {
val context = ApplicationProvider.getApplicationContext<Context>()
db = Room.inMemoryDatabaseBuilder(
context,
AppDatabase::class.java
).build()
bookDao = db.bookDao()
}
fun initDb() = database.clearAllTables()

@Test
fun bookDao_fetches_books_by_date_asc() = runTest {
val bookDao = database.bookDao()
val bookEntities = listOf(
testBookEntity(
id = "0",
Expand Down Expand Up @@ -76,6 +81,7 @@ class BookDaoTest {

@Test
fun bookDao_fetches_books_by_date_desc() = runTest {
val bookDao = database.bookDao()
val bookEntities = listOf(
testBookEntity(
id = "0",
Expand Down Expand Up @@ -113,6 +119,7 @@ class BookDaoTest {

@Test
fun bookDao_fetches_books_by_author_asc() = runTest {
val bookDao = database.bookDao()
val bookEntities = listOf(
testBookEntity(
id = "0",
Expand Down Expand Up @@ -150,6 +157,7 @@ class BookDaoTest {

@Test
fun bookDao_fetches_books_by_author_desc() = runTest {
val bookDao = database.bookDao()
val bookEntities = listOf(
testBookEntity(
id = "0",
Expand Down Expand Up @@ -187,6 +195,7 @@ class BookDaoTest {

@Test
fun bookDao_fetches_books_by_title_asc() = runTest {
val bookDao = database.bookDao()
val bookEntities = listOf(
testBookEntity(
id = "0",
Expand Down Expand Up @@ -224,6 +233,7 @@ class BookDaoTest {

@Test
fun bookDao_fetches_books_by_title_desc() = runTest {
val bookDao = database.bookDao()
val bookEntities = listOf(
testBookEntity(
id = "0",
Expand Down Expand Up @@ -261,6 +271,7 @@ class BookDaoTest {

@Test
fun bookDao_fetches_bookById_isNotNull() = runTest {
val bookDao = database.bookDao()
val bookEntities = listOf(
testBookEntity(
id = "0",
Expand Down Expand Up @@ -291,6 +302,7 @@ class BookDaoTest {

@Test
fun bookDao_fetches_bookById_isNull() = runTest {
val bookDao = database.bookDao()
val bookEntities = listOf(
testBookEntity(
id = "0",
Expand Down Expand Up @@ -319,6 +331,7 @@ class BookDaoTest {

@Test
fun bookDao_update_book() = runTest {
val bookDao = database.bookDao()
val bookEntity = testBookEntity(
id = "2",
title = "Zebras"
Expand All @@ -334,6 +347,7 @@ class BookDaoTest {

@Test
fun bookDao_deleteById_removes_book() = runTest {
val bookDao = database.bookDao()
val bookEntity = testBookEntity(
id = "2",
title = "Zebras"
Expand All @@ -348,6 +362,7 @@ class BookDaoTest {

@Test
fun bookDao_delete_removes_book() = runTest {
val bookDao = database.bookDao()
val bookEntity = testBookEntity(
id = "2",
title = "Zebras"
Expand All @@ -362,6 +377,7 @@ class BookDaoTest {

@Test
fun bookDao_deleteAll_removes_book() = runTest {
val bookDao = database.bookDao()
val bookEntities = listOf(
testBookEntity(
id = "0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,42 @@ package com.caminaapps.bookworm.app

import android.app.Application
import com.caminaapps.bookworm.BuildConfig
import com.caminaapps.bookworm.util.CrashlyticsLogging
import com.caminaapps.bookworm.di.DebugLogger
import com.caminaapps.bookworm.di.Logger
import com.caminaapps.bookworm.di.ProdLogger
import com.google.firebase.FirebaseApp
import com.google.firebase.appcheck.FirebaseAppCheck
import com.google.firebase.appcheck.playintegrity.PlayIntegrityAppCheckProviderFactory
import dagger.hilt.android.HiltAndroidApp
import org.jetbrains.annotations.VisibleForTesting
import timber.log.Timber
import javax.inject.Inject

@HiltAndroidApp
class BookwormApplication : Application() {

@DebugLogger
@Inject
lateinit var debugLogger: Logger

@ProdLogger
@Inject
lateinit var prodLogger: Logger

override fun onCreate() {
super.onCreate()
initLogger()
initLogger(BuildConfig.DEBUG)
initAppCheck()
}

private fun initLogger() {
if (BuildConfig.DEBUG) {
Timber.plant(Timber.DebugTree())
@VisibleForTesting
private fun initLogger(isDebug: Boolean) {
val logger = if (isDebug) {
debugLogger
} else {
Timber.plant(CrashlyticsLogging())
prodLogger
}
Timber.plant(logger)
}

private fun initAppCheck() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject

class BookRepositoryImpl @Inject constructor(
class DefaultBookRepository @Inject constructor(
private val bookDao: BookDao
) : BookRepository {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject

class UserPreferencesRepositoryImpl @Inject constructor(
class DefaultUserPreferencesRepository @Inject constructor(
private val dataStore: DataStore<Preferences>
) : UserPreferencesRepository {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.caminaapps.bookworm.core.data.network.dto.toBook
import com.caminaapps.bookworm.core.model.Book
import javax.inject.Inject

class OnlineSearchBookRepositoryImpl @Inject constructor(
class OpenLibrarySearchBookRepository @Inject constructor(
private val openLibraryAPI: OpenLibraryAPI,
) : OnlineSearchBookRepository {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.analytics.ktx.analytics
import com.google.firebase.analytics.ktx.logEvent
import com.google.firebase.ktx.Firebase
import timber.log.Timber

@Composable
fun TrackedScreen(
Expand All @@ -19,6 +20,7 @@ fun TrackedScreen(
DisposableEffect(lifecycleOwner) {
val observer = LifecycleEventObserver { _, event ->
if (event == Lifecycle.Event.ON_START) {
Timber.d("tracked screen $name")
Firebase.analytics.logEvent(FirebaseAnalytics.Event.SCREEN_VIEW) {
param(FirebaseAnalytics.Param.SCREEN_NAME, name)
param(FirebaseAnalytics.Param.SCREEN_CLASS, name)
Expand Down
31 changes: 31 additions & 0 deletions app/src/main/java/com/caminaapps/bookworm/di/LoggingModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.caminaapps.bookworm.di

import com.caminaapps.bookworm.logging.CrashlyticsLogging
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import timber.log.Timber
import javax.inject.Qualifier

typealias Logger = Timber.Tree

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class ProdLogger

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class DebugLogger

@Module
@InstallIn(SingletonComponent::class)
object LoggingModule {
@DebugLogger
@Provides
fun provideDebugLogger(): Logger = Timber.DebugTree()

@ProdLogger
@Provides
fun provideProdLogger(): Logger = CrashlyticsLogging()
}
Loading

0 comments on commit e60aaa1

Please sign in to comment.