Code cleanup, fixed nullability and converted DepictsContract to kotlin

This commit is contained in:
Paul Hawke 2024-12-09 13:32:44 -06:00
parent 484e56c17c
commit 1ba5b7e6c7
7 changed files with 40 additions and 45 deletions

View file

@ -1,129 +1,125 @@
package fr.free.nrw.commons.upload.depicts; package fr.free.nrw.commons.upload.depicts
import android.content.Context; import android.content.Context
import androidx.annotation.NonNull; import androidx.lifecycle.LiveData
import androidx.lifecycle.LiveData; import fr.free.nrw.commons.BasePresenter
import fr.free.nrw.commons.BasePresenter; import fr.free.nrw.commons.Media
import fr.free.nrw.commons.Media; import fr.free.nrw.commons.upload.structure.depictions.DepictedItem
import fr.free.nrw.commons.upload.structure.depictions.DepictedItem;
import java.util.List;
/** /**
* The contract with which DepictsFragment and its presenter would talk to each other * The contract with which DepictsFragment and its presenter would talk to each other
*/ */
public interface DepictsContract { interface DepictsContract {
interface View { interface View {
/** /**
* Go to category screen * Go to category screen
*/ */
void goToNextScreen(); fun goToNextScreen()
/** /**
* Go to media detail screen * Go to media detail screen
*/ */
void goToPreviousScreen(); fun goToPreviousScreen()
/** /**
* show error in case of no depiction selected * show error in case of no depiction selected
*/ */
void noDepictionSelected(); fun noDepictionSelected()
/** /**
* Show progress/Hide progress depending on the boolean value * Show progress/Hide progress depending on the boolean value
*/ */
void showProgress(boolean shouldShow); fun showProgress(shouldShow: Boolean)
/** /**
* decides whether to show error values or not depending on the boolean value * decides whether to show error values or not depending on the boolean value
*/ */
void showError(Boolean value); fun showError(value: Boolean)
/** /**
* add depictions to list * add depictions to list
*/ */
void setDepictsList(List<DepictedItem> depictedItemList); fun setDepictsList(depictedItemList: List<DepictedItem>)
/** /**
* Returns required context * Returns required context
*/ */
Context getFragmentContext(); fun getFragmentContext(): Context
/** /**
* Returns to previous fragment * Returns to previous fragment
*/ */
void goBackToPreviousScreen(); fun goBackToPreviousScreen()
/** /**
* Gets existing depictions IDs from media * Gets existing depictions IDs from media
*/ */
List<String> getExistingDepictions(); fun getExistingDepictions(): List<String>?
/** /**
* Shows the progress dialog * Shows the progress dialog
*/ */
void showProgressDialog(); fun showProgressDialog()
/** /**
* Hides the progress dialog * Hides the progress dialog
*/ */
void dismissProgressDialog(); fun dismissProgressDialog()
/** /**
* Update the depictions * Update the depictions
*/ */
void updateDepicts(); fun updateDepicts()
/** /**
* Navigate the user to Login Activity * Navigate the user to Login Activity
*/ */
void navigateToLoginScreen(); fun navigateToLoginScreen()
} }
interface UserActionListener extends BasePresenter<View> { interface UserActionListener : BasePresenter<View> {
/** /**
* Takes to previous screen * Takes to previous screen
*/ */
void onPreviousButtonClicked(); fun onPreviousButtonClicked()
/** /**
* Listener for the depicted items selected from the list * Listener for the depicted items selected from the list
*/ */
void onDepictItemClicked(DepictedItem depictedItem); fun onDepictItemClicked(depictedItem: DepictedItem)
/** /**
* asks the repository to fetch depictions for the query * asks the repository to fetch depictions for the query
* @param query * @param query
*/ */
void searchForDepictions(String query); fun searchForDepictions(query: String)
/** /**
* Selects all associated places (if any) as depictions * Selects all associated places (if any) as depictions
*/ */
void selectPlaceDepictions(); fun selectPlaceDepictions()
/** /**
* Check if depictions were selected * Check if depictions were selected
* from the depiction list * from the depiction list
*/ */
void verifyDepictions(); fun verifyDepictions()
/** /**
* Clears previous selections * Clears previous selections
*/ */
void clearPreviousSelection(); fun clearPreviousSelection()
LiveData<List<DepictedItem>> getDepictedItems(); fun getDepictedItems(): LiveData<List<DepictedItem>>
/** /**
* Update the depictions * Update the depictions
*/ */
void updateDepictions(Media media); fun updateDepictions(media: Media)
/** /**
* Attaches view and media * Attaches view and media
*/ */
void onAttachViewWithMedia(@NonNull View view, Media media); fun onAttachViewWithMedia(view: View, media: Media)
} }
} }

