Skip to content

Commit

Permalink
[Add]: [PrefixKey]
Browse files Browse the repository at this point in the history
  • Loading branch information
old-traveler committed Dec 23, 2019
1 parent 194713d commit b96711e
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 19 deletions.
1 change: 1 addition & 0 deletions app/src/main/java/com/hyc/parrot/MyApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class MyApplication : Application() {
super.onCreate()
context = this.applicationContext
Parrot.initJsonConvert(MyJsonConvert())
Parrot.initDefaultPrefixProvider(MyPrefixProvider())
}

companion object {
Expand Down
28 changes: 28 additions & 0 deletions app/src/main/java/com/hyc/parrot/MyPrefixProvider.kt
Original file line number Diff line number Diff line change
@@ -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 ""
}
}
6 changes: 3 additions & 3 deletions app/src/main/java/com/hyc/parrot/ThreeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand All @@ -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));
}
});

Expand Down
16 changes: 8 additions & 8 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:layout_marginTop="100dp"
android:layout_gravity="center_horizontal"
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
android:text="Hello World!" />

</android.support.constraint.ConstraintLayout>


</LinearLayout>
25 changes: 17 additions & 8 deletions parrot_lib/src/main/java/com/hyc/parrot_lib/CacheAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
Expand Down Expand Up @@ -292,5 +301,5 @@ class CacheAdapter(private val dataConvert: DataConvert) {
}

interface PrefixProvider {
fun getRealKey(key: String): String
fun getKeyPrefix(key: String,prefixKey : String): String
}
1 change: 1 addition & 0 deletions parrot_lib/src/main/java/com/hyc/parrot_lib/InitParam.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@ annotation class InitCache(
vararg val value: String = [],
val spName: String = "",
val prefixField: String = "",
val prefixKey: String = "",
val onlyRead: Boolean = false
)

0 comments on commit b96711e

Please sign in to comment.