Skip to content

Commit 904c1b7

Browse files
committed
Developed shared pref manager
1 parent 68716b5 commit 904c1b7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1689
-5
lines changed

build.gradle

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22
buildscript {
3-
ext.kotlin_version = "1.8.21"
3+
ext.kotlin_version = "1.9.0"
44
ext.lifecycle_version = "2.6.1"
55
ext.coroutines_version = "1.5.2"
66
ext.ktx_version = "1.10.1"
@@ -14,9 +14,12 @@ buildscript {
1414
ext.recycler_view_version = "1.3.0"
1515
ext.constraint_layout_version = "2.1.4"
1616
ext.moshi_version = "1.14.0"
17-
ext.room_version = "2.5.1"
17+
ext.room_version = "2.6.0"
1818
ext.ktor_version = "1.6.7"
1919
ext.okhttp_version = "4.11.0"
20+
ext.compose_version = "1.8.0"
21+
ext.compose_bom_version = "2024.04.01"
22+
ext.navigation_compose_version = "2.7.7"
2023

2124
repositories {
2225
google()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
plugins {
2+
id 'com.android.library'
3+
id 'kotlin-android'
4+
id 'kotlin-kapt'
5+
}
6+
7+
android {
8+
compileSdk 34
9+
10+
defaultConfig {
11+
minSdk 21
12+
targetSdk 34
13+
14+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
15+
consumerProguardFiles "consumer-rules.pro"
16+
}
17+
18+
buildTypes {
19+
release {
20+
minifyEnabled false
21+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
22+
}
23+
}
24+
compileOptions {
25+
sourceCompatibility JavaVersion.VERSION_11
26+
targetCompatibility JavaVersion.VERSION_11
27+
}
28+
kotlinOptions {
29+
jvmTarget = '11'
30+
}
31+
buildFeatures {
32+
compose = true
33+
}
34+
composeOptions {
35+
kotlinCompilerExtensionVersion = "1.5.1"
36+
}
37+
namespace 'com.trendyol.android.devtools.sharedprefmanager'
38+
}
39+
40+
ext {
41+
PUBLISH_GROUP_ID = 'com.trendyol.android.devtools.sharedprefmanager'
42+
PUBLISH_VERSION = '0.0.1'
43+
PUBLISH_ARTIFACT_ID = 'sharedpref-manager'
44+
PUBLISH_DESCRIPTION = "Android SharedPfre Manager"
45+
PUBLISH_URL = "https://github.com/Trendyol/android-dev-tools"
46+
PUBLISH_LICENSE_NAME = "Android DevTools License"
47+
PUBLISH_LICENSE_URL = "https://github.com/Trendyol/android-dev-tools/blob/master/LICENSE"
48+
PUBLISH_SCM_CONNECTION = "scm:git:github.com/Trendyol/android-dev-tools.git"
49+
PUBLISH_SCM_DEV_CONNECTION = "scm:git:ssh://github.com/Trendyol/android-dev-tools.git"
50+
PUBLISH_SCM_URL = "https://github.com/Trendyol/android-dev-tools/tree/main"
51+
}
52+
53+
apply from: "${rootProject.projectDir}/scripts/publish-module.gradle"
54+
55+
56+
dependencies {
57+
58+
implementation "androidx.core:core-ktx:$ktx_version"
59+
implementation "androidx.appcompat:appcompat:$appcompat_version"
60+
implementation "androidx.activity:activity-compose:$compose_version"
61+
implementation platform("androidx.compose:compose-bom:$compose_bom_version")
62+
implementation "androidx.compose.ui:ui"
63+
implementation "androidx.compose.ui:ui-graphics"
64+
implementation "androidx.compose.ui:ui-tooling-preview"
65+
implementation "androidx.compose.material3:material3"
66+
implementation "androidx.navigation:navigation-compose:$navigation_compose_version"
67+
}

libraries/sharedpref-manager/consumer-rules.pro

Whitespace-only changes.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.trendyol.android.devtools.sharedprefmanager
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.*
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22+
assertEquals("com.trendyol.android.devtools.sharedprefmanager.test", appContext.packageName)
23+
}
24+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
3+
4+
<application>
5+
<activity
6+
android:name=".ui.SharedPrefManagerActivity"
7+
android:exported="false"
8+
android:label="@string/title_activity_shared_pref_manager"
9+
android:theme="@style/Theme.Devtools" />
10+
</application>
11+
12+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.trendyol.android.devtools.sharedprefmanager
2+
3+
import android.app.Application
4+
import android.content.Intent
5+
import com.trendyol.android.devtools.sharedprefmanager.di.ContextContainer
6+
import com.trendyol.android.devtools.sharedprefmanager.ui.SharedPrefManagerActivity
7+
8+
object SharedPrefManager {
9+
10+
private val intent by lazy {
11+
Intent(ContextContainer.getContext(), SharedPrefManagerActivity::class.java).apply {
12+
flags = Intent.FLAG_ACTIVITY_NEW_TASK
13+
}
14+
}
15+
16+
fun init(application: Application) {
17+
ContextContainer.initialize(application)
18+
}
19+
20+
fun show(sharedPrefName: String) {
21+
ContextContainer.setSharedPrefName(sharedPrefName)
22+
ContextContainer.getContext().startActivity(intent)
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.trendyol.android.devtools.sharedprefmanager.data
2+
3+
import com.trendyol.android.devtools.sharedprefmanager.data.SharedPrefValueTypeConverter.getValueType
4+
5+
data class SharedPrefModel(
6+
val key: String,
7+
val value: Any,
8+
val valueType: SharedPrefValueType = getValueType(value)
9+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.trendyol.android.devtools.sharedprefmanager.data
2+
3+
enum class SharedPrefValueType {
4+
STRING,
5+
INT,
6+
LONG,
7+
BOOLEAN,
8+
FLOAT,
9+
UNKNOWN,
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.trendyol.android.devtools.sharedprefmanager.data
2+
3+
object SharedPrefValueTypeConverter {
4+
5+
fun getValueType(value: Any): SharedPrefValueType {
6+
return when(value) {
7+
is String -> SharedPrefValueType.STRING
8+
is Int -> SharedPrefValueType.INT
9+
is Boolean -> SharedPrefValueType.BOOLEAN
10+
is Float -> SharedPrefValueType.FLOAT
11+
is Long -> SharedPrefValueType.LONG
12+
else -> SharedPrefValueType.UNKNOWN
13+
}
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.trendyol.android.devtools.sharedprefmanager.di
2+
3+
import android.app.Application
4+
import android.content.Context
5+
import com.trendyol.android.devtools.sharedprefmanager.domain.SharedPrefUpdateTypeValidator
6+
7+
internal object ContextContainer {
8+
9+
val mainContainer by lazy {
10+
MainContainer(
11+
sharedPrefUseCaseContainer = sharedPrefUseCaseContainer,
12+
sharedPrefUpdateTypeValidator = sharedPrefUpdateTypeValidator,
13+
)
14+
}
15+
16+
private val sharedPrefUseCaseContainer by lazy {
17+
SharedPrefUseCaseContainer(application.applicationContext, sharedPrefName)
18+
}
19+
private val sharedPrefUpdateTypeValidator by lazy {
20+
SharedPrefUpdateTypeValidator()
21+
}
22+
private lateinit var application: Application
23+
private lateinit var sharedPrefName: String
24+
25+
fun getContext(): Context =
26+
if (::application.isInitialized) {
27+
application.applicationContext
28+
} else {
29+
throw NullPointerException("SharedPref Manager library is not initialized.")
30+
}
31+
32+
fun initialize(application: Application) {
33+
this.application = application
34+
}
35+
36+
fun setSharedPrefName(sharedPrefName: String) {
37+
this.sharedPrefName = sharedPrefName
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.trendyol.android.devtools.sharedprefmanager.di
2+
3+
import androidx.lifecycle.ViewModel
4+
import androidx.lifecycle.ViewModelProvider
5+
import com.trendyol.android.devtools.sharedprefmanager.domain.SharedPrefUpdateTypeValidator
6+
import com.trendyol.android.devtools.sharedprefmanager.ui.SharedPrefManagerViewModel
7+
8+
internal class MainContainer(
9+
private val sharedPrefUseCaseContainer: SharedPrefUseCaseContainer,
10+
private val sharedPrefUpdateTypeValidator: SharedPrefUpdateTypeValidator
11+
) {
12+
13+
inner class SharedPrefManagerViewModelFactory: ViewModelProvider.Factory {
14+
15+
@Suppress("UNCHECKED_CAST")
16+
override fun <T : ViewModel> create(modelClass: Class<T>): T {
17+
return SharedPrefManagerViewModel(
18+
sharedPrefManagerUseCase = sharedPrefUseCaseContainer.sharedPrefManagerUseCase,
19+
sharedPrefUpdateTypeValidator = sharedPrefUpdateTypeValidator
20+
) as T
21+
}
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.trendyol.android.devtools.sharedprefmanager.di
2+
3+
import android.content.Context
4+
import android.content.SharedPreferences
5+
import com.trendyol.android.devtools.sharedprefmanager.domain.SharedPrefManagerRepository
6+
import com.trendyol.android.devtools.sharedprefmanager.domain.SharedPrefManagerRepositoryImpl
7+
import com.trendyol.android.devtools.sharedprefmanager.domain.SharedPrefManagerUseCase
8+
import com.trendyol.android.devtools.sharedprefmanager.domain.SharedPrefManagerUseCaseImpl
9+
10+
internal class SharedPrefUseCaseContainer(
11+
private val context: Context,
12+
private val sharedPrefName: String
13+
) {
14+
15+
private val sharedPref: SharedPreferences by lazy {
16+
context.getSharedPreferences(sharedPrefName, Context.MODE_PRIVATE)
17+
}
18+
19+
private val sharedPrefManagerRepository: SharedPrefManagerRepository by lazy {
20+
SharedPrefManagerRepositoryImpl(sharedPref)
21+
}
22+
23+
val sharedPrefManagerUseCase: SharedPrefManagerUseCase by lazy {
24+
SharedPrefManagerUseCaseImpl(sharedPrefManagerRepository)
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.trendyol.android.devtools.sharedprefmanager.domain
2+
3+
import com.trendyol.android.devtools.sharedprefmanager.data.SharedPrefModel
4+
5+
interface SharedPrefManagerRepository {
6+
7+
suspend fun getAllSharedPref(): MutableMap<String, *>
8+
9+
suspend fun updateStringValue(key: String, newValue: String)
10+
11+
suspend fun updateIntValue(key: String, newValue: Int)
12+
13+
suspend fun updateBooleanValue(key: String, newValue: Boolean)
14+
15+
suspend fun updateLongValue(key: String, newValue: Long)
16+
17+
suspend fun updateFloatValue(key: String, newValue: Float)
18+
19+
suspend fun deleteSharedPrefItem(sharedPrefModel: SharedPrefModel)
20+
21+
suspend fun deleteAllSharedPref()
22+
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.trendyol.android.devtools.sharedprefmanager.domain
2+
3+
import android.content.SharedPreferences
4+
import com.trendyol.android.devtools.sharedprefmanager.data.SharedPrefModel
5+
6+
class SharedPrefManagerRepositoryImpl(
7+
private val sharedPreferences: SharedPreferences
8+
): SharedPrefManagerRepository {
9+
10+
override suspend fun getAllSharedPref(): MutableMap<String, *> {
11+
return sharedPreferences.all
12+
}
13+
14+
override suspend fun updateStringValue(key: String, newValue: String) {
15+
sharedPreferences.edit().putString(key,newValue).apply()
16+
}
17+
18+
override suspend fun updateIntValue(key: String, newValue: Int) {
19+
sharedPreferences.edit().putInt(key,newValue).apply()
20+
}
21+
22+
override suspend fun updateBooleanValue(key: String, newValue: Boolean) {
23+
sharedPreferences.edit().putBoolean(key,newValue).apply()
24+
}
25+
26+
override suspend fun updateLongValue(key: String, newValue: Long) {
27+
sharedPreferences.edit().putLong(key,newValue).apply()
28+
}
29+
30+
override suspend fun updateFloatValue(key: String, newValue: Float) {
31+
sharedPreferences.edit().putFloat(key,newValue).apply()
32+
}
33+
34+
override suspend fun deleteSharedPrefItem(sharedPrefModel: SharedPrefModel) {
35+
sharedPreferences.edit().remove(sharedPrefModel.key).apply()
36+
}
37+
38+
override suspend fun deleteAllSharedPref() {
39+
sharedPreferences.edit().clear().apply()
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.trendyol.android.devtools.sharedprefmanager.domain
2+
3+
import com.trendyol.android.devtools.sharedprefmanager.data.SharedPrefModel
4+
5+
interface SharedPrefManagerUseCase {
6+
7+
suspend fun getAllSharedPref(): List<SharedPrefModel>
8+
9+
suspend fun updateSharedPrefItem(sharedPrefModel: SharedPrefModel, newValue: String)
10+
11+
suspend fun deleteSharedPrefItem(sharedPrefModel: SharedPrefModel)
12+
13+
suspend fun deleteAllSharedPref()
14+
15+
}

0 commit comments

Comments
 (0)