View file

@ -218,7 +218,7 @@ public class DepictsFragment extends UploadBaseFragment implements DepictsContra
} }
@Override @Override
public void showError(Boolean value) { public void showError(boolean value) {
if (binding == null) { if (binding == null) {
return; return;
} }

View file

@ -6,7 +6,6 @@ import androidx.lifecycle.MutableLiveData
import fr.free.nrw.commons.Media import fr.free.nrw.commons.Media
import fr.free.nrw.commons.auth.csrf.InvalidLoginTokenException import fr.free.nrw.commons.auth.csrf.InvalidLoginTokenException
import fr.free.nrw.commons.bookmarks.items.BookmarkItemsController import fr.free.nrw.commons.bookmarks.items.BookmarkItemsController
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.IO_THREAD
import fr.free.nrw.commons.di.CommonsApplicationModule.Companion.MAIN_THREAD import fr.free.nrw.commons.di.CommonsApplicationModule.Companion.MAIN_THREAD
import fr.free.nrw.commons.repository.UploadRepository import fr.free.nrw.commons.repository.UploadRepository
@ -208,7 +207,7 @@ class DepictsPresenter
@SuppressLint("CheckResult") @SuppressLint("CheckResult")
override fun updateDepictions(media: Media) { override fun updateDepictions(media: Media) {
if (repository.getSelectedDepictions().isNotEmpty() || if (repository.getSelectedDepictions().isNotEmpty() ||
repository.getSelectedExistingDepictions().size != view.existingDepictions.size repository.getSelectedExistingDepictions().size != view.getExistingDepictions()?.size
) { ) {
view.showProgressDialog() view.showProgressDialog()
val selectedDepictions: MutableList<String> = val selectedDepictions: MutableList<String> =
@ -225,7 +224,7 @@ class DepictsPresenter
compositeDisposable.add( compositeDisposable.add(
depictsHelper depictsHelper
.makeDepictionEdit(view.fragmentContext, media, selectedDepictions) .makeDepictionEdit(view.getFragmentContext(), media, selectedDepictions)
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe({ .subscribe({
@ -256,7 +255,7 @@ class DepictsPresenter
) { ) {
this.view = view this.view = view
this.media = media this.media = media
repository.setSelectedExistingDepictions(view.existingDepictions) repository.setSelectedExistingDepictions(view.getExistingDepictions() ?: emptyList())
compositeDisposable.add( compositeDisposable.add(
searchTerm searchTerm
.observeOn(mainThreadScheduler) .observeOn(mainThreadScheduler)

View file

@ -9,9 +9,9 @@ interface MediaLicenseContract {
interface View { interface View {
fun setLicenses(licenses: List<String>?) fun setLicenses(licenses: List<String>?)
fun setSelectedLicense(license: String) fun setSelectedLicense(license: String?)
fun updateLicenseSummary(selectedLicense: String?, numberOfItems: Int) fun updateLicenseSummary(selectedLicense: String?, numberOfItems: Int?)
} }
interface UserActionListener : BasePresenter<View> { interface UserActionListener : BasePresenter<View> {

View file

@ -141,7 +141,7 @@ public class MediaLicenseFragment extends UploadBaseFragment implements MediaLic
} }
@Override @Override
public void updateLicenseSummary(String licenseSummary, int numberOfItems) { public void updateLicenseSummary(String licenseSummary, Integer numberOfItems) {
String licenseHyperLink = "<a href='" + Utils.licenseUrlFor(licenseSummary) + "'>" + String licenseHyperLink = "<a href='" + Utils.licenseUrlFor(licenseSummary) + "'>" +
getString(Utils.licenseNameFor(licenseSummary)) + "</a><br>"; getString(Utils.licenseNameFor(licenseSummary)) + "</a><br>";

View file

@ -71,7 +71,7 @@ public class MediaLicensePresenter implements MediaLicenseContract.UserActionLis
* @param licenseName * @param licenseName
*/ */
@Override @Override
public void selectLicense(@NonNull final String licenseName) { public void selectLicense(final String licenseName) {
repository.setSelectedLicense(licenseName); repository.setSelectedLicense(licenseName);
view.updateLicenseSummary(repository.getSelectedLicense(), repository.getCount()); view.updateLicenseSummary(repository.getSelectedLicense(), repository.getCount());
} }

View file

@ -258,7 +258,7 @@ class DepictsFragmentUnitTests {
@Test @Test
@Throws(Exception::class) @Throws(Exception::class)
fun testGetFragmentContext() { fun testGetFragmentContext() {
fragment.fragmentContext fragment.getFragmentContext()
} }
@Test @Test