diff --git a/app/src/main/java/com/hyc/parrot/MyApplication.kt b/app/src/main/java/com/hyc/parrot/MyApplication.kt
index 91b39b6..56ed79c 100644
--- a/app/src/main/java/com/hyc/parrot/MyApplication.kt
+++ b/app/src/main/java/com/hyc/parrot/MyApplication.kt
@@ -16,6 +16,7 @@ class MyApplication : Application() {
super.onCreate()
context = this.applicationContext
Parrot.initJsonConvert(MyJsonConvert())
+ Parrot.initDefaultPrefixProvider(MyPrefixProvider())
}
companion object {
diff --git a/app/src/main/java/com/hyc/parrot/MyPrefixProvider.kt b/app/src/main/java/com/hyc/parrot/MyPrefixProvider.kt
new file mode 100644
index 0000000..a9f510e
--- /dev/null
+++ b/app/src/main/java/com/hyc/parrot/MyPrefixProvider.kt
@@ -0,0 +1,28 @@
+package com.hyc.parrot
+
+import android.annotation.SuppressLint
+import com.hyc.parrot_lib.PrefixProvider
+import java.text.SimpleDateFormat
+import java.util.Date
+
+/**
+ * @author: 贺宇成
+ * @date: 2019-12-23 09:44
+ * @desc:
+ */
+class MyPrefixProvider : PrefixProvider {
+
+ companion object {
+ const val DATE = "date"
+ }
+
+ @SuppressLint("SimpleDateFormat")
+ override fun getKeyPrefix(key: String, prefixKey: String): String {
+ if (prefixKey == DATE) {
+ val date = Date()
+ val format = SimpleDateFormat("yyyy-MM-dd")
+ return format.format(date)
+ }
+ return ""
+ }
+}
\ No newline at end of file
diff --git a/app/src/main/java/com/hyc/parrot/ThreeActivity.java b/app/src/main/java/com/hyc/parrot/ThreeActivity.java
index 852dace..1d334b3 100644
--- a/app/src/main/java/com/hyc/parrot/ThreeActivity.java
+++ b/app/src/main/java/com/hyc/parrot/ThreeActivity.java
@@ -56,7 +56,7 @@ public class ThreeActivity extends BaseActivity {
private String prefix;
- @InitCache(value = "clickCount",prefixField = "prefix")
+ @InitCache(value = "clickCount",prefixKey = MyPrefixProvider.DATE)
private int clickCount = 0;
@@ -67,11 +67,11 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
printParam();
final TextView tvTitle = findViewById(R.id.tv_title);
- tvTitle.setText("共计点击了多少次:"+clickCount);
+ tvTitle.setText("今日共计点击了多少次:"+clickCount);
tvTitle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
- tvTitle.setText("共计点击了多少次:"+(++clickCount));
+ tvTitle.setText("今日共计点击了多少次:"+(++clickCount));
}
});
diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml
index 3d953fe..4b095b7 100644
--- a/app/src/main/res/layout/activity_main.xml
+++ b/app/src/main/res/layout/activity_main.xml
@@ -1,19 +1,19 @@
-
+ android:text="Hello World!" />
-
\ No newline at end of file
+
+
+
\ No newline at end of file
diff --git a/parrot_lib/src/main/java/com/hyc/parrot_lib/CacheAdapter.kt b/parrot_lib/src/main/java/com/hyc/parrot_lib/CacheAdapter.kt
index 75f5ca1..4d40d37 100644
--- a/parrot_lib/src/main/java/com/hyc/parrot_lib/CacheAdapter.kt
+++ b/parrot_lib/src/main/java/com/hyc/parrot_lib/CacheAdapter.kt
@@ -43,14 +43,23 @@ class CacheAdapter(private val dataConvert: DataConvert) {
}
private fun getRealKey(any: Any, initCache: InitCache, index: Int = 0): String {
- if (initCache.prefixField.isNotEmpty()) {
- val field = any::class.java.getDeclaredField(initCache.prefixField)
- field.enableAccessible()
- return field.get(any).toString()
- } else if (any is PrefixProvider) {
- return any.getRealKey(initCache.value[index])
+ val originKey = initCache.value[index]
+ return when {
+ initCache.prefixField.isNotEmpty() -> {
+ val field = any::class.java.getDeclaredField(initCache.prefixField)
+ field.enableAccessible()
+ "${field.get(any)}-$originKey"
+ }
+ any is PrefixProvider && initCache.prefixKey.isNotEmpty() -> "${any.getKeyPrefix(
+ originKey,
+ initCache.prefixKey
+ )}-$originKey"
+ defaultPrefixProvider != null && initCache.prefixKey.isNotEmpty() -> "${defaultPrefixProvider!!.getKeyPrefix(
+ originKey,
+ initCache.prefixKey
+ )}-$originKey"
+ else -> originKey
}
- return defaultPrefixProvider?.getRealKey(initCache.value[index]) ?: initCache.value[index]
}
private fun getRealKeyList(any: Any, initCache: InitCache): List {
@@ -292,5 +301,5 @@ class CacheAdapter(private val dataConvert: DataConvert) {
}
interface PrefixProvider {
- fun getRealKey(key: String): String
+ fun getKeyPrefix(key: String,prefixKey : String): String
}
\ No newline at end of file
diff --git a/parrot_lib/src/main/java/com/hyc/parrot_lib/InitParam.kt b/parrot_lib/src/main/java/com/hyc/parrot_lib/InitParam.kt
index ad78f0a..bbc63d8 100644
--- a/parrot_lib/src/main/java/com/hyc/parrot_lib/InitParam.kt
+++ b/parrot_lib/src/main/java/com/hyc/parrot_lib/InitParam.kt
@@ -56,5 +56,6 @@ annotation class InitCache(
vararg val value: String = [],
val spName: String = "",
val prefixField: String = "",
+ val prefixKey: String = "",
val onlyRead: Boolean = false
)
\ No newline at end of file