public class PasswordsActivity extends AppCompatActivity implements PasswordsFragment.OnPasswordListFragmentInteractionListener, PasswordFieldsFragment.OnPasswordFragmentInteractionListener { PasswordsContract.Presenter mPasswordsPresenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityUtils.enableScreenshots(this); setContentView(R.layout.activity_scrolling_passwords_list); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mPasswordsPresenter.onAddNewPasswordButtonClick(); } }); //Open passwords of category with that id String categoryId = getIntent().getStringExtra(PasswordsFragment.ARG_EXTRA_CATEGORY_ID); //In tablet mode open details of password with id String passwordId = getIntent().getStringExtra(PasswordFieldsFragment.ARG_EXTRA_PASSWORD_ID); PasswordsMvpController.createPasswordsViews(this, categoryId, passwordId); } @Override protected void onStop() { super.onStop(); if (isFinishing()) { Injector injector = Injector.getInstance(); injector.destroyPasswordsComponent(); if (ActivityUtils.isTablet(this)) { injector.destroyPasswordFieldsComponent(); } } } public void setPasswordsPresenter(PasswordsContract.Presenter passwordsPresenter) { mPasswordsPresenter = passwordsPresenter; } /** * Methods implemented from {@link PasswordsFragment.OnPasswordListFragmentInteractionListener} * * @param parentCategory category that must be displayed */ @Override public void showCategoryInfo(Category parentCategory) { setTitle(parentCategory.getTitle()); CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout); VectorDrawableCompat vectorDrawableCompat = VectorDrawableCompat.create(getResources(), parentCategory.getPicture().getResId(), getTheme()); collapsingToolbarLayout.setContentScrim(vectorDrawableCompat); ImageView categoryImageView = (ImageView) findViewById(R.id.category_picture_imageView); categoryImageView.setImageResource(parentCategory.getPicture().getResId()); }
public class PasswordsPresenter implements PasswordsContract.Presenter { private final DataRepository mDataRepository; private PasswordsContract.View mPasswordsView; // On tablets we can have null categoryId when @Nullable String mCategoryId; Category mCurrentCategory; PasswordsPresenter(DataRepository dataRepository, PasswordsContract.View passwordsView, @CategoryId @Nullable String categoryId) { mDataRepository = dataRepository; mPasswordsView = passwordsView; mCategoryId = categoryId; } public void setupListeners(PasswordsContract.View passwordsView) { mPasswordsView = passwordsView; mPasswordsView.setPresenter(this); } /* * Methods implemented from {@Link PasswordsContract.Presenter} * */ @Override public void start() { if (Strings.isNullOrEmpty(mCategoryId)) { // TODO: 25.02.2017 on tablets we can have null category id when don't pick any category, need to show message about it return; } mDataRepository.getCategory(mCategoryId, new DataSource.LoadCategoryCallback() { @Override public void onCategoryLoaded(Category category) { mCurrentCategory = category; mPasswordsView.showPasswordsInList(category.mPasswords); mPasswordsView.showCategoryInfo(mCurrentCategory); } @Override public void onDataNotAvailable() { //RecycleView show empty data message by itself } }); } @Override public void onPasswordClick(Password password) { mPasswordsView.showDetailPasswordUi(password); } @Override public void onAddNewPasswordButtonClick() { mPasswordsView.showNewPasswordUi(mCategoryId); } @Override public void onSwapPasswords(int firstPosition, int secondPosition) { mDataRepository.swapPasswords(mCurrentCategory, firstPosition, secondPosition); mPasswordsView.swapPasswordInList(firstPosition, secondPosition); } @Override public void loadCategory(Category category) { mCurrentCategory = category; mPasswordsView.showCategoryInfo(category); mPasswordsView.showPasswordsInList(category.mPasswords); } /* * Helper methods that can be called from {@Link CategoriesTabletPresenter} to manipulate view in tablet mode * */ public void onCurrentCategoryDeleted() { mPasswordsView.showEmptyUi(); } }
Source: https://habr.com/ru/post/328708/
All Articles