Skip to content

Commit

Permalink
Merge pull request #53 from orhanobut/oo/rx
Browse files Browse the repository at this point in the history
rx support
  • Loading branch information
yekmer committed Jul 15, 2015
2 parents 28db5d9 + 98b62ac commit 137c9ce
Show file tree
Hide file tree
Showing 10 changed files with 428 additions and 139 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
19 changes: 14 additions & 5 deletions app/src/main/java/com/orhanobut/hawksample/Foo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@
* @author Orhan Obut
*/
public class Foo {
int a = 1;
int b = 3;
String c = "asdfasdf";
int a = 1;
int b = 3;
String c = "asdfasdf";

void test() {
void test() {

}
}

@Override
public String toString() {
return "Foo{" +
"a=" + a +
", b=" + b +
", c='" + c + '\'' +
'}';
}
}
108 changes: 77 additions & 31 deletions app/src/main/java/com/orhanobut/hawksample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.orhanobut.hawk.Hawk;
import com.orhanobut.hawk.LogLevel;

import java.lang.reflect.Type;
import java.util.ArrayList;
Expand All @@ -18,6 +17,10 @@
import java.util.Map;
import java.util.Set;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


public class MainActivity extends Activity {

Expand All @@ -31,38 +34,81 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

long start = System.nanoTime();
Hawk.initWithoutEncryption(this, LogLevel.FULL);
long end = System.nanoTime();
// long start = System.nanoTime();
// Hawk.initWithoutEncryption(this, LogLevel.FULL);
// long end = System.nanoTime();
//
// double resultHawk = getTime(start, end);
// log("Hawk init with password : " + resultHawk);
//
// start = System.nanoTime();
// prefs = getSharedPreferences("BENCHARMK", MODE_PRIVATE);
// editor = prefs.edit();
// end = System.nanoTime();
// double resultPref = getTime(start, end);
// log("init: Prefs: " + resultPref + " Hawk : " + resultHawk);
//
//
// benchmarkPrimitivePut();
// benchmarkStringPut();
// benchmarkListObjectPut();
// benchmarkListStringPut();
// benchmarkObjectPut();
// benchmarkMapPut();
// benchmarkSetPut();
//
// benchmarkPrimitiveGet();
// benchmarkStringGet();
// benchmarkListObjectGet();
// benchmarkListStringGet();
// benchmarkObjectGet();
// benchmarkMapGet();
// benchmarkSetGet();
//
// benchmarkDelete();

double resultHawk = getTime(start, end);
log("Hawk init with password : " + resultHawk);
Hawk.init(this);
testRx();
}

private void testRx() {
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())
.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());
}
});

start = System.nanoTime();
prefs = getSharedPreferences("BENCHARMK", MODE_PRIVATE);
editor = prefs.edit();
end = System.nanoTime();
double resultPref = getTime(start, end);
log("init: Prefs: " + resultPref + " Hawk : " + resultHawk);


benchmarkPrimitivePut();
benchmarkStringPut();
benchmarkListObjectPut();
benchmarkListStringPut();
benchmarkObjectPut();
benchmarkMapPut();
benchmarkSetPut();

benchmarkPrimitiveGet();
benchmarkStringGet();
benchmarkListObjectGet();
benchmarkListStringGet();
benchmarkObjectGet();
benchmarkMapGet();
benchmarkSetGet();

benchmarkDelete();
}

private void testHawkInitWithoutPassword() {
Expand Down
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.1'
classpath 'com.android.tools.build:gradle:1.2.3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files

}
}

Expand Down
5 changes: 2 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
# org.gradle.parallel=true

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

POM_DESCRIPTION=Secure, Advanced Storage for android
Expand Down
29 changes: 17 additions & 12 deletions hawk/build.gradle
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
compileSdkVersion 21
buildToolsVersion "21.1.2"

defaultConfig {
minSdkVersion 8
targetSdkVersion 21
}

defaultConfig {
minSdkVersion 8
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
lintOptions {
abortOnError false
}
}

lintOptions {
abortOnError false
}
configurations {
optional
compile.extendsFrom optional
}

dependencies {
compile 'com.google.code.gson:gson:2.3.1'
compile 'com.google.code.gson:gson:2.3.1'
optional 'io.reactivex:rxandroid:0.25.0'
}

apply from: '../maven_push.gradle'
65 changes: 65 additions & 0 deletions hawk/src/androidTest/java/com/orhanobut/hawk/HawkRxTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.orhanobut.hawk;

import android.content.Context;
import android.test.InstrumentationTestCase;
import android.util.Log;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
* @author Orhan Obut
*/
public class HawkRxTest extends InstrumentationTestCase {

Context context;

@Override
protected void setUp() throws Exception {
super.setUp();
System.setProperty(
"dexmaker.dexcache",
getInstrumentation().getTargetContext().getCacheDir().getPath());
context = getInstrumentation().getContext();
Hawk.init(context, "testPassword");
}

@Override
protected void tearDown() throws Exception {
super.tearDown();
context = null;
Hawk.clear();
Hawk.resetCrypto();
}

private static final String KEY = "TAG";

public void testString() throws Exception {
Hawk.put(KEY, "hawk");

Hawk.<String>getObservable(KEY)
.observeOn(Schedulers.io())
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<String>() {
@Override
public void onCompleted() {
Log.d("rxtest", "completed");
assertTrue(true);
}

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

@Override
public void onNext(String s) {
Log.d("rxtest", s);
assertTrue(true);
}
});
}

}
Loading

0 comments on commit 137c9ce

Please sign in to comment.