Skip to content

Commit

Permalink
review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
orhanobut committed Jul 15, 2015
1 parent b5da999 commit 98b62ac
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 6 deletions.
63 changes: 62 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ Hawk provides:

###Add dependency
```groovy
compile 'com.orhanobut:hawk:1.15'
compile 'com.orhanobut:hawk:1.16'
```

If you want to have Rx features, make sure to add Rx dependency

#### Initialize the hawk
```java
Hawk.init(context, PASSWORD); // might take 36-400ms depends on device
Expand Down Expand Up @@ -63,6 +65,31 @@ Hawk.chain()
.commit();
```

#### Save (Rx)
```java
Observable<Boolean> result = Hawk.putObservable(key, T); // Returns the result as boolean
```

example usage
```java
Hawk.putObservable(KEY, new Foo())
.observeOn(Schedulers.io())
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<Boolean>() {
@Override
public void onCompleted() {
}

@Override
public void onError(Throwable e) {
}

@Override
public void onNext(Boolean s) {
}
});
```

#### Get
```java
T result = Hawk.get(key);
Expand All @@ -73,6 +100,40 @@ or with default value
T result = Hawk.get(key, T);
```

#### Get Observable (Rx support)
To be able to use rx support, you need to add the dependency.
```java
Observable<T> result = Hawk.getObservable(key);
```
or with default value

```java
Observable<T> result = Hawk.getObservable(key, T);
```

example usage
```java
Hawk.<Foo>getObservable(KEY)
.observeOn(Schedulers.io())
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<Foo>() {
@Override
public void onCompleted() {
Log.d("rxtest", "completed");
}

@Override
public void onError(Throwable e) {
Log.d("rxtest", "error");
}

@Override
public void onNext(Foo s) {
Log.d("rxtest", s.toString());
}
});
```

#### Remove
```java
Hawk.remove(key); // Returns the result as boolean
Expand Down
18 changes: 17 additions & 1 deletion app/src/main/java/com/orhanobut/hawksample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,22 @@ protected void onCreate(Bundle savedInstanceState) {
}

private void testRx() {
Hawk.put(KEY, new Foo());
Hawk.putObservable(KEY, new Foo())
.observeOn(Schedulers.io())
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<Boolean>() {
@Override
public void onCompleted() {
}

@Override
public void onError(Throwable e) {
}

@Override
public void onNext(Boolean s) {
}
});

Hawk.<Foo>getObservable(KEY)
.observeOn(Schedulers.io())
Expand All @@ -93,6 +108,7 @@ public void onNext(Foo s) {
Log.d("rxtest", s.toString());
}
});

}

private void testHawkInitWithoutPassword() {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# org.gradle.parallel=true

# VERSION_NAME=1.10-SNAPSHOT
VERSION_NAME=1.16-SNAPSHOT
VERSION_NAME=1.16
VERSION_CODE=17
GROUP=com.orhanobut

Expand Down
8 changes: 5 additions & 3 deletions hawk/src/main/java/com/orhanobut/hawk/Hawk.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public static <T> boolean put(String key, T value) {
}

/**
* Creates a stream to put data
* Creates a stream to put data, RxJava dependency is required
*
* @param <T> value type
* @return Observable<Boolean>
Expand All @@ -207,7 +207,7 @@ public void call(Subscriber<? super Boolean> subscriber) {

private static void checkRx() {
if (!Utils.hasRxJavaOnClasspath()) {
throw new NullPointerException("RxJava is not on classpath, " +
throw new NoClassDefFoundError("RxJava is not on classpath, " +
"make sure that you have it in your dependencies");
}
}
Expand Down Expand Up @@ -288,10 +288,11 @@ public static <T> T get(String key, T defaultValue) {

/**
* Creates a stream of data
* RxJava dependency is required
*
* @param key of the data
* @param <T> type of the data
* @return Observable</T>
* @return Observable<T>
*/
public static <T> Observable<T> getObservable(String key) {
checkRx();
Expand All @@ -300,6 +301,7 @@ public static <T> Observable<T> getObservable(String key) {

/**
* Creates a stream of data
* RxJava dependency is required
*
* @param key of the data
* @param defaultValue of the default value if the value doesn't exists
Expand Down

0 comments on commit 98b62ac

Please sign in to comment.