From 98b62ac1f9731fda35c9040ea92ad95c72cabd5c Mon Sep 17 00:00:00 2001 From: orhanobut Date: Wed, 15 Jul 2015 11:04:00 +0200 Subject: [PATCH] review fixes --- README.md | 63 ++++++++++++++++++- .../orhanobut/hawksample/MainActivity.java | 18 +++++- gradle.properties | 2 +- .../main/java/com/orhanobut/hawk/Hawk.java | 8 ++- 4 files changed, 85 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 7b106e7..1320daf 100644 --- a/README.md +++ b/README.md @@ -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 @@ -63,6 +65,31 @@ Hawk.chain() .commit(); ``` +#### Save (Rx) +```java +Observable 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() { + @Override + public void onCompleted() { + } + + @Override + public void onError(Throwable e) { + } + + @Override + public void onNext(Boolean s) { + } + }); +``` + #### Get ```java T result = Hawk.get(key); @@ -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 result = Hawk.getObservable(key); +``` +or with default value + +```java +Observable result = Hawk.getObservable(key, T); +``` + +example usage +```java +Hawk.getObservable(KEY) + .observeOn(Schedulers.io()) + .subscribeOn(AndroidSchedulers.mainThread()) + .subscribe(new Subscriber() { + @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 diff --git a/app/src/main/java/com/orhanobut/hawksample/MainActivity.java b/app/src/main/java/com/orhanobut/hawksample/MainActivity.java index 5227b9e..79fddda 100644 --- a/app/src/main/java/com/orhanobut/hawksample/MainActivity.java +++ b/app/src/main/java/com/orhanobut/hawksample/MainActivity.java @@ -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() { + @Override + public void onCompleted() { + } + + @Override + public void onError(Throwable e) { + } + + @Override + public void onNext(Boolean s) { + } + }); Hawk.getObservable(KEY) .observeOn(Schedulers.io()) @@ -93,6 +108,7 @@ public void onNext(Foo s) { Log.d("rxtest", s.toString()); } }); + } private void testHawkInitWithoutPassword() { diff --git a/gradle.properties b/gradle.properties index 0a4268f..15e59f2 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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 diff --git a/hawk/src/main/java/com/orhanobut/hawk/Hawk.java b/hawk/src/main/java/com/orhanobut/hawk/Hawk.java index e21496e..a52eb5b 100644 --- a/hawk/src/main/java/com/orhanobut/hawk/Hawk.java +++ b/hawk/src/main/java/com/orhanobut/hawk/Hawk.java @@ -180,7 +180,7 @@ public static boolean put(String key, T value) { } /** - * Creates a stream to put data + * Creates a stream to put data, RxJava dependency is required * * @param value type * @return Observable @@ -207,7 +207,7 @@ public void call(Subscriber 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"); } } @@ -288,10 +288,11 @@ public static T get(String key, T defaultValue) { /** * Creates a stream of data + * RxJava dependency is required * * @param key of the data * @param type of the data - * @return Observable + * @return Observable */ public static Observable getObservable(String key) { checkRx(); @@ -300,6 +301,7 @@ public static Observable 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