use of io.scal.secureshare.lib.ChooseAccountFragment in project storymaker by StoryMaker.
the class AccountsActivity method addChooseAccountFragment.
public void addChooseAccountFragment(Bundle bundle) {
FragmentManager fragManager = getSupportFragmentManager();
FragmentTransaction fragTrans = fragManager.beginTransaction();
List<Account> accounts = new ArrayList<>();
final AuthTable authTable = new AuthTable();
String[] siteAvailableNames = Account.CONTROLLER_SITE_NAMES;
String[] siteAvailableKeys = Account.CONTROLLER_SITE_KEYS;
Auth auth;
for (int i = 0; i < siteAvailableKeys.length; i++) {
auth = authTable.getAuthDefault(this, siteAvailableKeys[i]);
if (auth == null) {
accounts.add(new Account(-1, siteAvailableNames[i], siteAvailableKeys[i], "", "", null, false, false));
} else {
accounts.add(auth.convertToAccountObject());
}
}
caFragment = new ChooseAccountFragment();
caFragment.setArguments(bundle);
caFragment.setLoginIntent(new Intent(this, ConnectAccountActivity.class));
// FIXME we should probably make Account object parcelable and pass this through the bundle
caFragment.setAccountsList(accounts);
caFragment.setOnEventListener(new SiteController.OnEventListener() {
@Override
public void onSuccess(Account account) {
Auth auth = authTable.getAuthDefault(getApplicationContext(), account.getSite());
//if auth doesn't exist in db
if (auth == null) {
auth = new Auth(getApplicationContext(), -1, account.getName(), account.getSite(), null, null, null, null, null);
auth.insert();
}
//set id of account based on returned id of auth insert
account.setId(auth.getId());
auth.setCredentials(account.getCredentials());
auth.setData(account.getData());
auth.setUserName(account.getUserName());
auth.setExpires(null);
authTable.updateLastLogin(getApplicationContext(), account.getSite(), auth.getUserName());
auth.update();
}
@Override
public void onFailure(Account account, String failureMessage) {
Auth auth = authTable.getAuthDefault(getApplicationContext(), account.getSite());
if (auth != null) {
//TODO set variables here
auth.setCredentials(account.getCredentials());
auth.setUserName(account.getName());
auth.setData(account.getData());
// FIXME this is a hack to get isValid to fail, probably should be a setFailed() in auth that marks that we are busted
auth.setExpires(new Date());
auth.update();
}
}
@Override
public void onRemove(Account account) {
authTable.delete(getApplicationContext(), account.getId());
}
});
fragTrans.add(R.id.fragmentLayout, caFragment);
fragTrans.commit();
}
Aggregations