Skip to content

Commit

Permalink
fix: separate data access to drivers module
Browse files Browse the repository at this point in the history
  • Loading branch information
anangkur committed Sep 12, 2024
1 parent 8691f18 commit 5c915eb
Show file tree
Hide file tree
Showing 47 changed files with 309 additions and 141 deletions.
32 changes: 20 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,25 @@
graph LR
subgraph :data
:data:local["local"]
:data:remote["remote"]
:data:local["local"]
end
subgraph :driver
:driver:rest["rest"]
:driver:localdb["localdb"]
:driver:config["config"]
end
subgraph :features
:features:preview["preview"]
:features:saved["saved"]
:features:home["home"]
:features:search["search"]
:features:saved["saved"]
:features:preview["preview"]
:features:collection["collection"]
end
:features:preview --> :app
:data:local --> :data
:features:saved --> :app
:data --> :domain
:data:remote --> :data
:data:remote --> :driver:rest
:features:home --> :app
:injection --> :data
:injection --> :domain
:injection --> :data:local
:injection --> :data:remote
:features:search --> :app
:features:collection --> :app
:app --> :navigator
:app --> :injection
:app --> :domain
Expand All @@ -39,6 +36,17 @@ graph LR
:app --> :features:saved
:app --> :features:preview
:app --> :features:collection
:features:preview --> :app
:data:local --> :data
:data:local --> :driver:localdb
:data:local --> :driver:config
:features:saved --> :app
:data --> :domain
:injection --> :data
:injection --> :domain
:injection --> :data:local
:injection --> :data:remote
:features:collection --> :app
```
## Built With
* [Kotlin](https://kotlinlang.org/)
Expand Down
5 changes: 2 additions & 3 deletions data/local/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

android{
kotlinOptions {
Expand All @@ -10,7 +9,7 @@ android{

dependencies {
implementation(project(':data'))

implementation(project(':driver:localdb'))
implementation(project(':driver:config'))
implementation Deps.androidxRoom
kapt Deps.androidxRoomCompiler
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package com.anangkur.wallpaper.local

import android.content.Context
import android.content.SharedPreferences
import com.anangkur.wallpaper.config.SharedPreferencesConfig
import com.anangkur.wallpaper.data.repository.LocalRepository
import com.anangkur.wallpaper.domain.model.Wallpaper
import com.anangkur.wallpaper.local.db.AppDatabase
import com.anangkur.wallpaper.local.model.toDatabaseEntity
import com.anangkur.wallpaper.local.model.toWallpaper
import com.anangkur.wallpaper.local.mapper.toDatabaseEntity
import com.anangkur.wallpaper.local.mapper.toWallpaper
import com.anangkur.wallpaper.localdb.db.AppDatabase

class LocalRepository(
private val preferences: SharedPreferences,
private val appDatabase: AppDatabase
private val preferences: SharedPreferencesConfig,
private val appDatabase: AppDatabase,
): LocalRepository {

companion object{
private var INSTANCE: LocalRepository? = null
fun getInstance(context: Context) = INSTANCE ?: LocalRepository(
context.getSharedPreferences(Const.PREF_NAME, Context.MODE_PRIVATE),
AppDatabase.getDatabase(context)
SharedPreferencesConfig.getInstance(context),
AppDatabase.getDatabase(context),
)
}

Expand Down Expand Up @@ -53,6 +53,6 @@ class LocalRepository(
}

private fun getLastCacheUpdateTimeMillis(): Long {
return preferences.getLong(Const.PREF_CACHED_TIME, 0)
return preferences.loadCacheTime()
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.anangkur.wallpaper.local.mapper

import com.anangkur.wallpaper.domain.model.Wallpaper
import com.anangkur.wallpaper.localdb.model.DatabaseEntity

fun Wallpaper.toDatabaseEntity() = DatabaseEntity(
title = title,
creator = creator,
imageUrl = imageUrl,
id = id,
isSaved = isSaved,
thumbnailUrl = thumbnailUrl
)

fun DatabaseEntity.toWallpaper() = Wallpaper(
id = id,
title = title,
imageUrl = imageUrl,
creator = creator,
isSaved = isSaved,
thumbnailUrl = thumbnailUrl
)
5 changes: 1 addition & 4 deletions data/remote/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ apply plugin: 'kotlin'
dependencies {

implementation(project(':data'))
implementation(project(':driver:rest'))

implementation Deps.toolsKotlinJdk8
api Deps.retrofitConverterGson
api Deps.retrofit
api Deps.okhttp
api Deps.okhttpLoggingInterceptor
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ package com.anangkur.wallpaper.remote

import com.anangkur.wallpaper.data.repository.RemoteRepository
import com.anangkur.wallpaper.domain.model.Wallpaper
import com.anangkur.wallpaper.remote.model.unsplash.toCollection
import com.anangkur.wallpaper.remote.model.unsplash.toWallpaper
import com.anangkur.wallpaper.remote.services.UnsplashService
import com.anangkur.wallpaper.remote.mapper.toCollection
import com.anangkur.wallpaper.remote.mapper.toWallpaper
import com.anangkur.wallpaper.rest.di.provideRetrofitBuilder
import com.anangkur.wallpaper.rest.service.UnsplashService
import com.anangkur.wallpaper.domain.model.Collection as ModelCollection

class RemoteRepository(private val unsplashService: UnsplashService): RemoteRepository {

companion object{
private var INSTANCE: RemoteRepository? = null
fun getInstance(unsplashService: UnsplashService) = INSTANCE ?: RemoteRepository(unsplashService)
fun getInstance(baseUrl: String) = INSTANCE ?: RemoteRepository(
provideRetrofitBuilder(baseUrl).create(UnsplashService::class.java)
)
}

override suspend fun fetchWallpaper(clientId: String): List<Wallpaper> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.anangkur.wallpaper.remote.mapper

import com.anangkur.wallpaper.domain.model.Collection
import com.anangkur.wallpaper.rest.model.unsplash.CollectionResponse

fun CollectionResponse.toCollection() = Collection(
id = id.orEmpty(),
title = title.orEmpty(),
description = description.orEmpty(),
wallpapers = previewPhotos.map { it.urls.regular.orEmpty() }
)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.anangkur.wallpaper.remote.mapper

import com.anangkur.wallpaper.domain.model.Wallpaper
import com.anangkur.wallpaper.rest.model.unsplash.WallpaperResponse

fun WallpaperResponse.toWallpaper() = Wallpaper(
id = id.orEmpty(),
creator = user?.name.orEmpty().ifEmpty { "-" },
imageUrl = urls?.full.orEmpty(),
isSaved = false,
title = description.orEmpty().ifEmpty { altDescription.orEmpty().ifEmpty { "-" } },
thumbnailUrl = urls?.regular.orEmpty()
)
1 change: 1 addition & 0 deletions driver/config/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
37 changes: 37 additions & 0 deletions driver/config/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
plugins {
id("com.android.library")
id("org.jetbrains.kotlin.android")
}

android {
namespace = "com.anangkur.wallpaper.config"
compileSdk = 34

defaultConfig {
minSdk = 21

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}

dependencies {

}
Empty file.
21 changes: 21 additions & 0 deletions driver/config/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
4 changes: 4 additions & 0 deletions driver/config/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.anangkur.wallpaper.config

import android.content.Context
import android.content.SharedPreferences

class SharedPreferencesConfig(context: Context) {

companion object {
private const val PREF_NAME = "PREF_NAME"
private const val PREF_CACHED_TIME = "PREF_CACHED_TIME"
private var INSTANCE: SharedPreferencesConfig? = null
fun getInstance(context: Context) = INSTANCE ?: SharedPreferencesConfig(context)
}

private val instance: SharedPreferences by lazy {
context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
}

fun saveCacheTime(cacheTime: Long) {
instance.edit().putLong(PREF_CACHED_TIME, cacheTime).apply()
}

fun loadCacheTime(): Long {
return instance.getLong(PREF_CACHED_TIME, 0)
}
}
1 change: 1 addition & 0 deletions driver/localdb/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
14 changes: 14 additions & 0 deletions driver/localdb/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

android{
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
}
}

dependencies {
implementation Deps.androidxRoom
kapt Deps.androidxRoomCompiler
}
Empty file.
21 changes: 21 additions & 0 deletions driver/localdb/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
5 changes: 5 additions & 0 deletions driver/localdb/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.anangkur.wallpaper.localdb">

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.anangkur.wallpaper.localdb

object Const {
const val DATABASE_NAME = "DATABASE_NAME"
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.anangkur.wallpaper.local.dao
package com.anangkur.wallpaper.localdb.dao

import androidx.room.*
import com.anangkur.wallpaper.local.model.DatabaseEntity
import com.anangkur.wallpaper.localdb.model.DatabaseEntity

@Dao
interface AppDao {
Expand Down
Loading

0 comments on commit 5c915eb

Please sign in to comment.