All Projects → EspoirX → ElegantData

EspoirX / ElegantData

Licence: other
像操作Room一样操作 SharedPreferences 和 File 文件.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to ElegantData

Android Debug Database
A library for debugging android databases and shared preferences - Make Debugging Great Again
Stars: ✭ 7,946 (+44044.44%)
Mutual labels:  room, sharedpreferences
ToDoApp
📱My android playground app - Simple and Fastest todo app - developing to cover most android concepts, simple logic can make me focus more on framework
Stars: ✭ 28 (+55.56%)
Mutual labels:  room, sharedpreferences
Lazer Database
PHP flat file database to store data with JSON
Stars: ✭ 254 (+1311.11%)
Mutual labels:  file, db
Android-ORM-Benchmarks
No description or website provided.
Stars: ✭ 25 (+38.89%)
Mutual labels:  room, db
github-diff-explorer
The GitHub Diff Explorer is a Chrome/Firefox extension that generates a seamless file explorer with a minimalist focus.
Stars: ✭ 27 (+50%)
Mutual labels:  file
code-sync
Collaborative cloud platform for students, teachers, and professionals.
Stars: ✭ 28 (+55.56%)
Mutual labels:  room
kotlin-mvvm-boilerplate
💡💡 An Android boilerplate project with: Kotlin, MVVM, Room, Dagger2, RxJava, Retrofit and more.
Stars: ✭ 32 (+77.78%)
Mutual labels:  room
Simple-Notes-Kotlin-App
✍️ Simple Note Making App use mvvm architecture , dagger , coroutines and navigation component. Features includes 🗒️ create , edit and ❌ delete notes
Stars: ✭ 40 (+122.22%)
Mutual labels:  room
DailyBugle
📰Modern MVVM Android application following single activity architecture which fetches news from 🕷️ news API. this repository contains some best practices ⚡ of android development
Stars: ✭ 17 (-5.56%)
Mutual labels:  sharedpreferences
dbclient
데이터배이스 관리 / 자동 메일링 / Admin 자동화 / Database IDE Tool. SQL Development Helper. Support DBMS Oracle/Mysql/MS-SQL
Stars: ✭ 35 (+94.44%)
Mutual labels:  db
Part-DB
Open Source Electronic Parts Database using PHP and MySQL
Stars: ✭ 143 (+694.44%)
Mutual labels:  db
MusicX
MusicX is a music player 🎵 android app built using Kotlin and Jetpack Compose. It follows M.A.D. practices and hence is a good learning resource for beginners
Stars: ✭ 85 (+372.22%)
Mutual labels:  room
ad-automoli
💡 Fully automatic light management based on conditions like motion, illuminance, humidity, and other clever features
Stars: ✭ 99 (+450%)
Mutual labels:  room
simple-preferences
Android Library to simplify SharedPreferences use with code generation.
Stars: ✭ 48 (+166.67%)
Mutual labels:  sharedpreferences
cypress-upload-file-post-form
Solution for two Cypress testing use-cases I came across with: perform a direct http FORM request to the server containing a file and other parameters and upload a file into a form before submission
Stars: ✭ 59 (+227.78%)
Mutual labels:  file
fsify
Convert an array of objects into a persistent or temporary directory structure.
Stars: ✭ 24 (+33.33%)
Mutual labels:  file
kesho
store cache any data type string, boolean, jsonObject, jsonArray, .....
Stars: ✭ 19 (+5.56%)
Mutual labels:  sharedpreferences
webtrekk-android-sdk-v5
Webtrekk Android SDK V5
Stars: ✭ 13 (-27.78%)
Mutual labels:  room
filecloud
仿百度网盘,个人云盘。 file cloud
Stars: ✭ 130 (+622.22%)
Mutual labels:  file
WiFi-Direct-File-Transfer-App
WiFi Direct File Transfer is a experimental app that will allow sharing of data between Android devices running Android 4.0 or higher using a WiFi direct connection without the use of a WiFi access point. This will enable data transfer between devices without relying on any existing network infrastructure
Stars: ✭ 88 (+388.89%)
Mutual labels:  file

ElegantData

像操作Room一样操作 SharedPreferences 和 File 文件.

Download

相关文章

掘金

依赖

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.lzx.elegantData:code:1.0.0'
    annotationProcessor 'com.lzx.elegantData:compiler:1.0.0'
}

repositories {
    google()
    jcenter()
    maven {
        url "https://dl.bintray.com/lizixian/ElegantData"
    }
}

使用方法

一、创建一个接口:

@ElegantEntity(fileName = "UserInfo_Preferences")
public interface SharedPreferencesInfo extends ISharedPreferencesInfoDao {
    String keyUserName = "";
}
//或者
@ElegantEntity(fileName = "CacheFile.txt", fileType = ElegantEntity.TYPE_FILE)
public interface FileCacheInfo extends IFileCacheInfoDao {
    int keyPassword = 0;
}

加上@ElegantEntity注解,并且定义 fileName 文件名,fileType 文件类型,fileType 默认为 SharedPreferences 文件。

二、定义一个抽象类继承 ElegantDataBase

@ElegantDataMark
public abstract class AppDataBase extends ElegantDataBase {

    public abstract SharedPreferencesInfo getSharedPreferencesInfo();

    public abstract FileCacheInfo getFileCacheInfo();
}

让这个类加上 @ElegantDataMark 注解,并且定义在第一步中定义的接口的抽象方法。

然后后 ReBuild 一下,这时候会 build 报错,这时候会生成一个 IXXXDao 接口,然后让你的接口继承它,如上第一步所示。

三、使用单例模式去构建

@ElegantDataMark
public abstract class AppDataBase extends ElegantDataBase {

    public abstract SharedPreferencesInfo getSharedPreferencesInfo();

    public abstract FileCacheInfo getFileCacheInfo();


    private static AppDataBase spInstance;
    private static AppDataBase fileInstance;
    private static final Object sLock = new Object();

    //使用SP文件
    public static AppDataBase withSp() {
        synchronized (sLock) {
            if (spInstance == null) {
                spInstance = ElegantData
                        .preferenceBuilder(ElegantApplication.getContext(), AppDataBase.class)
                        .build();
            }
            return spInstance;
        }
    }

    //使用File文件
    public static AppDataBase withFile() {
        synchronized (sLock) {
            if (fileInstance == null) {
                String path = Environment.getExternalStorageDirectory() + "/ElegantFolder";
                fileInstance = ElegantData
                        .fileBuilder(ElegantApplication.getContext(), path, AppDataBase.class)
                        .build();
            }
            return fileInstance;
        }
    }
}

如果使用 SP 文件,调用 ElegantData#preferenceBuilder 方法去构建实例。
如果是 File 文件,则使用 ElegantData#fileBuilder 去构建。
两个方法都需要传入上下文和 AppDataBase 的 class。唯一不一样的是使用 File 文件需要先创建文件夹,所以在第二个参数传入的是创建文件夹的路径。

四、使用

经过上面三个步骤后,ReBuild 一下,然后就可以开始使用了。

//使用 SP 文件存入数据
AppDataBase.withSp().getSharedPreferencesInfo().putKeyUserName("小明");

//使用 File 文件存入数据
AppDataBase.withFile().getFileCacheInfo().putKeyPassword(123456789);


String userName = AppDataBase.withSp().getSharedPreferencesInfo().getKeyUserName();
Log.i("MainActivity", "userName = " + userName);

int password = AppDataBase.withFile().getFileCacheInfo().getKeyPassword();
Log.i("MainActivity", "password = " + password);

@IgnoreField

被 @IgnoreField 注解标记的字段,将不会被解析:

@ElegantEntity(fileName = "UserInfo_Preferences")
public interface SharedPreferencesInfo extends ISharedPreferencesInfoDao {
    String keyUserName = "";
    
    @IgnoreField
    int keyUserSex = 0;
}

Rebuild 后,keyUserSex 会被忽略,相关字段的方法不会被生成。

@NameField

被 @NameField 注解标记的字段,可以重命名:

@ElegantEntity(fileName = "UserInfo_Preferences")
public interface SharedPreferencesInfo extends ISharedPreferencesInfoDao {
    String keyUserName = "";
    
    @NameField(value = "sex")
    int keyUserSex = 0;
}

字段 keyUserSex 解析后生成的 put 和 get 方法是 putSex 和 getSex , 而不是 putUserSex 和 getUserSex。

@EntityClass

@EntityClass 注解用来标注实体类,如果你需要往文件中存入实体类,那么需要加上这个注解,否则会出错。

@ElegantEntity(fileName = "UserInfo_Preferences")
public interface SharedPreferencesInfo extends ISharedPreferencesInfoDao {
    String keyUserName = "";

    @EntityClass(value = SimpleJsonParser.class)
    User user = null;
}

如上所示,@EntityClass 注解需要传入一个 json 解析器,存入实体类的原理是把实体类通过解析器变成 json 字符串存入文件,取出来的时候 通过解析器解析 json 字符串变成实体类。

public class SimpleJsonParser extends JsonParser<User> {

    private Gson mGson;

    public SimpleJsonParser(Class<User> clazz) {
        super(clazz);
        mGson = new Gson();
    }

    @Override
    public String convertObject(User object) {
        return mGson.toJson(object);
    }

    @Override
    public User onParse(@NonNull String json)   {
        return mGson.fromJson(json, User.class);
    }
}

json 解析器需要实现两个方法,convertObject 方法作用是把实体类变成 json 字符串,onParse 方法作用是把 json 字符串变成 实体类。

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].