public static void loadUsers(LoadCompletedListener loadCompletedListener) { if (!isLoaded) { isLoaded = true; ConnectionSource connectionSource = null; try { DataBaseHelper helper = new DataBaseHelper(Global.getInstance().getApplicationContext()); connectionSource = helper.getConnectionSource(); TableUtils.createTableIfNotExists(connectionSource, ClientUserTable.class); Dao<ClientUserTable, String> dao = helper.getDao(ClientUserTable.class); List<ClientUserTable> tables = dao.queryForAll(); int i = 0; for (ClientUserTable table : tables) { TwitterAuthToken twitterAuthToken = new TwitterAuthToken(table.getAuthToken(), table.getAuthTokenSecret()); TwitterSession session = new TwitterSession(twitterAuthToken, table.getUserId(), table.getUserName()); new ClientUser( i, session, user -> { clientUsers.add(user); confirmationLoadComplete(clientUsers, tables, loadCompletedListener); }, e -> { clientUsers.add(null); MainActivity mainActivity = ((MainActivity) Global.getInstance().getMainActivityContext()); if (mainActivity != null && mainActivity.isDestroyed()) mainActivity.sendToast(e.getMessage()); confirmationLoadComplete(clientUsers, tables, loadCompletedListener); }); i++; } } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } finally { if (connectionSource != null) { try { connectionSource.close(); } catch (SQLException e) { e.printStackTrace(); } } } } else { loadCompletedListener.completed(clientUsers); } }
public void addClientUser(TwitterSession twitterSession, CreateCompletedListener addedListener) { ConnectionSource connectionSource = null; try { DataBaseHelper helper = new DataBaseHelper(Global.getInstance().getApplicationContext()); connectionSource = helper.getConnectionSource(); TableUtils.createTableIfNotExists(connectionSource, ClientUserTable.class); Dao<ClientUserTable, String> dao = helper.getDao(ClientUserTable.class); ClientUserTable table1 = dao.queryForId("" + twitterSession.getUserId()); if (table1 == null) { ClientUserTable table = new ClientUserTable(twitterSession); dao.createOrUpdate(table); new ClientUser( clientUsers.size(), twitterSession, clientUser -> { clientUsers.add(clientUser); addedListener.completed(clientUser); }, TwitterException::printStackTrace); } else { addedListener.completed(null); } } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } finally { if (connectionSource != null) { try { connectionSource.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
public int size() { ConnectionSource connectionSource = null; try { DataBaseHelper helper = new DataBaseHelper(Global.getInstance().getApplicationContext()); connectionSource = helper.getConnectionSource(); TableUtils.createTableIfNotExists(connectionSource, ClientUserTable.class); Dao<ClientUserTable, String> dao = helper.getDao(ClientUserTable.class); List<ClientUserTable> tables = dao.queryForAll(); return tables.size(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } finally { if (connectionSource != null) { try { connectionSource.close(); } catch (SQLException e) { e.printStackTrace(); } } } }