Skip to content
This repository has been archived by the owner on May 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #24 from 2tu/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
zongbintu authored Mar 7, 2017
2 parents fb02b54 + f56d200 commit 010be49
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 112 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ android:
components:
- tools
- platform-tools
- build-tools-25.0.0
- build-tools-25.0.2
- android-25
- extra-android-m2repository
script:
- chmod +x gradlew
- ./gradlew clean build
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Changelog

### Newest version: 0.3.2

* 为Fit添加get、edit方法

### Newest version: 0.3.1

* save和clear返回Editor
Expand Down
55 changes: 40 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Fit

[![Release](https://jitpack.io/v/2tu/fit.svg)](https://jitpack.io/#2tu/fit) [![Build Status](https://travis-ci.org/2tu/fit.svg?branch=master)](https://travis-ci.org/2tu/fit) [![Apache License](http://img.shields.io/hexpm/l/plug.svg?style=flat)](https://github.com/kentarosasaki/raspberrypi/blob/master/LICENSE)
[![Release](https://jitpack.io/v/2tu/fit.svg)](https://jitpack.io/#2tu/fit)

Fit 使用SharedPreferences存储对象中的基本数据类型。利用APT编译时生成代码,与转成String及反射相比更快。

## support
* 基本类型
* 基本包装类型
* Set<String>
* Set&lt;String&gt;
* minSdkVersion 4


Expand All @@ -27,8 +27,8 @@ Add the following dependency to your `build.gradle` file:

```
dependencies {
compile 'com.github.2tu.fit:fit:0.3.1'
annotationProcessor 'com.github.2tu.fit:fit-compiler:0.3.1'
compile 'com.github.2tu.fit:fit:0.3.2'
annotationProcessor 'com.github.2tu.fit:fit-compiler:0.3.2'
}
```

Expand All @@ -38,23 +38,48 @@ annotation model class.
@SharedPreferenceAble
```

save
save object
```java
User user = new User("Three.Tu");
Fit.save(this, user);
Fit.save(this, "user", user);
User user = new User();
Fit.save(context, user);
Fit.save(context, "user", user);
```
get
```java
User user = Fit.get(this, User.class);
User user = Fit.get(this, "user", User.class);
User user = Fit.get(context, User.class);
User user = Fit.get(context, "user", User.class);
```
clear
```java
Fit.clear(this, User.class);
Fit.clear(this, "user", User.class);
Fit.clear(context, User.class);
Fit.clear(context, "user");
```
other
```java
Fit.get(context, "name").getBoolean("isFirst", false);
SharedPreferences.Editor editor = Fit.edit(context, "name");
editor.putBoolean("isFirst", true);
editor.apply();
```


License
-------

Copyright 2016 Tu

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.



## What's new (0.3.1) - [Changelog](https://github.com/2tu/fit/blob/master/CHANGELOG.md)
* save和clear返回Editor
* 修复Double未校验空问题
[1]: http://2tu.github.com/fit/
31 changes: 15 additions & 16 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,29 @@ allprojects {
maven { url "https://jitpack.io" }
}
}

def supportLibraryVersion = '25.1.0'
ext {
minSdkVersion = 4
targetSdkVersion = 25
compileSdkVersion = 25
buildToolsVersion = '25.0.0'
buildToolsVersion = '25.0.2'
sourceCompatibilityVersion = JavaVersion.VERSION_1_7
targetCompatibilityVersion = JavaVersion.VERSION_1_7
}

ext.deps = [
// Android
android: 'com.google.android:android:4.0.1.2',
androidGradle: 'com.android.tools.build:gradle:2.2.2',

// Square
javapoet: 'com.squareup:javapoet:1.7.0',

// Test dependencies
autoservice: 'com.google.auto.service:auto-service:1.0-rc2',
compiletesting: 'com.google.testing.compile:compile-testing:0.9',
autocommon: 'com.google.auto:auto-common:0.6',
truth: 'com.google.truth:truth:0.28',
junit: 'junit:junit:4.12'
ext.deps = [// Android
android : 'com.google.android:android:4.0.1.2',
androidGradle : 'com.android.tools.build:gradle:2.2.2',
supportAnnotations: "com.android.support:support-annotations:$supportLibraryVersion",
// Square
javapoet : 'com.squareup:javapoet:1.7.0',

// Test dependencies
autoservice : 'com.google.auto.service:auto-service:1.0-rc2',
compiletesting : 'com.google.testing.compile:compile-testing:0.9',
autocommon : 'com.google.auto:auto-common:0.6',
truth : 'com.google.truth:truth:0.28',
junit : 'junit:junit:4.12'

]

Expand Down
17 changes: 0 additions & 17 deletions fit-compiler/src/main/java/fit/compiler/FitProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@ private TypeSpec createPreferenceClass(ClassName preferenceClassName, boolean is

result.addMethod(createPreferenceGetMethod(targetTypeName, fieldElements, setterElement));

result.addMethod(createPreferenceClearMethod());
return result.build();
}

Expand Down Expand Up @@ -458,22 +457,6 @@ private MethodSpec.Builder genGetCode(boolean isSetter, MethodSpec.Builder build
propertyName, defaultValue);
}

private MethodSpec createPreferenceClearMethod() {
MethodSpec.Builder result = MethodSpec.methodBuilder("clear")
.returns(SHARED_PREFERENCES_EDITOR)
.addAnnotation(Override.class)
.addModifiers(PUBLIC)
.addParameter(CONTEXT, "context")
.addParameter(STRING, "name");
result.addStatement(
"SharedPreferences.Editor editor = $T.getSharedPreferenceEditor(context, name).clear()",
UTILS);

result.addStatement("return editor");

return result.build();
}

private TypeName unbox(TypeName typeName) {
if (typeName.isBoxedPrimitive()) {
return typeName.unbox();
Expand Down
2 changes: 0 additions & 2 deletions fit-compiler/src/test/java/fit/MM.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,4 @@ public interface MM<T> {
SharedPreferences.Editor save(Context context, String name, T obj);

T get(Context context, String name);

SharedPreferences.Editor clear(Context context, String name);
}
28 changes: 0 additions & 28 deletions fit-compiler/src/test/java/fit/SharedPreferenceAbleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ public class SharedPreferenceAbleTest {
+ " obj.aT = sharedPreferences.getString(\"aT\", null);\n"
+ " return obj;\n"
+ " }\n"
+ " @Override public Editor clear(Context context, String name) {\n"
+ " SharedPreferences.Editor editor = Utils.getSharedPreferenceEditor(context, name).clear();\n"
+ " return editor;\n"
+ " }\n"
+ "}");

assertAbout(javaSource()).that(source)
Expand Down Expand Up @@ -87,10 +83,6 @@ public class SharedPreferenceAbleTest {
+ " obj.aT = sharedPreferences.getString(\"aT\", null);\n"
+ " return obj;\n"
+ " }\n"
+ " @Override public Editor clear(Context context, String name) {\n"
+ " SharedPreferences.Editor editor = Utils.getSharedPreferenceEditor(context, name).clear();"
+ " return editor;\n"
+ " }\n"
+ "}");

assertAbout(javaSource()).that(source)
Expand Down Expand Up @@ -130,10 +122,6 @@ public class SharedPreferenceAbleTest {
+ " obj.aT = sharedPreferences.getString(\"aT\", null);\n"
+ " return obj;\n"
+ " }\n"
+ " @Override public Editor clear(Context context, String name) {\n"
+ " SharedPreferences.Editor editor = Utils.getSharedPreferenceEditor(context, name).clear();"
+ " return editor;\n"
+ " }\n"
+ "}");

assertAbout(javaSource()).that(source)
Expand Down Expand Up @@ -175,10 +163,6 @@ public class SharedPreferenceAbleTest {
+ " obj.aT = sharedPreferences.getString(\"aT\", null);\n"
+ " return obj;\n"
+ " }\n"
+ " @Override public Editor clear(Context context, String name) {\n"
+ " SharedPreferences.Editor editor = Utils.getSharedPreferenceEditor(context, name).clear();\n"
+ " return editor;\n"
+ " }\n"
+ "}");

assertAbout(javaSource()).that(source)
Expand Down Expand Up @@ -231,10 +215,6 @@ public class SharedPreferenceAbleTest {
+ " obj.aT = sharedPreferences.getString(\"aT\", null);\n"
+ " return obj;\n"
+ " }\n"
+ " @Override public Editor clear(Context context, String name) {\n"
+ " SharedPreferences.Editor editor = Utils.getSharedPreferenceEditor(context, name).clear();\n"
+ " return editor;\n"
+ " }\n"
+ "}");

JavaFileObject sharedSource2 = JavaFileObjects.forSourceString("test/TestOne_Preference", ""
Expand All @@ -260,10 +240,6 @@ public class SharedPreferenceAbleTest {
+ " obj.aT = sharedPreferences.getString(\"aT\", null);\n"
+ " return obj;\n"
+ " }\n"
+ " @Override public Editor clear(Context context, String name) {\n"
+ " SharedPreferences.Editor editor = Utils.getSharedPreferenceEditor(context, name).clear();\n"
+ " return editor;\n"
+ " }\n"
+ "}");

JavaFileObject sharedSource3 = JavaFileObjects.forSourceString("test/TestTwo_Preference", ""
Expand All @@ -287,10 +263,6 @@ public class SharedPreferenceAbleTest {
+ " obj.aT = sharedPreferences.getString(\"aT\", null);\n"
+ " return obj;\n"
+ " }\n"
+ " @Override public Editor clear(Context context, String name) {\n"
+ " SharedPreferences.Editor editor = Utils.getSharedPreferenceEditor(context, name).clear();"
+ " return editor;\n"
+ " }\n"
+ "}");

assertAbout(javaSources()).that(asList(source1, source2, source3))
Expand Down
7 changes: 5 additions & 2 deletions fit-compiler/src/test/java/fit/internal/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
@SuppressWarnings({ "deprecation", "WeakerAccess" }) // Used by generated code.
public final class Utils {

public static SharedPreferences getSharedPreference(Context context, String name) {
return context.getSharedPreferences(name, Context.MODE_PRIVATE);
}

public static SharedPreferences.Editor getSharedPreferenceEditor(Context context, String name) {
SharedPreferences sharedPreferences = context.getSharedPreferences(name, Context.MODE_PRIVATE);
return sharedPreferences.edit();
return getSharedPreference(context, name).edit();
}

public static void apply(SharedPreferences.Editor editor) {
Expand Down
1 change: 1 addition & 0 deletions fit/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ android {
}
dependencies {
compile project(':fit-annotations')
compile deps.supportAnnotations
}
Loading

0 comments on commit 010be49

Please sign in to comment.