mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-11-01 23:33:54 +01:00
Code cleanup and convert CategoriesContract to kotlin
This commit is contained in:
parent
5cbc2ad757
commit
7da4bb6dad
5 changed files with 108 additions and 118 deletions
|
|
@ -1,97 +0,0 @@
|
|||
package fr.free.nrw.commons.upload.categories;
|
||||
|
||||
import android.content.Context;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import fr.free.nrw.commons.BasePresenter;
|
||||
import fr.free.nrw.commons.Media;
|
||||
import fr.free.nrw.commons.category.CategoryItem;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The contract with with UploadCategoriesFragment and its presenter would talk to each other
|
||||
*/
|
||||
public interface CategoriesContract {
|
||||
|
||||
interface View {
|
||||
|
||||
void showProgress(boolean shouldShow);
|
||||
|
||||
void showError(String error);
|
||||
|
||||
void showError(int stringResourceId);
|
||||
|
||||
void setCategories(List<CategoryItem> categories);
|
||||
|
||||
void goToNextScreen();
|
||||
|
||||
void showNoCategorySelected();
|
||||
|
||||
/**
|
||||
* Gets existing category names from media
|
||||
*/
|
||||
List<String> getExistingCategories();
|
||||
|
||||
/**
|
||||
* Returns required context
|
||||
*/
|
||||
Context getFragmentContext();
|
||||
|
||||
/**
|
||||
* Returns to previous fragment
|
||||
*/
|
||||
void goBackToPreviousScreen();
|
||||
|
||||
/**
|
||||
* Shows the progress dialog
|
||||
*/
|
||||
void showProgressDialog();
|
||||
|
||||
/**
|
||||
* Hides the progress dialog
|
||||
*/
|
||||
void dismissProgressDialog();
|
||||
|
||||
/**
|
||||
* Refreshes the categories
|
||||
*/
|
||||
void refreshCategories();
|
||||
|
||||
|
||||
/**
|
||||
* Navigate the user to Login Activity
|
||||
*/
|
||||
void navigateToLoginScreen();
|
||||
}
|
||||
|
||||
interface UserActionListener extends BasePresenter<View> {
|
||||
|
||||
void searchForCategories(String query);
|
||||
|
||||
void verifyCategories();
|
||||
|
||||
void onCategoryItemClicked(CategoryItem categoryItem);
|
||||
|
||||
/**
|
||||
* Attaches view and media
|
||||
*/
|
||||
void onAttachViewWithMedia(@NonNull CategoriesContract.View view, Media media);
|
||||
|
||||
/**
|
||||
* Clears previous selections
|
||||
*/
|
||||
void clearPreviousSelection();
|
||||
|
||||
/**
|
||||
* Update the categories
|
||||
*/
|
||||
void updateCategories(Media media, String wikiText);
|
||||
|
||||
LiveData<List<CategoryItem>> getCategories();
|
||||
|
||||
void selectCategories();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,88 @@
|
|||
package fr.free.nrw.commons.upload.categories
|
||||
|
||||
import android.content.Context
|
||||
import androidx.lifecycle.LiveData
|
||||
import fr.free.nrw.commons.BasePresenter
|
||||
import fr.free.nrw.commons.Media
|
||||
import fr.free.nrw.commons.category.CategoryItem
|
||||
|
||||
/**
|
||||
* The contract with with UploadCategoriesFragment and its presenter would talk to each other
|
||||
*/
|
||||
interface CategoriesContract {
|
||||
interface View {
|
||||
fun showProgress(shouldShow: Boolean)
|
||||
|
||||
fun showError(error: String?)
|
||||
|
||||
fun showError(stringResourceId: Int)
|
||||
|
||||
fun setCategories(categories: List<CategoryItem>?)
|
||||
|
||||
fun goToNextScreen()
|
||||
|
||||
fun showNoCategorySelected()
|
||||
|
||||
/**
|
||||
* Gets existing category names from media
|
||||
*/
|
||||
fun getExistingCategories(): List<String>?
|
||||
|
||||
/**
|
||||
* Returns required context
|
||||
*/
|
||||
fun getFragmentContext(): Context
|
||||
|
||||
/**
|
||||
* Returns to previous fragment
|
||||
*/
|
||||
fun goBackToPreviousScreen()
|
||||
|
||||
/**
|
||||
* Shows the progress dialog
|
||||
*/
|
||||
fun showProgressDialog()
|
||||
|
||||
/**
|
||||
* Hides the progress dialog
|
||||
*/
|
||||
fun dismissProgressDialog()
|
||||
|
||||
/**
|
||||
* Refreshes the categories
|
||||
*/
|
||||
fun refreshCategories()
|
||||
|
||||
/**
|
||||
* Navigate the user to Login Activity
|
||||
*/
|
||||
fun navigateToLoginScreen()
|
||||
}
|
||||
|
||||
interface UserActionListener : BasePresenter<View> {
|
||||
fun searchForCategories(query: String)
|
||||
|
||||
fun verifyCategories()
|
||||
|
||||
fun onCategoryItemClicked(categoryItem: CategoryItem)
|
||||
|
||||
/**
|
||||
* Attaches view and media
|
||||
*/
|
||||
fun onAttachViewWithMedia(view: View, media: Media)
|
||||
|
||||
/**
|
||||
* Clears previous selections
|
||||
*/
|
||||
fun clearPreviousSelection()
|
||||
|
||||
/**
|
||||
* Update the categories
|
||||
*/
|
||||
fun updateCategories(media: Media, wikiText: String)
|
||||
|
||||
fun getCategories(): LiveData<List<CategoryItem>>
|
||||
|
||||
fun selectCategories()
|
||||
}
|
||||
}
|
||||
|
|
@ -8,7 +8,6 @@ import fr.free.nrw.commons.R
|
|||
import fr.free.nrw.commons.auth.csrf.InvalidLoginTokenException
|
||||
import fr.free.nrw.commons.category.CategoryEditHelper
|
||||
import fr.free.nrw.commons.category.CategoryItem
|
||||
import fr.free.nrw.commons.di.CommonsApplicationModule
|
||||
import fr.free.nrw.commons.di.CommonsApplicationModule.Companion.IO_THREAD
|
||||
import fr.free.nrw.commons.di.CommonsApplicationModule.Companion.MAIN_THREAD
|
||||
import fr.free.nrw.commons.repository.UploadRepository
|
||||
|
|
@ -175,7 +174,7 @@ class CategoriesPresenter
|
|||
) {
|
||||
this.view = view
|
||||
this.media = media
|
||||
repository.setSelectedExistingCategories(view.existingCategories)
|
||||
repository.setSelectedExistingCategories(view.getExistingCategories() ?: emptyList())
|
||||
compositeDisposable.add(
|
||||
searchTerms
|
||||
.observeOn(mainThreadScheduler)
|
||||
|
|
@ -224,11 +223,11 @@ class CategoriesPresenter
|
|||
repository.getSelectedCategories().isNotEmpty()
|
||||
||
|
||||
(
|
||||
view.existingCategories != null
|
||||
view.getExistingCategories() != null
|
||||
&&
|
||||
repository.getSelectedExistingCategories().size
|
||||
!=
|
||||
view.existingCategories.size
|
||||
view.getExistingCategories()?.size
|
||||
)
|
||||
) {
|
||||
val selectedCategories: MutableList<String> =
|
||||
|
|
@ -244,7 +243,7 @@ class CategoriesPresenter
|
|||
compositeDisposable.add(
|
||||
categoryEditHelper
|
||||
.makeCategoryEdit(
|
||||
view.fragmentContext,
|
||||
view.getFragmentContext(),
|
||||
media,
|
||||
selectedCategories,
|
||||
wikiText,
|
||||
|
|
|
|||
|
|
@ -65,14 +65,14 @@ public class UploadCategoriesFragment extends UploadBaseFragment implements Cate
|
|||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
|
||||
@Nullable Bundle savedInstanceState) {
|
||||
public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable final ViewGroup container,
|
||||
@Nullable final Bundle savedInstanceState) {
|
||||
binding = UploadCategoriesFragmentBinding.inflate(inflater, container, false);
|
||||
return binding.getRoot();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||
public void onViewCreated(@NonNull final View view, @Nullable final Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
final Bundle bundle = getArguments();
|
||||
if (bundle != null) {
|
||||
|
|
@ -104,8 +104,8 @@ public class UploadCategoriesFragment extends UploadBaseFragment implements Cate
|
|||
setTvSubTitle();
|
||||
binding.tooltip.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
DialogUtil.showAlertDialog(getActivity(), getString(R.string.categories_activity_title), getString(R.string.categories_tooltip), getString(android.R.string.ok), null);
|
||||
public void onClick(final View v) {
|
||||
DialogUtil.showAlertDialog(requireActivity(), getString(R.string.categories_activity_title), getString(R.string.categories_tooltip), getString(android.R.string.ok), null, true);
|
||||
}
|
||||
});
|
||||
if (media == null) {
|
||||
|
|
@ -146,7 +146,7 @@ public class UploadCategoriesFragment extends UploadBaseFragment implements Cate
|
|||
}
|
||||
}
|
||||
|
||||
private void searchForCategory(String query) {
|
||||
private void searchForCategory(final String query) {
|
||||
presenter.searchForCategories(query);
|
||||
}
|
||||
|
||||
|
|
@ -170,28 +170,28 @@ public class UploadCategoriesFragment extends UploadBaseFragment implements Cate
|
|||
}
|
||||
|
||||
@Override
|
||||
public void showProgress(boolean shouldShow) {
|
||||
public void showProgress(final boolean shouldShow) {
|
||||
if (binding != null) {
|
||||
binding.pbCategories.setVisibility(shouldShow ? View.VISIBLE : View.GONE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showError(String error) {
|
||||
public void showError(final String error) {
|
||||
if (binding != null) {
|
||||
binding.tilContainerSearch.setError(error);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showError(int stringResourceId) {
|
||||
public void showError(final int stringResourceId) {
|
||||
if (binding != null) {
|
||||
binding.tilContainerSearch.setError(getString(stringResourceId));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCategories(List<CategoryItem> categories) {
|
||||
public void setCategories(final List<CategoryItem> categories) {
|
||||
if (categories == null) {
|
||||
adapter.clear();
|
||||
} else {
|
||||
|
|
@ -229,12 +229,12 @@ public class UploadCategoriesFragment extends UploadBaseFragment implements Cate
|
|||
@Override
|
||||
public void showNoCategorySelected() {
|
||||
if (media == null) {
|
||||
DialogUtil.showAlertDialog(getActivity(),
|
||||
DialogUtil.showAlertDialog(requireActivity(),
|
||||
getString(R.string.no_categories_selected),
|
||||
getString(R.string.no_categories_selected_warning_desc),
|
||||
getString(R.string.continue_message),
|
||||
getString(R.string.cancel),
|
||||
() -> goToNextScreen(),
|
||||
this::goToNextScreen,
|
||||
null);
|
||||
} else {
|
||||
Toast.makeText(requireContext(), getString(R.string.no_categories_selected),
|
||||
|
|
@ -256,6 +256,7 @@ public class UploadCategoriesFragment extends UploadBaseFragment implements Cate
|
|||
/**
|
||||
* Returns required context
|
||||
*/
|
||||
@NonNull
|
||||
@Override
|
||||
public Context getFragmentContext() {
|
||||
return requireContext();
|
||||
|
|
@ -306,7 +307,7 @@ public class UploadCategoriesFragment extends UploadBaseFragment implements Cate
|
|||
public void navigateToLoginScreen() {
|
||||
final String username = sessionManager.getUserName();
|
||||
final CommonsApplication.BaseLogoutListener logoutListener = new CommonsApplication.BaseLogoutListener(
|
||||
getActivity(),
|
||||
requireActivity(),
|
||||
requireActivity().getString(R.string.invalid_login_message),
|
||||
username
|
||||
);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ import android.view.LayoutInflater
|
|||
import androidx.fragment.app.FragmentManager
|
||||
import androidx.fragment.app.FragmentTransaction
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import com.nhaarman.mockitokotlin2.times
|
||||
import fr.free.nrw.commons.Media
|
||||
import fr.free.nrw.commons.OkHttpConnectionFactory
|
||||
import fr.free.nrw.commons.R
|
||||
|
|
@ -184,14 +183,14 @@ class UploadCategoriesFragmentUnitTests {
|
|||
@Throws(Exception::class)
|
||||
fun testGetExistingCategories() {
|
||||
Shadows.shadowOf(Looper.getMainLooper()).idle()
|
||||
fragment.existingCategories
|
||||
fragment.getExistingCategories()
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testGetFragmentContext() {
|
||||
Shadows.shadowOf(Looper.getMainLooper()).idle()
|
||||
fragment.fragmentContext
|
||||
fragment.getFragmentContext()
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue