use of com.github.moko256.twicalico.widget.FragmentPagerAdapter in project twicalico by moko256.
the class MainActivity method onCreate.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
subscription = new CompositeSubscription();
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);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
drawer.addDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
}
@Override
public void onDrawerOpened(@NonNull View drawerView) {
}
@Override
public void onDrawerClosed(@NonNull View drawerView) {
if (isDrawerAccountsSelection) {
changeIsDrawerAccountsSelection();
}
}
@Override
public void onDrawerStateChanged(int newState) {
}
});
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.newInstance(GlobalApplication.userId));
break;
case R.id.nav_settings:
startActivity(new Intent(this, SettingsActivity.class));
break;
}
}
drawer.closeDrawer(GravityCompat.START);
return (id != R.id.nav_settings) && (id != R.id.nav_account);
});
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);
userBackgroundImage = headerView.findViewById(R.id.user_bg_image);
userBackgroundImage.setOnClickListener(v -> changeIsDrawerAccountsSelection());
updateDrawerImage();
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);
SelectAccountsAdapter adapter = new SelectAccountsAdapter(this);
adapter.setOnImageButtonClickListener(accessToken -> {
drawer.closeDrawer(GravityCompat.START);
if (accessToken.getUserId() != GlobalApplication.userId) {
PreferenceManager.getDefaultSharedPreferences(this).edit().putString("AccountKey", accessToken.getKeyString()).apply();
((GlobalApplication) getApplication()).initTwitter(accessToken);
updateDrawerImage();
clearAndPrepareFragment();
}
});
adapter.setOnAddButtonClickListener(v -> {
PreferenceManager.getDefaultSharedPreferences(this).edit().putString("AccountKey", "-1").apply();
GlobalApplication.twitter = null;
startActivity(new Intent(this, OAuthActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK));
});
adapter.setOnRemoveButtonClickListener(v -> new AlertDialog.Builder(this).setMessage(R.string.confirm_logout).setCancelable(true).setPositiveButton(R.string.do_logout, (dialog, i) -> {
SharedPreferences defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
TokenSQLiteOpenHelper helper = new TokenSQLiteOpenHelper(this);
helper.deleteAccessToken(helper.getAccessToken(defaultSharedPreferences.getString("AccountKey", "-1")));
int point = helper.getSize() - 1;
if (point != -1) {
AccessToken accessToken = helper.getAccessTokens()[point];
defaultSharedPreferences.edit().putString("AccountKey", accessToken.getKeyString()).apply();
((GlobalApplication) getApplication()).initTwitter(accessToken);
updateDrawerImage();
clearAndPrepareFragment();
} else {
adapter.getOnAddButtonClickListener().onClick(v);
}
helper.close();
}).setNeutralButton(R.string.back, (dialog, i) -> dialog.cancel()).show());
accountListView.setAdapter(adapter);
subscription.add(Single.create(singleSubscriber -> {
TokenSQLiteOpenHelper helper = new TokenSQLiteOpenHelper(this);
AccessToken[] accessTokens = helper.getAccessTokens();
helper.close();
ArrayList<Pair<User, AccessToken>> r = new ArrayList<>(accessTokens.length);
for (AccessToken accessToken : accessTokens) {
long id = accessToken.getUserId();
CachedUsersSQLiteOpenHelper userHelper = new CachedUsersSQLiteOpenHelper(this, id, accessToken.getType() == Type.TWITTER);
User user = userHelper.getCachedUser(id);
if (user == null) {
try {
user = ((GlobalApplication) getApplication()).getTwitterInstance(accessToken).verifyCredentials();
userHelper.addCachedUser(user);
} catch (TwitterException e) {
singleSubscriber.onError(e);
return;
} finally {
userHelper.close();
}
}
r.add(new Pair<>(user, accessToken));
}
singleSubscriber.onSuccess(r);
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(o -> {
ArrayList<Pair<User, AccessToken>> pairs = (ArrayList<Pair<User, AccessToken>>) o;
adapter.getImagesList().addAll(pairs);
adapter.notifyDataSetChanged();
}, Throwable::printStackTrace));
findViewById(R.id.fab).setOnClickListener(v -> startActivity(new Intent(this, PostActivity.class)));
tabLayout = findViewById(R.id.toolbar_tab);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
Fragment fragment = ((FragmentPagerAdapter) ((UseTabsInterface) getMainFragment()).getTabsViewPager().getAdapter()).getFragment(tab.getPosition());
if (fragment instanceof MovableTopInterface) {
((MovableTopInterface) fragment).moveToTop();
}
}
});
tweetListViewPool = new RecyclerView.RecycledViewPool();
userListViewPool = new RecyclerView.RecycledViewPool();
getSupportFragmentManager().addOnBackStackChangedListener(() -> attachFragment(getMainFragment()));
if (savedInstanceState == null) {
prepareFragment();
}
}
use of com.github.moko256.twicalico.widget.FragmentPagerAdapter in project twicalico by moko256.
the class ShowUserActivity method onCreate.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_user);
subscription = new CompositeSubscription();
setSupportActionBar(findViewById(R.id.toolbar_show_user));
actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.ic_back_white_24dp);
viewPager = findViewById(R.id.show_user_view_pager);
viewPager.setOffscreenPageLimit(1);
tabLayout = findViewById(R.id.tab_show_user);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
Fragment fragment = ((FragmentPagerAdapter) viewPager.getAdapter()).getFragment(tab.getPosition());
if (fragment instanceof MovableTopInterface) {
((MovableTopInterface) fragment).moveToTop();
}
}
});
tweetListViewPool = new RecyclerView.RecycledViewPool();
userListViewPool = new RecyclerView.RecycledViewPool();
findViewById(R.id.activity_show_user_fab).setOnClickListener(v -> {
if (user != null) {
startActivity(PostActivity.getIntent(this, TwitterStringUtils.plusAtMark(user.getScreenName()) + " "));
}
});
userScreenName = getIntent().getStringExtra("userScreenName");
userId = getIntent().getLongExtra("userId", -1);
if (userId != -1) {
user = GlobalApplication.userCache.get(userId);
}
if (user != null) {
new ShowUserFragmentsPagerAdapter(getSupportFragmentManager(), this, user.getId()).initAdapter(viewPager);
} else {
subscription.add(getUserSingle().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(it -> {
user = it;
new ShowUserFragmentsPagerAdapter(getSupportFragmentManager(), this, it.getId()).initAdapter(viewPager);
}, e -> Snackbar.make(findViewById(getSnackBarParentContainerId()), TwitterStringUtils.convertErrorToText(e), Snackbar.LENGTH_INDEFINITE).show()));
}
}
Aggregations