Last active
August 29, 2015 13:57
-
-
Save eliantor/9527155 to your computer and use it in GitHub Desktop.
Dear Diary Android blog post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if( BaasUser.current() == null ) { | |
startLoginScreen(); | |
return; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public BaasDocument getData() { | |
String title = mTitle.getText().toString(); | |
String content = mContent.getText().toString(); | |
BaasDocument note = new BaasDocument("memos"); | |
note.putString("title",title); | |
note.putString("content",content); | |
return note; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private RequestToken mRefresh; | |
private void refreshDocuments(){ | |
if(!mDialog.isShowing()) mDialog.show(); | |
mRefresh = BaasDocument.fetchAll("memos",onRefresh); | |
} | |
private final BaasHandler<List<BaasDocument>> onRefresh = | |
new BaasHandler<List<BaasDocument>> () { | |
public void handle(BaasResult<List<BaasDocument>> docs) { | |
mDialog.dismiss(); | |
mRefresh = null; | |
try { | |
mListFragment.refresh(result.get()); | |
} catch(BaasInvalidException e) { | |
startLoginScreen(); | |
} catch(BaasException e) { | |
Log.e("ERROR","Error "+e.getMessage(),e); | |
Toast.makeText(NoteListActivity.this,"Error talking with the server",Toast.LENGTH_LONG).show(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
config.apiDomain="192.168.56.1"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if( item.getItemId() == R.id.logout_action ) { | |
BaasUser.current().logout(logoutHandler); | |
return true; | |
} | |
// ... | |
private final BaasHandler<Void> logoutHandler = | |
new BaasHandler<Void>(){ | |
@Override | |
public void handle(BaasResult<Void> res) { | |
onLogout(); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private RequestToken mAddToken; | |
private void saveOnBaasBox(BaasDocument document) { | |
mDialog.show(); | |
mAddToken = document.save(SaveMode.IGNORE_VERSION,uploadHandler); | |
} | |
private final BaasHandler<BaasDocument> uploadHandler = | |
new BaasHandler<BaasDocument>() { | |
@Override | |
public void handle(BaasResult<BaasDocument> doc) { | |
mDialog.dismiss(); | |
mAddToken = null; | |
if (doc.isSuccess()) { | |
setResult(RESULT_OK); | |
finish(); | |
} else { | |
if (doc.error() instanceof BaasInvalidSessionException) { | |
setResult(RESULT_SESSION_EXPIRED); | |
finish(); | |
} else { | |
setResult(RESULT_FAILED); | |
finish(); | |
Log.d("ERROR","Failed with error",doc.error()); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private RequestToken mSignupOrLogin; | |
//... | |
//... | |
private void signupWithBaasBox(boolean newUser){ | |
BaasUser user = BaasUser.withUserName(mUserName); | |
user.setPassword(mPassword); | |
if( newUser ) { | |
mSignupOrLogin = user.signup(onComplete); | |
} else { | |
mSignupOrLogin = user.login(onComplete); | |
} | |
} | |
//... | |
//... | |
private final BaasHandler<BaasUser> onComplete = | |
new BaasHandler<BaasUser>() { | |
@Override | |
public void handle(BaasResult<BaasUser> result) { | |
mSignupOrLogin = null; | |
if (result.isFailed()) { | |
Log.d("ERROR","ERROR",result.error()); | |
} | |
completeLogin(result.isSuccess()); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static final String REFRESH_TOKEN_KEY = "refresh"; | |
private RequestToken mRefresh; | |
protected void onCreate(Bundle savedInstanceState) { | |
//... | |
if(savedInstanceState!=null) { | |
mRefresh = RequestToken.loadAndResume(savedInstanceState,REFRESH_TOKEN_KEY,onRefresh); | |
} | |
} | |
protected void onSaveInstanceState(Bundle outState) { | |
super.onSaveInstanceState(outState); | |
if(mRefresh!=null) { | |
mRefresh.suspendAndSave(outState,REFRESH_TOKEN_KEY); | |
} | |
} | |
private void refreshDocuments() { | |
//... | |
mRefresh = BaasDocument.fetchAll("memos",onRefresh); | |
} | |
private final BaasHandler<List<BaasDocument>> onRefresh = | |
new BaasHandler<List<BaasDocument>>() { | |
@Override | |
public void handle(BaasResult<List<BaasDocument>> req) { | |
mRefresh = null; | |
//... | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment