Skip to content

Instantly share code, notes, and snippets.

@chyrta
Last active July 23, 2017 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chyrta/880bb1dedab62e9c8f734b5bc65bf55d to your computer and use it in GitHub Desktop.
Save chyrta/880bb1dedab62e9c8f734b5bc65bf55d to your computer and use it in GitHub Desktop.
TokenDaoImpl
@Singleton
public final class TokenDaoImpl implements TokenDao {
private MyDatabase db;
@Inject public TokenDaoImpl(MyDatabase database) {
this.db = database;
}
@Override public synchronized TokenEntity get() {
Dao<TokenEntity, String> dao = db.getTokenDao();
try {
return dao.queryBuilder().queryForFirst();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override public synchronized void save(TokenEntity token) {
Dao<TokenEntity, String> dao = db.getTokenDao();
truncateTable();
saveToken(token, dao);
}
private void saveToken(TokenEntity config, Dao<TokenEntity, String> dao) {
try {
dao.create(config);
} catch (Exception e) {
e.printStackTrace();
}
}
private void truncateTable() {
try {
TableUtils.clearTable(db.getConnectionSource(), ApiTokenDao.class);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment