[desc]:添加MMKV的支持

[author]:wangyimiao
master
yimiao 3 years ago
parent 2e58b77dcc
commit 29db3ecd46

@ -2,6 +2,7 @@ package com.common.commonlibtest;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
@ -15,6 +16,7 @@ import com.common.commonlib.net.interceptor.RequestHeadInterceptor;
import com.common.commonlib.net.interceptor.ResponseHeadInterceptor; import com.common.commonlib.net.interceptor.ResponseHeadInterceptor;
import com.common.commonlib.utils.BaseUtils; import com.common.commonlib.utils.BaseUtils;
import com.common.commonlib.utils.FTPUtils; import com.common.commonlib.utils.FTPUtils;
import com.common.commonlib.utils.MMKVUtils;
import com.common.commonlibtest.bean.ArticlesResponse; import com.common.commonlibtest.bean.ArticlesResponse;
import com.common.commonlibtest.bean.CollectionResponse; import com.common.commonlibtest.bean.CollectionResponse;
import com.common.commonlibtest.bean.LoginResponse; import com.common.commonlibtest.bean.LoginResponse;
@ -78,7 +80,8 @@ public class DemoActivity extends AppCompatActivity {
binding.btn.setOnClickListener(l -> { binding.btn.setOnClickListener(l -> {
List<RequestHeadInterceptor> interceptors = new ArrayList<>(); List<RequestHeadInterceptor> interceptors = new ArrayList<>();
RequestHeadInterceptor.Builder builder = new RequestHeadInterceptor.Builder(); RequestHeadInterceptor.Builder builder = new RequestHeadInterceptor.Builder();
builder.addHeadParams("Cookie", "loginUserName_wanandroid_com=wang11;token_pass_wanandroid_com=6fbec4fe15d69af8b4531171798f3926;loginUserName=wang11;token_pass=6fbec4fe15d69af8b4531171798f3926"); builder.addHeadParams("Cookie",
"loginUserName_wanandroid_com=wang11;token_pass_wanandroid_com=6fbec4fe15d69af8b4531171798f3926;loginUserName=wang11;token_pass=6fbec4fe15d69af8b4531171798f3926");
interceptors.add(builder.build()); interceptors.add(builder.build());
CollectionLoader loader = new CollectionLoader(interceptors); CollectionLoader loader = new CollectionLoader(interceptors);
loader.getCollections(0, new RequestNoResultCallBack<CollectionResponse>() { loader.getCollections(0, new RequestNoResultCallBack<CollectionResponse>() {
@ -119,11 +122,18 @@ public class DemoActivity extends AppCompatActivity {
Thread thread = new Thread(new Runnable() { Thread thread = new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
FTPUtils.RESULT result = FTPUtils.INSTANCE.ftpUpload("192.168.101.215", "2221", "admin", "123456", filePath, fileName); FTPUtils.RESULT result = FTPUtils.INSTANCE.ftpUpload("192.168.101.215", "2221", "admin", "123456",
filePath, fileName);
Logger.INSTANCE.d(result.name()); Logger.INSTANCE.d(result.name());
} }
}); });
thread.start(); thread.start();
}); });
binding.mmkv.setOnClickListener(l -> {
MMKVUtils.INSTANCE.put("key", 100, "aa");
int value = MMKVUtils.INSTANCE.getInt("key", "bb");
Toast.makeText(DemoActivity.this, ""+value, Toast.LENGTH_SHORT).show();
});
} }
} }

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
@ -93,4 +94,16 @@
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.83" /> app:layout_constraintVertical_bias="0.83" />
<Button
android:id="@+id/mmkv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="mmkv test"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.505"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.9" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

@ -38,6 +38,7 @@ project.ext {
kotlin_android : "1.4.1", kotlin_android : "1.4.1",
gson : "2.8.6", gson : "2.8.6",
arouter : "1.5.2", arouter : "1.5.2",
mmkv : "1.2.10",
] ]
dependencies = [ dependencies = [
@ -65,6 +66,7 @@ project.ext {
gson : "com.google.code.gson:gson:${versions.gson}", gson : "com.google.code.gson:gson:${versions.gson}",
arouter : "com.alibaba:arouter-api:${versions.arouter}", arouter : "com.alibaba:arouter-api:${versions.arouter}",
arouter_compiler : "com.alibaba:arouter-compiler:${versions.arouter}", arouter_compiler : "com.alibaba:arouter-compiler:${versions.arouter}",
mmkv : "com.tencent:mmkv-static:${versions.mmkv}",
] ]
/** /**

@ -25,6 +25,8 @@ dependencies {
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5' implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5' implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
implementation(rootProject.ext.dependencies.mmkv)
// kotlin // kotlin
implementation rootProject.ext.dependencies.kotlin implementation rootProject.ext.dependencies.kotlin
implementation rootProject.ext.dependencies.kotlin_android implementation rootProject.ext.dependencies.kotlin_android

@ -3,6 +3,7 @@ package com.common.commonlib
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.Application import android.app.Application
import android.content.Context import android.content.Context
import com.tencent.mmkv.MMKV
/** /**
* 基础Application * 基础Application
@ -19,6 +20,9 @@ open class CommonApplication : Application() {
private fun intLibs(context: Context) { private fun intLibs(context: Context) {
commonContext = context commonContext = context
// 初始化MMKV
MMKV.initialize(this)
} }
companion object { companion object {

@ -0,0 +1,73 @@
package com.common.commonlib.utils
import com.common.commonlib.log.Logger.e
import com.tencent.mmkv.MMKV
/**
* MMKV的工具类
*
* @author wangym
* @since 2021-12-14
*/
object MMKVUtils {
const val TAG = "MMKVUtils"
/**
* 保存数据
* @param key
* @param value
* @param args 第一位自定义保存ID
* 第二位是否支持跨进程
*/
fun <T> put(key: String, value: T, vararg args: Any) {
val kv: MMKV = getKV(*args)
when (value) {
is String -> kv.putString(key, value)
is Boolean -> kv.putBoolean(key, value)
is Int -> kv.putInt(key, value)
is Float -> kv.putFloat(key, value)
is Long -> kv.putLong(key, value)
is ByteArray -> kv.putBytes(key, value)
is Set<*> -> kv.putStringSet(key, value as Set<String>)
else -> {
e(TAG, "got error value type,please check")
}
}
}
fun getByteArray(key: String, vararg args: Any): ByteArray? {
return getKV(*args).getBytes(key, null)
}
fun getBoolean(key: String, vararg args: Any): Boolean {
return getKV(*args).getBoolean(key, false)
}
fun getFloat(key: String, vararg args: Any): Float {
return getKV(*args).getFloat(key, 0f)
}
fun getInt(key: String, vararg args: Any): Int {
return getKV(*args).getInt(key, 0)
}
fun getLong(key: String, vararg args: Any): Long {
return getKV(*args).getLong(key, 0L)
}
fun getString(key: String, vararg args: Any): String? {
return getKV(*args).getString(key, "")
}
fun getStringSet(key: String, vararg args: Any): Set<String>? {
return getKV(*args).getStringSet(key, null)
}
private fun getKV(vararg args: Any): MMKV {
return when (args.size) {
1 -> MMKV.mmkvWithID(args[0] as String)
2 -> MMKV.mmkvWithID(args[0] as String, MMKV.MULTI_PROCESS_MODE)
else -> MMKV.defaultMMKV()
}
}
}
Loading…
Cancel
Save