Skip to content

Commit

Permalink
Merge pull request #9 from sbxcloud/annotationlib
Browse files Browse the repository at this point in the history
Rx Java
  • Loading branch information
lgguzman authored Mar 24, 2017
2 parents c2b3035 + 9fdcee4 commit 2edf104
Show file tree
Hide file tree
Showing 7 changed files with 479 additions and 8 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Agregamos la librería como dependencia

dependencies {
//...otras dependencias de tu proyeco aquí.....
compile 'com.github.sbxcloud:androidlib:v2.0.3'
compile 'com.github.sbxcloud:androidlib:v2.1.0'
}
Esta librería se basa en annotaciones. Para crear tu propia Clase usuario puedes hacerla así:
Expand Down Expand Up @@ -366,6 +366,26 @@ Incluso puedes hacer fecth a los objetos

```

Además puedes utilizar RXJava para cualquira de las anteriores fuciones

```java

SbxQuery sbxquery= new SbxQuery(Product.class);
sbxquery.fetch(properties).find(Product.class).
subscribe(new Consumer<List<Product>>() {
@Override
public void accept(List<Product> products) throws Exception {
for (Product p : products) {
Log.e("Productos", p.name);
}
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
throwable.printStackTrace();
}
});
```



Expand Down
8 changes: 6 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {

minSdkVersion 15
targetSdkVersion 25
versionCode 2
versionName "2.0.3"
versionCode 0
versionName "2.1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}
Expand All @@ -36,6 +36,10 @@ dependencies {
compile 'com.squareup.okhttp3:okhttp:3.4.1'
//androidTestCompile 'com.android.support:support-annotations:24.0.0'
compile 'com.android.support:appcompat-v7:25.1.1'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
// Because RxAndroid releases are few and far between, it is recommended you also
// explicitly depend on RxJava's latest version for bug fixes and new features.
compile 'io.reactivex.rxjava2:rxjava:2.0.1'
testCompile 'junit:junit:4.12'
// testCompile 'org.mockito:mockito-core:1.10.19'
// testCompile 'org.json:json:20140107'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.sbxcloud.android.sbxcloudsdk.auth.SbxAuth;
import com.sbxcloud.android.sbxcloudsdk.auth.user.SbxAuthToken;
Expand All @@ -18,12 +19,22 @@
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.reactivestreams.Subscriber;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Scheduler;
import io.reactivex.Single;
import io.reactivex.SingleEmitter;
import io.reactivex.SingleOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
Expand Down Expand Up @@ -69,6 +80,68 @@ public void onResponse(Call call, Response response) throws IOException {
});
}

public <T extends SbxUser> Single<T> signUp(Class<T> type) throws Exception{
SbxUrlComposer sbxUrlComposer= SbxAuth.getDefaultSbxAuth().getUrlSigIn(this);
final Request request = ApiManager.getInstance().sbxUrlComposer2Request(sbxUrlComposer);
return Single.create(new SingleOnSubscribe<T> () {
@Override
public void subscribe(final SingleEmitter<T> e) throws Exception {
new Thread(new Runnable() {
@Override
public void run() {
try {
Response response= ApiManager.getInstance().getOkHttpClient().newCall(request).execute();
JSONObject jsonObject = new JSONObject(response.body().string());
if (jsonObject.getBoolean("success")) {
updateUser(jsonObject);
e.onSuccess((T)SbxUser.this);
//sucess
} else {
//error
e.onError(new Exception(jsonObject.getString("error")));
}
}catch (Exception ex){
e.onError(ex);
}

}
}).start();
}
});
}

public <T extends SbxUser> Single<T> logIn(Class<T> type) throws Exception{
SbxUrlComposer sbxUrlComposer= SbxAuth.getDefaultSbxAuth().getUrllogin(this);
final Request request = ApiManager.getInstance().sbxUrlComposer2Request(sbxUrlComposer);
return Single.create(new SingleOnSubscribe<T>() {
@Override
public void subscribe(final SingleEmitter<T> e) throws Exception {
new Thread(new Runnable() {
@Override
public void run() {
try {
Response response=ApiManager.getInstance().getOkHttpClient().newCall(request).execute();
JSONObject jsonObject = new JSONObject(response.body().string());
Log.e("user",jsonObject.toString());
if (jsonObject.getBoolean("success")) {
updateUser(jsonObject);
e.onSuccess((T)SbxUser.this);
//sucess
} else {
//error
e.onError(new Exception(jsonObject.getString("error")));
}
}catch (Exception ex){
e.onError(ex);
}
}
}).start();

}
});
}


public void logInBackground(final SbxSimpleResponse simpleResponse) throws Exception{
SbxUrlComposer sbxUrlComposer= SbxAuth.getDefaultSbxAuth().getUrllogin(this);
Request request = ApiManager.getInstance().sbxUrlComposer2Request(sbxUrlComposer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.Single;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.SingleEmitter;
import io.reactivex.SingleOnSubscribe;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
Expand Down Expand Up @@ -56,6 +62,153 @@ public void onResponse(Call call, Response response) throws IOException {
});
}

public <T extends SbxModel> Single<T> save(Class<T> type)throws Exception{
SbxUrlComposer sbxUrlComposer = SbxModelHelper.getUrlInsertOrUpdateRow(SbxModel.this);
final Request request = ApiManager.getInstance().sbxUrlComposer2Request(sbxUrlComposer);
ApiManager.getInstance().getOkHttpClient().newCall(request).execute();
return Single.create(new SingleOnSubscribe<T>() {
@Override
public void subscribe(final SingleEmitter<T> e) throws Exception {
new Thread(new Runnable() {
@Override
public void run() {
try {
Response response= ApiManager.getInstance().getOkHttpClient().newCall(request).execute();
JSONObject jsonObject = new JSONObject(response.body().string());
if (jsonObject.getBoolean("success")) {
updateKey(jsonObject);
e.onSuccess((T)SbxModel.this);
//sucess
} else {
//error
e.onError(new Exception(jsonObject.getString("error")));
}
}catch (Exception ex){
e.onError(ex);
}
}
}).start();

}
});

}


public static <T> Single <List<T>> saveMany(final List <T> list)throws Exception{
SbxUrlComposer sbxUrlComposer = SbxModelHelper.getUrlInsertOrUpdateRows(list);
final Request request = ApiManager.getInstance().sbxUrlComposer2Request(sbxUrlComposer);
ApiManager.getInstance().getOkHttpClient().newCall(request).execute();
return Single.create(new SingleOnSubscribe<List<T>>() {
@Override
public void subscribe(final SingleEmitter<List<T>> e) throws Exception {
new Thread(new Runnable() {
@Override
public void run() {
try {
Response response= ApiManager.getInstance().getOkHttpClient().newCall(request).execute();
JSONObject jsonObject = new JSONObject(response.body().string());
if (jsonObject.getBoolean("success")) {
updateKeys(jsonObject, list);
e.onSuccess(list);
//sucess
} else {
//error
e.onError(new Exception(jsonObject.getString("error")));
}
}catch (Exception ex){
e.onError(ex);
}
}
}).start();

}
});

}

public <T extends SbxModel>Single<T> fetch(Class<T> type)throws Exception{

String []keys = {SbxModelHelper.getKeyFromAnnotation(this)};
SbxQueryBuilder sbxQueryBuilder = (new SbxQuery(this.getClass())).sbxQueryBuilder;
SbxUrlComposer sbxUrlComposer = SbxModelHelper.getUrlQueryKeys(sbxQueryBuilder, keys);
final Request request = ApiManager.getInstance().sbxUrlComposer2Request(sbxUrlComposer);
return Single.create(new SingleOnSubscribe<T>() {
@Override
public void subscribe(final SingleEmitter<T> e) throws Exception {
new Thread(new Runnable() {
@Override
public void run() {
try {
Response response= ApiManager.getInstance().getOkHttpClient().newCall(request).execute();
JSONObject jsonObject = new JSONObject(response.body().string());
if (jsonObject.getBoolean("success")) {
JSONArray jsonArray=jsonObject.getJSONArray("results");
if(jsonArray.length()>0) {
SbxMagicComposer.getSbxModel(jsonArray.getJSONObject(0), SbxModel.this.getClass(),SbxModel.this, 0);

e.onSuccess((T)SbxModel.this);
}else{
e.onError(new Exception("key not found"));
}
//sucess
} else {
//error
e.onError(new Exception(jsonObject.getString("error")));
}
}catch (Exception ex){
e.onError(ex);
}
}
}).start();

}
});
}

public <T extends SbxModel>Single<T> fetch(Class<T> type,String []properties)throws Exception{
String []keys = {SbxModelHelper.getKeyFromAnnotation(this)};
SbxQueryBuilder sbxQueryBuilder = (new SbxQuery(this.getClass())).fetch(properties).sbxQueryBuilder;
SbxUrlComposer sbxUrlComposer = SbxModelHelper.getUrlQueryKeys(sbxQueryBuilder, keys);
final Request request = ApiManager.getInstance().sbxUrlComposer2Request(sbxUrlComposer);
return Single.create(new SingleOnSubscribe<T>() {
@Override
public void subscribe(final SingleEmitter<T> e) throws Exception {

new Thread(new Runnable() {
@Override
public void run() {
try {
Response response= ApiManager.getInstance().getOkHttpClient().newCall(request).execute();
JSONObject jsonObject = new JSONObject(response.body().string());
if (jsonObject.getBoolean("success")) {
JSONArray jsonArray=jsonObject.getJSONArray("results");
if(jsonArray.length()>0) {
if(jsonObject.has("fetched_results")) {
SbxMagicComposer.getSbxModel(jsonArray.getJSONObject(0), SbxModel.this.getClass(), SbxModel.this, 0,jsonObject.getJSONObject("fetched_results"));
}else{
SbxMagicComposer.getSbxModel(jsonArray.getJSONObject(0), SbxModel.this.getClass(), SbxModel.this, 0);
}

e.onSuccess((T)SbxModel.this);
}else{
e.onError(new Exception("key not found"));
}
//sucess
} else {
//error
e.onError(new Exception(jsonObject.getString("error")));
}
}catch (Exception ex){
e.onError(ex);
}
}
}).start();

}
});
}

public void fetchInBackground(final SbxSimpleResponse simpleResponse)throws Exception{

String []keys = {SbxModelHelper.getKeyFromAnnotation(this)};
Expand Down Expand Up @@ -134,6 +287,39 @@ public void onResponse(Call call, Response response) throws IOException {
});
}

public <T extends SbxModel>Single<T> delete(Class<T> type)throws Exception{

SbxQueryBuilder sbxQueryBuilder = SbxModelHelper.prepareQueryToDelete(this.getClass());
sbxQueryBuilder.addDeleteKey(SbxModelHelper.getKeyFromAnnotation(this));
SbxUrlComposer sbxUrlComposer = SbxModelHelper.getUrlDelete(sbxQueryBuilder);
final Request request = ApiManager.getInstance().sbxUrlComposer2Request(sbxUrlComposer);
return Single.create(new SingleOnSubscribe<T>() {
@Override
public void subscribe(final SingleEmitter<T> e) throws Exception {

new Thread(new Runnable() {
@Override
public void run() {
try {
Response response= ApiManager.getInstance().getOkHttpClient().newCall(request).execute();
JSONObject jsonObject = new JSONObject(response.body().string());
if(jsonObject.getBoolean("success")) {
e.onSuccess((T)SbxModel.this);
//sucess
} else {
//error
e.onError(new Exception(jsonObject.getString("error")));
}
}catch (Exception ex){
e.onError(ex);
}
}
}).start();

}
});
}



public void deleteInBackground(final SbxSimpleResponse simpleResponse)throws Exception{
Expand Down Expand Up @@ -171,6 +357,16 @@ private void updateKey(JSONObject jsonObject)throws Exception{
}
}

private static <T> void updateKeys(JSONObject jsonObject, List<T> list)throws Exception{
if(jsonObject.has("keys")) {
JSONArray key = jsonObject.getJSONArray("keys");
for (int i=0;i<key.length();i++){

SbxDataValidator.setKeyFromAnnotation(list.get(i), key.getString(i));
}
}
}



}
Loading

0 comments on commit 2edf104

Please sign in to comment.