Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/shared pref manager #46

Merged
merged 4 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Android Dev Tools is a library that contains various QA/Debug tools to speed up
* [Environment Manager](#environment-manager)
* [Debug Menu](#debug-menu)
* [DeepLink Launcher](#deeplink-launcher)
* [SharedPref Manager](#sharedpref-manager)

## Autofill Service
Autofill Service allows developers and QA to fill form inputs automatically with predefined form data.
Expand Down Expand Up @@ -225,6 +226,41 @@ DeepLinkLauncher.show()
```
![Maven Central](https://img.shields.io/maven-central/v/com.trendyol.android.devtools/deeplink-launcher?color=%2373c248)

## SharedPref Manager
SharedPref Manager allows you manage shared preferences

### Demo
<table>
<tr>
<td><img src="/art/sharedpref_manager_1.png" width="200" /></td>
<td><img src="/art/sharedpref_manager_2.png" width="200" /></td>
<td><img src="/art/sharedpref_manager_3.png" width="200" /></td>
<td><img src="/art/sharedpref_manager_4.png" width="200" /></td>
</tr>
</table>

### Initialize

You need to initialize the library with with your `android.app.Application` instance.
```kotlin
SharedPrefManager.init(this)
```

### Usage

And just call this function to open SharedPref Manager screen.
```kotlin
SharedPrefManager.show("shared_pref_name")
```

### Setup

```gradle
"com.trendyol.android.devtools:sharedpref-manager:$version"
```

![Maven Central](https://img.shields.io/maven-central/v/com.trendyol.android.devtools/sharedpref-manager?color=%2373c248)

License
--------

Expand Down
Binary file added art/sharedpref_manager_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added art/sharedpref_manager_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added art/sharedpref_manager_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added art/sharedpref_manager_4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 5 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.8.21"
ext.kotlin_version = "1.9.0"
ext.lifecycle_version = "2.6.1"
ext.coroutines_version = "1.5.2"
ext.ktx_version = "1.10.1"
Expand All @@ -14,9 +14,12 @@ buildscript {
ext.recycler_view_version = "1.3.0"
ext.constraint_layout_version = "2.1.4"
ext.moshi_version = "1.14.0"
ext.room_version = "2.5.1"
ext.room_version = "2.6.0"
ext.ktor_version = "1.6.7"
ext.okhttp_version = "4.11.0"
ext.compose_version = "1.8.0"
ext.compose_bom_version = "2024.04.01"
ext.navigation_compose_version = "2.7.7"

repositories {
google()
Expand Down
1 change: 1 addition & 0 deletions libraries/sharedpref-manager/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
67 changes: 67 additions & 0 deletions libraries/sharedpref-manager/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
plugins {
id 'com.android.library'
id 'kotlin-android'
id 'kotlin-kapt'
}

android {
compileSdk 34

defaultConfig {
minSdk 21
targetSdk 34

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

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = '11'
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.5.1"
}
namespace 'com.trendyol.android.devtools.sharedprefmanager'
}

ext {
PUBLISH_GROUP_ID = 'com.trendyol.android.devtools.sharedprefmanager'
PUBLISH_VERSION = '0.0.1'
PUBLISH_ARTIFACT_ID = 'sharedpref-manager'
PUBLISH_DESCRIPTION = "Android SharedPfre Manager"
PUBLISH_URL = "https://github.com/Trendyol/android-dev-tools"
PUBLISH_LICENSE_NAME = "Android DevTools License"
PUBLISH_LICENSE_URL = "https://github.com/Trendyol/android-dev-tools/blob/master/LICENSE"
PUBLISH_SCM_CONNECTION = "scm:git:github.com/Trendyol/android-dev-tools.git"
PUBLISH_SCM_DEV_CONNECTION = "scm:git:ssh://github.com/Trendyol/android-dev-tools.git"
PUBLISH_SCM_URL = "https://github.com/Trendyol/android-dev-tools/tree/main"
}

apply from: "${rootProject.projectDir}/scripts/publish-module.gradle"


dependencies {

implementation "androidx.core:core-ktx:$ktx_version"
implementation "androidx.appcompat:appcompat:$appcompat_version"
implementation "androidx.activity:activity-compose:$compose_version"
implementation platform("androidx.compose:compose-bom:$compose_bom_version")
implementation "androidx.compose.ui:ui"
implementation "androidx.compose.ui:ui-graphics"
implementation "androidx.compose.ui:ui-tooling-preview"
implementation "androidx.compose.material3:material3"
implementation "androidx.navigation:navigation-compose:$navigation_compose_version"
}
Empty file.
21 changes: 21 additions & 0 deletions libraries/sharedpref-manager/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
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.trendyol.android.devtools.sharedprefmanager

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.trendyol.android.devtools.sharedprefmanager.test", appContext.packageName)
}
}
12 changes: 12 additions & 0 deletions libraries/sharedpref-manager/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<application>
<activity
android:name=".ui.SharedPrefManagerActivity"
android:exported="false"
android:label="@string/title_activity_shared_pref_manager"
android:theme="@style/Theme.Devtools" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.trendyol.android.devtools.sharedprefmanager

import android.app.Application
import android.content.Intent
import com.trendyol.android.devtools.sharedprefmanager.di.ContextContainer
import com.trendyol.android.devtools.sharedprefmanager.ui.SharedPrefManagerActivity

object SharedPrefManager {

private val intent by lazy {
Intent(ContextContainer.getContext(), SharedPrefManagerActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK
}
}

fun init(application: Application) {
ContextContainer.initialize(application)
}

fun show(sharedPrefName: String) {
ContextContainer.setSharedPrefName(sharedPrefName)
ContextContainer.getContext().startActivity(intent)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.trendyol.android.devtools.sharedprefmanager.data

import com.trendyol.android.devtools.sharedprefmanager.data.SharedPrefValueTypeConverter.getValueType

data class SharedPrefModel(
val key: String,
val value: Any,
val valueType: SharedPrefValueType = getValueType(value)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.trendyol.android.devtools.sharedprefmanager.data

enum class SharedPrefValueType {
STRING,
INT,
LONG,
BOOLEAN,
FLOAT,
UNKNOWN,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.trendyol.android.devtools.sharedprefmanager.data

object SharedPrefValueTypeConverter {

fun getValueType(value: Any): SharedPrefValueType {
return when(value) {
is String -> SharedPrefValueType.STRING
is Int -> SharedPrefValueType.INT
is Boolean -> SharedPrefValueType.BOOLEAN
is Float -> SharedPrefValueType.FLOAT
is Long -> SharedPrefValueType.LONG
else -> SharedPrefValueType.UNKNOWN
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.trendyol.android.devtools.sharedprefmanager.di

import android.app.Application
import android.content.Context
import com.trendyol.android.devtools.sharedprefmanager.domain.SharedPrefUpdateTypeValidator

internal object ContextContainer {

val mainContainer by lazy {
MainContainer(
sharedPrefUseCaseContainer = sharedPrefUseCaseContainer,
sharedPrefUpdateTypeValidator = sharedPrefUpdateTypeValidator,
)
}

private val sharedPrefUseCaseContainer by lazy {
SharedPrefUseCaseContainer(application.applicationContext, sharedPrefName)
}
private val sharedPrefUpdateTypeValidator by lazy {
SharedPrefUpdateTypeValidator()
}
private lateinit var application: Application
private lateinit var sharedPrefName: String

fun getContext(): Context =
if (::application.isInitialized) {
application.applicationContext
} else {
throw NullPointerException("SharedPref Manager library is not initialized.")
}

fun initialize(application: Application) {
this.application = application
}

fun setSharedPrefName(sharedPrefName: String) {
this.sharedPrefName = sharedPrefName
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.trendyol.android.devtools.sharedprefmanager.di

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.trendyol.android.devtools.sharedprefmanager.domain.SharedPrefUpdateTypeValidator
import com.trendyol.android.devtools.sharedprefmanager.ui.SharedPrefManagerViewModel

internal class MainContainer(
private val sharedPrefUseCaseContainer: SharedPrefUseCaseContainer,
private val sharedPrefUpdateTypeValidator: SharedPrefUpdateTypeValidator
) {

inner class SharedPrefManagerViewModelFactory: ViewModelProvider.Factory {

@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return SharedPrefManagerViewModel(
sharedPrefManagerUseCase = sharedPrefUseCaseContainer.sharedPrefManagerUseCase,
sharedPrefUpdateTypeValidator = sharedPrefUpdateTypeValidator
) as T
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.trendyol.android.devtools.sharedprefmanager.di

import android.content.Context
import android.content.SharedPreferences
import com.trendyol.android.devtools.sharedprefmanager.domain.SharedPrefManagerRepository
import com.trendyol.android.devtools.sharedprefmanager.domain.SharedPrefManagerRepositoryImpl
import com.trendyol.android.devtools.sharedprefmanager.domain.SharedPrefManagerUseCase
import com.trendyol.android.devtools.sharedprefmanager.domain.SharedPrefManagerUseCaseImpl

internal class SharedPrefUseCaseContainer(
private val context: Context,
private val sharedPrefName: String
) {

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

private val sharedPrefManagerRepository: SharedPrefManagerRepository by lazy {
SharedPrefManagerRepositoryImpl(sharedPref)
}

val sharedPrefManagerUseCase: SharedPrefManagerUseCase by lazy {
SharedPrefManagerUseCaseImpl(sharedPrefManagerRepository)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.trendyol.android.devtools.sharedprefmanager.domain

import com.trendyol.android.devtools.sharedprefmanager.data.SharedPrefModel

interface SharedPrefManagerRepository {

suspend fun getAllSharedPref(): MutableMap<String, *>

suspend fun updateStringValue(key: String, newValue: String)

suspend fun updateIntValue(key: String, newValue: Int)

suspend fun updateBooleanValue(key: String, newValue: Boolean)

suspend fun updateLongValue(key: String, newValue: Long)

suspend fun updateFloatValue(key: String, newValue: Float)

suspend fun deleteSharedPrefItem(sharedPrefModel: SharedPrefModel)

suspend fun deleteAllSharedPref()

}
Loading
Loading