Skip to content
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
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
activity = "1.7.2"
activity-compose = "1.8.0"
agp = "8.10.1"
encryptedprefs-ktx = "1.1.0"
java = "21"
annotation = "1.9.1"
appcompat = "1.6.1"
Expand Down Expand Up @@ -81,6 +82,7 @@ androidx-room-ktx = { group = "androidx.room", name = "room-ktx", version.ref =
androidx-room-runtime = { group = "androidx.room", name = "room-runtime", version.ref = "room" }
androidx-sqlite-ktx = { module = "androidx.sqlite:sqlite-ktx", version.ref = "sqlite-ktx" }
androidx-startup-runtime = { group = "androidx.startup", name = "startup-runtime", version.ref = "startup" }
encryptedprefs-ktx = { module = "dev.spght:encryptedprefs-ktx", version.ref = "encryptedprefs-ktx" }
google-android-material = { group = "com.google.android.material", name = "material", version.ref = "material" }
google-gson = { group = "com.google.code.gson", name = "gson", version.ref = "gson" }
jetbrains-kotlin-coroutines-core = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-core", version.ref = "coroutines" }
Expand Down
2 changes: 1 addition & 1 deletion libraries/sharedpref-manager/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ android {
}

group = "com.trendyol.android.devtools"
version = "0.2.0"
version = "0.3.0"

publishConfig {
defaultConfiguration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package com.trendyol.android.devtools.sharedprefmanager

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

object SharedPrefManager {
Expand All @@ -18,7 +20,18 @@ object SharedPrefManager {
}

fun show(sharedPrefName: String) {
ContextContainer.setSharedPrefName(sharedPrefName)
ContextContainer.setSharedPreferencesProvider(
SharedPreferencesProvider.ByName(sharedPrefName)
)
ContextContainer.getContext().startActivity(intent)
}

fun show(sharedPreferences: SharedPreferences) {
ContextContainer.setSharedPreferencesProvider(
SharedPreferencesProvider.ProvidedSharedPreferences(
sharedPreferences,
),
)
ContextContainer.getContext().startActivity(intent)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.trendyol.android.devtools.sharedprefmanager.di

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

internal object ContextContainer {
Expand All @@ -14,14 +15,13 @@ internal object ContextContainer {
}

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

private lateinit var sharedPreferencesProvider: SharedPreferencesProvider
fun getContext(): Context =
if (::application.isInitialized) {
application.applicationContext
Expand All @@ -33,7 +33,22 @@ internal object ContextContainer {
this.application = application
}

fun setSharedPrefName(sharedPrefName: String) {
this.sharedPrefName = sharedPrefName
fun setSharedPreferencesProvider(provider: SharedPreferencesProvider) {
this.sharedPreferencesProvider = provider
}
}

sealed class SharedPreferencesProvider {
abstract fun provide(context: Context): SharedPreferences
class ByName(val name: String) : SharedPreferencesProvider() {
override fun provide(context: Context): SharedPreferences {
return context.getSharedPreferences(name, Context.MODE_PRIVATE)
}
}

class ProvidedSharedPreferences(val sharedPreferences: SharedPreferences) : SharedPreferencesProvider() {
override fun provide(context: Context): SharedPreferences {
return sharedPreferences
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import com.trendyol.android.devtools.sharedprefmanager.domain.SharedPrefManagerU

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

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

private val sharedPrefManagerRepository: SharedPrefManagerRepository by lazy {
Expand Down
3 changes: 2 additions & 1 deletion sample/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ android {

defaultConfig {
applicationId = "com.trendyol.android.devtools"
minSdk = 21
minSdk = 23
versionCode = 1
versionName = "1.0"

Expand Down Expand Up @@ -58,6 +58,7 @@ dependencies {
implementation(project(":libraries:debug-toast"))
implementation(project(":libraries:deeplink-launcher"))
implementation(project(":libraries:sharedpref-manager"))
implementation(libs.encryptedprefs.ktx)

implementation(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat)
Expand Down
3 changes: 1 addition & 2 deletions sample/src/main/java/com/trendyol/android/devtools/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package com.trendyol.android.devtools

import android.app.Application
import com.trendyol.android.devtools.analyticslogger.AnalyticsLogger
import com.trendyol.android.devtools.debugactionitem.AnalyticsLoggerDebugActionItem
import com.trendyol.android.devtools.debugactionitem.DummyClickDebugActionItem
import com.trendyol.android.devtools.debugactionitem.DummySwitchDebugActionItem
import com.trendyol.android.devtools.debugactionitem.AnalyticsLoggerDebugActionItem
import com.trendyol.android.devtools.debugmenu.DebugMenu
import com.trendyol.android.devtools.debugtoast.DebugToast
import com.trendyol.android.devtools.sharedprefmanager.SharedPrefManager
Expand All @@ -19,7 +19,6 @@ class App : Application() {

// Environment Manager
EnvironmentManager.init(this)

// Debug Menu
DebugMenu.init(this)
val debugMenuItems = listOf(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.trendyol.android.devtools.ui.main

import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.edit
import androidx.fragment.app.Fragment
import com.trendyol.android.devtools.MainActivity
import com.trendyol.android.devtools.R
Expand All @@ -15,6 +17,8 @@ import com.trendyol.android.devtools.sharedprefmanager.SharedPrefManager
import com.trendyol.android.devtools.ui.login.LoginFragment
import com.trendyol.devtools.deeplinklauncher.DeepLinkLauncher
import com.trendyol.devtools.environmentmanager.EnvironmentManager
import dev.spght.encryptedprefs.EncryptedSharedPreferences
import dev.spght.encryptedprefs.MasterKey
import kotlin.random.Random

class MainFragment : Fragment() {
Expand Down Expand Up @@ -56,7 +60,9 @@ class MainFragment : Fragment() {
loadDummySharedPrefValues(sharedPrefName)
SharedPrefManager.show(sharedPrefName)
}

binding.buttonEncryptedSharedPrefManager.setOnClickListener {
SharedPrefManager.show(getEncryptedSharedPreferences())
}
binding.switchAnalyticsLogger.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
AnalyticsLogger.showNotification()
Expand Down Expand Up @@ -86,6 +92,24 @@ class MainFragment : Fragment() {
)
}

private fun getEncryptedSharedPreferences(): SharedPreferences {
return EncryptedSharedPreferences(
requireContext(),
"encrypted_shared_pref",
MasterKey(requireContext())
).apply {
// Clear previous values
this.edit(commit = true) { clear() }

// Add new values
this.edit {
putString("key_shared_pref_short_string", "Super secret string")
putInt("key_shared_pref_int", 123456)
putBoolean("key_shared_pref_boolean", true)
}
}
}

private fun loadDummySharedPrefValues(sharedPrefName: String) {
val sharedPref = requireContext().getSharedPreferences(sharedPrefName, Context.MODE_PRIVATE)
val edit = sharedPref.edit()
Expand Down
12 changes: 11 additions & 1 deletion sample/src/main/res/layout/main_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,19 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/buttonEncryptedSharedPrefManager"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:text="@string/encrypted_sharedpref_manager"
app:layout_constraintBottom_toTopOf="@id/buttonSharedPrefManager"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/buttonSharedPrefManager"
android:layout_width="match_parent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:text="@string/sharedpref_manager"
Expand Down
2 changes: 2 additions & 0 deletions sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
<string name="analytics_logger">Analytics Logger</string>
<string name="current_environment">Current Environment %1$s</string>
<string name="sharedpref_manager">SharedPref Manager - Beta</string>
<string name="encrypted_sharedpref_manager">Encrypted SharedPref</string>

</resources>
Loading