Skip to content

Commit

Permalink
feature added and Ktorfit Update (#36)
Browse files Browse the repository at this point in the history
* Updated .gitignore

Signed-off-by: Rajesh Hadiya <[email protected]>

* Updated .gitignore

Signed-off-by: Rajesh Hadiya <[email protected]>

* Delete FlowerSample_jslegacy.xml

* Delete FlowerSample_jvm.xml

* Delete flower_core_jslegacy.xml

* Delete flower_core_jvm.xml

* Delete flower_ktorfit_jslegacy.xml

* Delete flower_ktorfit_jvm.xml

* Delete Project_Default.xml

* Delete jarRepositories.xml

* Delete kotlinScripting.xml

* Delete uiDesigner.xml

* Delete kotlinc.xml

* Dev (#27) (#28)

* Updated .gitignore

Signed-off-by: Rajesh Hadiya <[email protected]>

* Updated .gitignore

Signed-off-by: Rajesh Hadiya <[email protected]>

Signed-off-by: Rajesh Hadiya <[email protected]>

Signed-off-by: Rajesh Hadiya <[email protected]>

* Dev (#27) (#29)

* Updated .gitignore

Signed-off-by: Rajesh Hadiya <[email protected]>

* Updated .gitignore

Signed-off-by: Rajesh Hadiya <[email protected]>

Signed-off-by: Rajesh Hadiya <[email protected]>

Signed-off-by: Rajesh Hadiya <[email protected]>

* Updated README.md

Signed-off-by: Rajesh Hadiya <[email protected]>

* 1. Updated dependencies (core-ktx, appcompat, activity-compose, navigation-compose, gradle-maven-publish-plugin.
2. Updated compile/target sdk version
3. Added error handling in compose sample app

Signed-off-by: Rajesh Hadiya <[email protected]>

* Feature use inline classes (#34)

* 1. Made Resource class as regular class with private constructor.
2. Added ErrorMessage and HttpStatusCode value classes to avoid using primitives
3. Added DBResource.kt

Signed-off-by: Rajesh Hadiya <[email protected]>

* 1. Added dbResource to fetch data from db only
2. Made Resource class constructor private
3. Compose app migrated to material 3 design
4. Code cleanup

Signed-off-by: Rajesh Hadiya <[email protected]>

* 1. Added dbResource to fetch data from db only
2. Used kotlin value classes for ErrorMessage and HttpStatusCode
3. Added more features in compose-app
4. Moved Flow related functions to separate package

Signed-off-by: Rajesh Hadiya <[email protected]>

* 1. Reverted error messages and http status code to primitives
2. Code re-structured

Signed-off-by: Rajesh Hadiya <[email protected]>

Signed-off-by: Rajesh Hadiya <[email protected]>

* Ktorfit Update (#35)

* Dev (#27)

* Updated .gitignore

Signed-off-by: Rajesh Hadiya <[email protected]>

* Updated .gitignore

Signed-off-by: Rajesh Hadiya <[email protected]>

Signed-off-by: Rajesh Hadiya <[email protected]>

* Removed ignored file (#30)

* Updated .gitignore

* Dev (#31)

* Updated .gitignore

Signed-off-by: Rajesh Hadiya <[email protected]>

* Updated .gitignore

Signed-off-by: Rajesh Hadiya <[email protected]>

* Delete FlowerSample_jslegacy.xml

* Delete FlowerSample_jvm.xml

* Delete flower_core_jslegacy.xml

* Delete flower_core_jvm.xml

* Delete flower_ktorfit_jslegacy.xml

* Delete flower_ktorfit_jvm.xml

* Delete Project_Default.xml

* Delete jarRepositories.xml

* Delete kotlinScripting.xml

* Delete uiDesigner.xml

* Delete kotlinc.xml

* Dev (#27) (#28)

* Updated .gitignore

Signed-off-by: Rajesh Hadiya <[email protected]>

* Updated .gitignore

Signed-off-by: Rajesh Hadiya <[email protected]>

Signed-off-by: Rajesh Hadiya <[email protected]>

Signed-off-by: Rajesh Hadiya <[email protected]>

* Dev (#27) (#29)

* Updated .gitignore

Signed-off-by: Rajesh Hadiya <[email protected]>

* Updated .gitignore

Signed-off-by: Rajesh Hadiya <[email protected]>

Signed-off-by: Rajesh Hadiya <[email protected]>

Signed-off-by: Rajesh Hadiya <[email protected]>

* Updated README.md

Signed-off-by: Rajesh Hadiya <[email protected]>

Signed-off-by: Rajesh Hadiya <[email protected]>

* Update README.md

* Update README.md

* updated ktorfit

Signed-off-by: Rajesh Hadiya <[email protected]>
Co-authored-by: Rajesh Hadiya <[email protected]>

Signed-off-by: Rajesh Hadiya <[email protected]>
Co-authored-by: DatLag <[email protected]>
  • Loading branch information
hadiyarajesh and DatL4g authored Dec 4, 2022
1 parent ffe5c74 commit 434b7cf
Show file tree
Hide file tree
Showing 46 changed files with 517 additions and 273 deletions.
1 change: 0 additions & 1 deletion .idea/.name

This file was deleted.

2 changes: 1 addition & 1 deletion .idea/compiler.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.

16 changes: 8 additions & 8 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ plugins {

android {
namespace = "com.hadiyarajesh.flowersample"
compileSdk = 32
buildToolsVersion = "30.0.3"
compileSdk = 33

defaultConfig {
applicationId = "com.hadiyarajesh.flowersample"
minSdk = 21
targetSdk = 32
targetSdk = 33
versionCode = 1
versionName = "1.0"

Expand Down Expand Up @@ -62,13 +61,14 @@ object LibVersion {
}

dependencies {
implementation("androidx.core:core-ktx:1.8.0")
implementation("androidx.appcompat:appcompat:1.5.0")
implementation("androidx.core:core-ktx:${rootProject.extra["coreKtxVersion"]}")
implementation("androidx.appcompat:appcompat:1.5.1")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("com.google.android.material:material:1.6.1")
implementation("com.google.android.material:material:1.7.0")
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:${LibVersion.lifecycleVersion}")
implementation("androidx.lifecycle:lifecycle-livedata-ktx:${LibVersion.lifecycleVersion}")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:${LibVersion.lifecycleVersion}")

implementation("com.google.dagger:hilt-android:${rootProject.extra["hiltVersion"]}")
kapt("com.google.dagger:hilt-android-compiler:${rootProject.extra["hiltVersion"]}")

Expand All @@ -83,6 +83,6 @@ dependencies {
implementation(project(":flower-retrofit"))

testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.3")
androidTestImplementation("androidx.test.espresso:espresso-core:3.4.0")
androidTestImplementation("androidx.test.ext:junit:1.1.4")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.0")
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ import androidx.room.RoomDatabase
import com.hadiyarajesh.flowersample.data.database.dao.QuoteDao
import com.hadiyarajesh.flowersample.data.database.entity.Quote

@Database(entities = [Quote::class], version = 1, exportSchema = true)
@Database(
entities = [Quote::class],
version = 1,
exportSchema = true
)
abstract class QuoteDatabase : RoomDatabase() {
abstract val quoteDao: QuoteDao
abstract val quoteDao: QuoteDao
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ import com.squareup.moshi.JsonClass
@Entity(tableName = "quote")
@JsonClass(generateAdapter = true)
data class Quote(
@ColumnInfo(name = "_internalId")
@Json(name = "id")
val _internalId: Int,
val title: String,
val quote: String,
val author: Int,
@ColumnInfo(name = "id")
@PrimaryKey(autoGenerate = false) val primaryId: Int
@ColumnInfo(name = "id")
@PrimaryKey(autoGenerate = false)
val primaryId: Int,
@ColumnInfo(name = "_internalId")
@Json(name = "id")
val _internalId: Int,
val title: String,
val quote: String,
val author: Int,
)
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,31 @@ import com.squareup.moshi.FromJson
import com.squareup.moshi.JsonClass
import com.squareup.moshi.ToJson

class QuoteAdapter {
class QuoteAdapter {
@ToJson
fun toJson(_quote: Quote): QuoteJson {
return _quote.run { QuoteJson(_internalId, Title(title),Content(quote),author) }
return _quote.run { QuoteJson(_internalId, Title(title), Content(quote), author) }
}

@FromJson
fun fromJson(quoteJson: QuoteJson): Quote{
return quoteJson.run { Quote(id,title.rendered,content.rendered,author,1) }
fun fromJson(quoteJson: QuoteJson): Quote {
return quoteJson.run {
Quote(
primaryId = 1,
_internalId = id,
title = title.rendered,
quote = content.rendered,
author = author
)
}
}
}


@JsonClass(generateAdapter = true)
data class Title(val rendered: String)

@JsonClass(generateAdapter = true)
data class Content(val rendered: String)

@JsonClass(generateAdapter = true)
data class QuoteJson(val id: Int, val title: Title, val content: Content,val author: Int)
data class QuoteJson(val id: Int, val title: Title, val content: Content, val author: Int)
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ class QuoteRepository @Inject constructor(
return dbBoundResource(
fetchFromLocal = {
Log.i(TAG, "Fetching from local cache")
val localResult = quoteDao.getQuote(pageNo)
localResult
quoteDao.getQuote(pageNo)
},
shouldMakeNetworkRequest = {
Log.i(TAG, "Checking if remote fetch is needed")
Expand All @@ -60,7 +59,9 @@ class QuoteRepository @Inject constructor(
val copiedQuotes = quotes.map { it.copy(primaryId = pageNo) }
copiedQuotes.forEach { quoteDao.insertOrUpdateQuote(it) }
},
onNetworkRequestFailed = { errorBody, statusCode -> onFailed(errorBody, statusCode) },
onNetworkRequestFailed = { errorMessage, httpStatusCode ->
onFailed(errorMessage, httpStatusCode)
},
).map {
when (it.status) {
is Resource.Status.Loading -> {
Expand All @@ -78,7 +79,7 @@ class QuoteRepository @Inject constructor(

is Resource.Status.Error -> {
val error = it.status as Resource.Status.Error
Resource.error(error.message, error.statusCode, error.data)
Resource.error(error.errorMessage, error.httpStatusCode, error.data)
}
}
}.flowOn(Dispatchers.IO)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,14 @@ import javax.inject.Singleton
object NetworkModule {
private const val API_BASE_URL = "https://quotesondesign.com/wp-json/wp/v2/"

private val customMoshi = Moshi.Builder()
private val moshi = Moshi.Builder()
.add(QuoteAdapter())
.build()

@Provides
@Singleton
fun getOkHttpClient(): OkHttpClient {
return OkHttpClient.Builder()
.followRedirects(true)
.followSslRedirects(true)
.retryOnConnectionFailure(true)
.build()
}
Expand All @@ -55,7 +53,7 @@ object NetworkModule {
.baseUrl(API_BASE_URL)
.client(okHttpClient)
.addCallAdapterFactory(FlowerCallAdapterFactory.create())
.addConverterFactory(MoshiConverterFactory.create(customMoshi))
.addConverterFactory(MoshiConverterFactory.create(moshi))
.build()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ suspend fun <T> Flow<Resource<T>>.foldApiStates(

is Resource.Status.Error -> {
val error = resource.status as Resource.Status.Error
onError(MainActivityViewModel.State.ErrorState(error.message, error.statusCode))
onError(MainActivityViewModel.State.ErrorState(error.errorMessage, error.httpStatusCode))
}
}
}
Expand Down
16 changes: 7 additions & 9 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,39 +1,37 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
kotlin("multiplatform") version "1.7.10" apply false
id("com.vanniktech.maven.publish") version "0.21.0" apply false
kotlin("multiplatform") version "1.7.20" apply false
id("com.vanniktech.maven.publish") version "0.22.0" apply false
}

buildscript {
val hiltVersion by extra("2.43")
val coreKtxVersion by extra("1.9.0")
val hiltVersion by extra("2.44")

allprojects {
repositories {
google()
mavenLocal()
mavenCentral()
gradlePluginPortal()
}
}

repositories {
google()
mavenLocal()
mavenCentral()
gradlePluginPortal()
}

dependencies {
classpath("com.android.tools.build:gradle:7.2.2")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10")
classpath("com.android.tools.build:gradle:7.3.1")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.20")
classpath("com.google.dagger:hilt-android-gradle-plugin:$hiltVersion")
classpath("com.vanniktech:gradle-maven-publish-plugin:0.21.0")
classpath("com.vanniktech:gradle-maven-publish-plugin:0.22.0")
}
}

repositories {
google()
mavenLocal()
mavenCentral()
gradlePluginPortal()
}
42 changes: 21 additions & 21 deletions compose-app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ plugins {

android {
namespace = "com.hadiyarajesh.compose_app"
compileSdk = 32
compileSdk = 33

defaultConfig {
applicationId = "com.hadiyarajesh.compose_app"
minSdk = 21
targetSdk = 32
targetSdk = 33
versionCode = 1
versionName = "1.0"

Expand Down Expand Up @@ -62,22 +62,23 @@ android {
}

object LibVersion {
const val composeVersion = "1.2.0"
const val composeCompilerVersion = "1.3.0"
const val composeCompilerVersion = "1.3.2"
const val roomVersion = "2.4.2"
const val retrofitVersion = "2.9.0"
const val moshiVersion = "1.13.0"
const val accompanistVersion = "0.25.0"
const val accompanistVersion = "0.27.0"
}

dependencies {
implementation("androidx.core:core-ktx:1.8.0")
val composeBom = platform("androidx.compose:compose-bom:2022.10.00")

implementation("androidx.core:core-ktx:${rootProject.extra["coreKtxVersion"]}")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.5.1")
implementation("androidx.activity:activity-compose:1.5.1")
implementation("androidx.compose.ui:ui:${LibVersion.composeVersion}")
implementation("androidx.compose.ui:ui-tooling-preview:${LibVersion.composeVersion}")
implementation("androidx.compose.material:material:${LibVersion.composeVersion}")
implementation("androidx.navigation:navigation-compose:2.5.1")
implementation("androidx.activity:activity-compose:1.6.1")
implementation(composeBom)
implementation("androidx.compose.material3:material3")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.navigation:navigation-compose:2.5.3")

implementation("androidx.hilt:hilt-navigation-compose:1.0.0")
implementation("com.google.dagger:hilt-android:${rootProject.extra["hiltVersion"]}")
Expand All @@ -96,16 +97,15 @@ dependencies {

implementation("com.google.accompanist:accompanist-swiperefresh:${LibVersion.accompanistVersion}")

implementation("io.coil-kt:coil-compose:2.2.0")
implementation("io.coil-kt:coil-compose:2.2.2")

testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.3")
androidTestImplementation("androidx.test.espresso:espresso-core:3.4.0")
androidTestImplementation("androidx.compose.ui:ui-test-junit4:${LibVersion.composeVersion}")
debugImplementation("androidx.compose.ui:ui-tooling:${LibVersion.composeVersion}")
debugImplementation("androidx.compose.ui:ui-test-manifest:${LibVersion.composeVersion}")
}

repositories {
mavenCentral()
androidTestImplementation("androidx.test.ext:junit:1.1.4")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.0")
// UI Tests
androidTestImplementation(composeBom)
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
debugImplementation("androidx.compose.ui:ui-test-manifest")
// Android Studio Preview support
debugImplementation("androidx.compose.ui:ui-tooling")
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ interface ImageDao {
@Query("SELECT * FROM Image ORDER BY id")
fun getAllImages(): Flow<List<Image>>

@Query("SELECT * FROM Image WHERE id=:imageId ORDER BY id")
fun getImageById(imageId: Long): Flow<Image>

@Query("DELETE FROM Image")
suspend fun deleteAllImages()
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.hadiyarajesh.compose_app.database.entity.Image
import com.hadiyarajesh.compose_app.network.ImageApi
import com.hadiyarajesh.flower_core.Resource
import com.hadiyarajesh.flower_core.dbBoundResource
import com.hadiyarajesh.flower_core.dbResource
import com.hadiyarajesh.flower_core.networkResource
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
Expand Down Expand Up @@ -58,7 +59,11 @@ class ImageRepository @Inject constructor(
).flowOn(Dispatchers.IO)
}

fun getImage(imageId: Long): Flow<Resource<Image>> {
fun getImageFromDb(imageId: Long): Flow<Resource<Image>> {
return dbResource { imageDao.getImageById(imageId) }.flowOn(Dispatchers.IO)
}

fun getImageFromNetwork(imageId: Long): Flow<Resource<Image>> {
return networkResource(
makeNetworkRequest = { imageApi.getImage(imageId = imageId) },
onNetworkRequestFailed = { _, _ -> }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@

package com.hadiyarajesh.compose_app.ui.component

import androidx.compose.foundation.layout.*
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.material.MaterialTheme
import androidx.compose.material.OutlinedButton
import androidx.compose.material.Text
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand Down Expand Up @@ -95,7 +102,7 @@ fun SubComposeImageItem(
fun LoadingProgressBar(
modifier: Modifier = Modifier,
size: Dp = 40.dp,
loadingCircleColor: Color = MaterialTheme.colors.onBackground,
loadingCircleColor: Color = MaterialTheme.colorScheme.onBackground,
strokeWidth: Dp = 4.dp
) {
Box(modifier = modifier) {
Expand Down Expand Up @@ -133,7 +140,7 @@ fun ErrorText(
Text(
modifier = modifier,
text = text,
color = MaterialTheme.colors.error,
style = MaterialTheme.typography.caption,
color = MaterialTheme.colorScheme.error,
style = MaterialTheme.typography.labelMedium,
)
}
Loading

0 comments on commit 434b7cf

Please sign in to comment.