Skip to content

Commit

Permalink
Merge pull request #8 from sbxcloud/annotationlib
Browse files Browse the repository at this point in the history
getCurrentUser
  • Loading branch information
lgguzman authored Mar 21, 2017
2 parents 829dc09 + 8cd6db2 commit c2b3035
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 18 deletions.
6 changes: 3 additions & 3 deletions 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.1'
compile 'com.github.sbxcloud:androidlib:v2.0.3'
}
Esta librería se basa en annotaciones. Para crear tu propia Clase usuario puedes hacerla así:
Expand Down Expand Up @@ -158,7 +158,7 @@ Luego, puedes conectarte con tus datos utilizando el cliente hhtp que consideres
```


Si deseas, puedes utilizar nuestras clases para mayor facilidad, puedes crear tu custom user heredando de SbxUser, y tus custom modelos heredando de SbxModel de esta forma:
Si deseas, puedes utilizar nuestras clases para mayor facilidad, puedes crear tu custom user heredando de SbxUser, y tus custom modelos heredando de SbxModel (Todas las clases que hereden deben tener un constructor vacío) de esta forma:

```java
public class User extends SbxUser {
Expand All @@ -178,7 +178,7 @@ public class User extends SbxUser {
@SbxAuthToken
String token;


public User(){}

public User(String name, String username, String email, String password) {
this.name = name;
Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ android {
minSdkVersion 15
targetSdkVersion 25
versionCode 2
versionName "2.0.2"
versionName "2.0.3"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

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

import okhttp3.Call;
Expand Down Expand Up @@ -83,7 +84,6 @@ public void onResponse(Call call, Response response) throws IOException {
JSONObject jsonObject = new JSONObject(response.body().string());
if(jsonObject.getBoolean("success")) {
updateUser(jsonObject);
SbxAuth.getDefaultSbxAuth().refreshToken(sbxUser);
simpleResponse.onSuccess(SbxUser.this);
}else{
simpleResponse.onError(new Exception(jsonObject.getString("error")));
Expand Down Expand Up @@ -114,29 +114,27 @@ public void updateUser(JSONObject jsonObject) throws Exception{
final Field[] variables = myClass.getDeclaredFields();
for (final Field variable : variables) {

Annotation annotation = variable.getAnnotation(SbxAuthToken.class);
Annotation annotation = variable.getAnnotation(SbxAuthToken.class);

if (annotation != null && annotation instanceof SbxAuthToken) {
try {
boolean isAccessible=variable.isAccessible();
variable.setAccessible(true);
variable.set(SbxUser.this,token);
variable.setAccessible(isAccessible);
break;
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new SbxAuthException(e);
}
}

annotation = variable.getAnnotation(SbxEmailField.class);
annotation = variable.getAnnotation(SbxEmailField.class);

if (annotation != null && annotation instanceof SbxEmailField) {
try {
boolean isAccessible=variable.isAccessible();
variable.setAccessible(true);
variable.set(SbxUser.this,email);
variable.setAccessible(isAccessible);
break;
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new SbxAuthException(e);
}
Expand All @@ -150,7 +148,6 @@ public void updateUser(JSONObject jsonObject) throws Exception{
variable.setAccessible(true);
variable.set(SbxUser.this,login);
variable.setAccessible(isAccessible);
break;
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new SbxAuthException(e);
}
Expand All @@ -164,7 +161,6 @@ public void updateUser(JSONObject jsonObject) throws Exception{
variable.setAccessible(true);
variable.set(SbxUser.this,name);
variable.setAccessible(isAccessible);
break;
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new SbxAuthException(e);
}
Expand All @@ -175,16 +171,18 @@ public void updateUser(JSONObject jsonObject) throws Exception{
this.memberOf=userJson.getJSONArray("member_of");
this.homeFolder=userJson.getString("home_folder_key");
sbxUser=SbxUser.this;
SbxAuth.getDefaultSbxAuth().refreshToken(SbxUser.this);
}

public static SbxUser getCurrentSbxUser()throws Exception {
public static SbxUser getCurrentSbxUser(Class<?> clazz)throws Exception {
if(sbxUser==null) {
SharedPreferences sharedPreferences = SbxAuth.getDefaultSbxAuth().getContext().getSharedPreferences(FILE_NAME,Context.MODE_PRIVATE);
String s=sharedPreferences.getString(FILE_NAME_USER,"");
if(s.equals("")) {
return null;
}
sbxUser= new SbxUser();
Constructor<?> ctor = clazz.getConstructor();
sbxUser = (SbxUser) ctor.newInstance();
sbxUser.updateUser(new JSONObject(s));
}

Expand All @@ -202,5 +200,4 @@ public String getHomeFolder() {
public JSONArray getMemberOf() {
return memberOf;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ public void onResponse(Call call, Response response) throws IOException {
public void fetchInBackground(final SbxSimpleResponse simpleResponse)throws Exception{

String []keys = {SbxModelHelper.getKeyFromAnnotation(this)};
for (String key:keys){
Log.e("key",key);
}
SbxQueryBuilder sbxQueryBuilder = (new SbxQuery(this.getClass())).sbxQueryBuilder;
SbxUrlComposer sbxUrlComposer = SbxModelHelper.getUrlQueryKeys(sbxQueryBuilder, keys);
Request request = ApiManager.getInstance().sbxUrlComposer2Request(sbxUrlComposer);
Expand Down

0 comments on commit c2b3035

Please sign in to comment.