use of com.github.moko256.latte.client.base.entity.AccessToken in project twicalico by moko256.
the class MainActivity method onCreate.
@Override
public void onCreate(Bundle savedInstanceState) {
setTheme(R.style.MainActivityTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
disposable = new CompositeDisposable();
client = GlobalApplicationKt.getClient(this);
accountsModel = GlobalApplicationKt.getAccountsModel(this);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.getChildAt(0).setOnClickListener(v -> {
Fragment fragment = getMainFragment();
if (fragment instanceof MovableTopInterface) {
((MovableTopInterface) fragment).moveToTop();
}
});
drawer = findViewById(R.id.drawer_layout);
if (drawer != null) {
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
toggle.setDrawerSlideAnimationEnabled(false);
toggle.syncState();
drawer.addDrawerListener(this);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.color_primary_dark));
}
navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(item -> {
int id = item.getItemId();
if (!item.isChecked()) {
switch(id) {
case R.id.nav_timeline:
replaceFragment(new HomeTimeLineFragment());
break;
case R.id.nav_mentions:
replaceFragment(new MentionsFragment());
break;
case R.id.nav_account:
startMyUserActivity();
break;
case R.id.nav_follow_and_follower:
replaceFragment(new MyFollowFollowerFragment());
break;
case R.id.nav_like:
replaceFragment(UserLikeFragment.Companion.newInstance(client.getAccessToken().getUserId()));
break;
case R.id.nav_lists:
replaceFragment(SelectListsEntriesFragment.Companion.newInstance(client.getAccessToken().getUserId()));
break;
case R.id.nav_settings:
startActivity(new Intent(this, SettingsActivity.class));
break;
}
}
if (drawer != null) {
drawer.closeDrawer(GravityCompat.START);
}
return (id != R.id.nav_settings) && (id != R.id.nav_account);
});
View headerView = navigationView.inflateHeaderView(R.layout.nav_header_main);
userNameText = headerView.findViewById(R.id.user_name);
userIdText = headerView.findViewById(R.id.user_id);
userImage = headerView.findViewById(R.id.user_image);
userToggleImage = headerView.findViewById(R.id.toggle_account);
userBackgroundImage = headerView.findViewById(R.id.user_bg_image);
userBackgroundImage.setOnClickListener(v -> changeIsDrawerAccountsSelection());
accountListView = new RecyclerView(this);
accountListView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
accountListView.setLayoutManager(new LinearLayoutManager(this));
accountListView.setOverScrollMode(View.OVER_SCROLL_NEVER);
accountListView.setVisibility(View.GONE);
navigationView.addHeaderView(accountListView);
adapter = new SelectAccountsAdapter(this);
adapter.onImageButtonClickListener = accessToken -> {
if (accessToken.getUserId() != client.getAccessToken().getUserId()) {
changeIsDrawerAccountsSelection();
if (drawer != null) {
drawer.closeDrawer(GravityCompat.START);
}
GlobalApplicationKt.preferenceRepository.putString(KEY_ACCOUNT_KEY, accessToken.getKeyString());
((GlobalApplication) getApplication()).initCurrentClient(accessToken);
client = GlobalApplicationKt.getClient(this);
adapter.updateSelectedPosition(accessToken);
updateDrawerImage();
clearAndPrepareFragment();
}
};
adapter.onAddButtonClickListener = v -> startActivityForResult(new Intent(this, OAuthActivity.class), REQUEST_OAUTH);
adapter.onRemoveButtonClickListener = v -> new AlertDialog.Builder(this).setMessage(R.string.confirm_logout).setCancelable(true).setPositiveButton(R.string.do_logout, (dialog, i) -> {
AccessToken token = accountsModel.get(GlobalApplicationKt.preferenceRepository.getString(KEY_ACCOUNT_KEY, "-1"));
accountsModel.delete(token);
adapter.removeAccessTokensAndUpdate(token);
int point = accountsModel.size() - 1;
if (point != -1) {
AccessToken accessToken = accountsModel.getAccessTokens().get(point);
GlobalApplicationKt.preferenceRepository.putString(KEY_ACCOUNT_KEY, accessToken.getKeyString());
((GlobalApplication) getApplication()).initCurrentClient(accessToken);
client = GlobalApplicationKt.getClient(this);
adapter.updateSelectedPosition(accessToken);
updateDrawerImage();
updateAccountsList();
clearAndPrepareFragment();
} else {
GlobalApplicationKt.preferenceRepository.putString(KEY_ACCOUNT_KEY, "-1");
((GlobalApplication) getApplication()).clearCurrentClient();
startActivityForResult(new Intent(this, OAuthActivity.class), REQUEST_OAUTH);
}
}).setNegativeButton(android.R.string.cancel, null).show();
accountListView.setAdapter(adapter);
findViewById(R.id.fab).setOnClickListener(v -> startActivity(new Intent(this, PostActivity.class)));
tabLayout = findViewById(R.id.toolbar_tab);
tabLayout.addOnTabSelectedListener(this);
alwaysCloseApp = GlobalApplicationKt.preferenceRepository.getBoolean(KEY_ALWAYS_CLOSE_APP, true);
recycledViewPool = new RecyclerView.RecycledViewPool();
recycledViewPool.setMaxRecycledViews(R.layout.layout_post_card, 16);
getSupportFragmentManager().addOnBackStackChangedListener(() -> attachFragment(getMainFragment()));
if (client == null) {
startActivityForResult(new Intent(this, OAuthActivity.class), REQUEST_OAUTH);
} else {
updateDrawerImage();
updateAccountsList();
if (savedInstanceState == null) {
prepareFragment();
}
if (client.getAccessToken().getToken().isEmpty()) {
Toast.makeText(this, R.string.please_re_login, Toast.LENGTH_LONG).show();
}
}
}
use of com.github.moko256.latte.client.base.entity.AccessToken in project twicalico by moko256.
the class MainActivity method updateAccountsList.
private void updateAccountsList() {
disposable.add(Single.create(singleSubscriber -> {
try {
List<AccessToken> accessTokens = accountsModel.getAccessTokens();
ArrayList<User> users = new ArrayList<>(accessTokens.size());
for (AccessToken accessToken : accessTokens) {
long id = accessToken.getUserId();
CachedUsersSQLiteOpenHelper userHelper = new CachedUsersSQLiteOpenHelper(getApplicationContext(), accessToken);
User user = userHelper.getCachedUser(id);
if (user == null) {
try {
user = ((GlobalApplication) getApplication()).createApiClientInstance(accessToken).verifyCredentials();
userHelper.addCachedUser(user);
} catch (Throwable e) {
e.printStackTrace();
} finally {
userHelper.close();
}
}
users.add(user);
}
singleSubscriber.onSuccess(new Pair<>(users, accessTokens));
} catch (Throwable e) {
singleSubscriber.tryOnError(e);
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(o -> {
@SuppressWarnings("unchecked") Pair<ArrayList<User>, ArrayList<AccessToken>> pairs = (Pair<ArrayList<User>, ArrayList<AccessToken>>) o;
adapter.clearImages();
adapter.addAndUpdate(pairs.first, pairs.second);
adapter.setSelectedPosition(client.getAccessToken());
adapter.notifyDataSetChanged();
}, Throwable::printStackTrace));
}
use of com.github.moko256.latte.client.base.entity.AccessToken in project twicalico by moko256.
the class TokenSQLiteOpenHelper method getAccessTokens.
public AccessToken[] getAccessTokens() {
SQLiteDatabase db = getReadableDatabase();
AccessToken[] result = getAccessTokenInternal(db);
db.close();
return result;
}
use of com.github.moko256.latte.client.base.entity.AccessToken in project twicalico by moko256.
the class TokenSQLiteOpenHelper method getAccessToken.
public AccessToken getAccessToken(String key) {
Pair<String, Long> pair = AccessTokenKt.splitAccessTokenKey(key);
AccessToken accessToken;
SQLiteDatabase db = getReadableDatabase();
Cursor c = db.query(TABLE_NAME, TABLE_COLUMNS, "url = '" + pair.getFirst() + "' AND " + "userId = " + pair.getSecond(), null, null, null, null, "1");
if (c.moveToNext()) {
accessToken = convertFromCursor(c);
} else {
accessToken = null;
}
c.close();
db.close();
return accessToken;
}
use of com.github.moko256.latte.client.base.entity.AccessToken in project twicalico by moko256.
the class TokenSQLiteOpenHelper method onUpgrade.
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
if (oldVersion < 2) {
sqLiteDatabase.execSQL("update " + TABLE_NAME + " set token='',tokenSecret='' where type=0");
}
if (oldVersion < 3) {
TableDBUtilKt.addColumn(sqLiteDatabase, TABLE_NAME, "consumerKey", null);
TableDBUtilKt.addColumn(sqLiteDatabase, TABLE_NAME, "consumerSecret", null);
}
if (oldVersion < 4) {
AccessToken[] accessTokens;
try {
accessTokens = getAccessTokenInternal(sqLiteDatabase);
} catch (Throwable throwable) {
accessTokens = new AccessToken[0];
}
sqLiteDatabase.execSQL("drop table if exists " + TABLE_NAME);
sqLiteDatabase.execSQL("drop index if exists idindex");
onCreate(sqLiteDatabase);
for (AccessToken accessToken : accessTokens) {
addAccessTokenInternal(sqLiteDatabase, accessToken);
}
}
}
Aggregations