Skip to content

Commit

Permalink
Convert UploadMediaPresenter to kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
psh committed Jan 11, 2025
1 parent e4b4ceb commit 69f8044
Show file tree
Hide file tree
Showing 9 changed files with 530 additions and 613 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class UploadRepository @Inject constructor(
*
* @return
*/
fun buildContributions(): Observable<Contribution>? {
fun buildContributions(): Observable<Contribution> {
return uploadModel.buildContributions()
}

Expand Down Expand Up @@ -177,7 +177,7 @@ class UploadRepository @Inject constructor(
place: Place?,
similarImageInterface: SimilarImageInterface?,
inAppPictureLocation: LatLng?
): Observable<UploadItem>? {
): Observable<UploadItem> {
return uploadModel.preProcessImage(
uploadableFile,
place,
Expand All @@ -193,7 +193,7 @@ class UploadRepository @Inject constructor(
* @param location Location of the image
* @return Quality of UploadItem
*/
fun getImageQuality(uploadItem: UploadItem, location: LatLng?): Single<Int>? {
fun getImageQuality(uploadItem: UploadItem, location: LatLng?): Single<Int> {
return uploadModel.getImageQuality(uploadItem, location)
}

Expand All @@ -213,7 +213,7 @@ class UploadRepository @Inject constructor(
* @param uploadItem UploadItem whose caption is to be checked
* @return Quality of caption of the UploadItem
*/
fun getCaptionQuality(uploadItem: UploadItem): Single<Int>? {
fun getCaptionQuality(uploadItem: UploadItem): Single<Int> {
return uploadModel.getCaptionQuality(uploadItem)
}

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/fr/free/nrw/commons/upload/UploadModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,11 @@ class UploadModel @Inject internal constructor(
Timber.d(
"Created timestamp while building contribution is %s, %s",
item.createdTimestamp,
Date(item.createdTimestamp!!)
item.createdTimestamp?.let { Date(it) }
)

if (item.createdTimestamp != -1L) {
contribution.dateCreated = Date(item.createdTimestamp)
contribution.dateCreated = item.createdTimestamp?.let { Date(it) }
contribution.dateCreatedSource = item.createdTimestampSource
//Set the date only if you have it, else the upload service is gonna try it the other way
}
Expand Down
14 changes: 7 additions & 7 deletions app/src/main/java/fr/free/nrw/commons/upload/UploadPresenter.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package fr.free.nrw.commons.upload

import android.annotation.SuppressLint
import fr.free.nrw.commons.CommonsApplication
import fr.free.nrw.commons.CommonsApplication.Companion.IS_LIMITED_CONNECTION_MODE_ENABLED
import fr.free.nrw.commons.R
import fr.free.nrw.commons.contributions.Contribution
Expand Down Expand Up @@ -69,8 +68,7 @@ class UploadPresenter @Inject internal constructor(
private fun processContributionsForSubmission() {
if (view.isLoggedIn()) {
view.showProgress(true)
repository.buildContributions()
?.observeOn(Schedulers.io())
repository.buildContributions().observeOn(Schedulers.io())
?.subscribe(object : Observer<Contribution> {
override fun onSubscribe(d: Disposable) {
view.showProgress(false)
Expand Down Expand Up @@ -133,8 +131,9 @@ class UploadPresenter @Inject internal constructor(
* @param uploadItemIndex Index of next image, whose quality is to be checked
*/
override fun checkImageQuality(uploadItemIndex: Int) {
val uploadItem = repository.getUploadItem(uploadItemIndex)
presenter.checkImageQuality(uploadItem, uploadItemIndex)
repository.getUploadItem(uploadItemIndex)?.let {
presenter.checkImageQuality(it, uploadItemIndex)
}
}

override fun deletePictureAtIndex(index: Int) {
Expand All @@ -156,8 +155,9 @@ class UploadPresenter @Inject internal constructor(
view.onUploadMediaDeleted(index)
if (index != uploadableFiles.size && index != 0) {
// if the deleted image was not the last item to be uploaded, check quality of next
val uploadItem = repository.getUploadItem(index)
presenter.checkImageQuality(uploadItem, index)
repository.getUploadItem(index)?.let {
presenter.checkImageQuality(it, index)
}
}

if (uploadableFiles.size < 2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import java.util.Objects;
import javax.inject.Inject;
import javax.inject.Named;
import org.jetbrains.annotations.NotNull;
import timber.log.Timber;

public class UploadMediaDetailFragment extends UploadBaseFragment implements
Expand Down Expand Up @@ -154,7 +155,7 @@ public class UploadMediaDetailFragment extends UploadBaseFragment implements

public void setCallback(UploadMediaDetailFragmentCallback callback) {
this.callback = callback;
UploadMediaPresenter.presenterCallback = callback;
UploadMediaPresenter.Companion.setPresenterCallback(callback);
}

@Override
Expand Down Expand Up @@ -190,12 +191,12 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

activity = getActivity();
activity = requireActivity();
basicKvStore = new BasicKvStore(activity, "CurrentUploadImageQualities");

if (callback != null) {
indexOfFragment = callback.getIndexInViewFlipper(this);
init();
initializeFragment();
}

if(savedInstanceState!=null){
Expand All @@ -207,7 +208,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
}

try {
if(!presenter.getImageQuality(indexOfFragment, inAppPictureLocation, getActivity())) {
if(!presenter.getImageQuality(indexOfFragment, inAppPictureLocation, requireActivity())) {
ActivityUtils.startActivityWithFlags(
getActivity(), MainActivity.class, Intent.FLAG_ACTIVITY_CLEAR_TOP,
Intent.FLAG_ACTIVITY_SINGLE_TOP);
Expand All @@ -217,7 +218,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat

}

private void init() {
private void initializeFragment() {
if (binding == null) {
return;
}
Expand Down Expand Up @@ -373,7 +374,7 @@ public void onNegativeResponse() {
}

@Override
public void onImageProcessed(UploadItem uploadItem, Place place) {
public void onImageProcessed(@NotNull UploadItem uploadItem) {
if (binding == null) {
return;
}
Expand All @@ -386,7 +387,8 @@ public void onImageProcessed(UploadItem uploadItem, Place place) {
* @param place
*/
@Override
public void onNearbyPlaceFound(UploadItem uploadItem, Place place) {
public void onNearbyPlaceFound(
@NotNull UploadItem uploadItem, @org.jetbrains.annotations.Nullable Place place) {
nearbyPlace = place;
this.uploadItem = uploadItem;
showNearbyFound = true;
Expand Down Expand Up @@ -506,7 +508,7 @@ public void showMessage(String message, int colorResourceId) {
}

@Override
public void showDuplicatePicturePopup(UploadItem uploadItem) {
public void showDuplicatePicturePopup(@NotNull UploadItem uploadItem) {
if (defaultKvStore.getBoolean("showDuplicatePicturePopup", true)) {
String uploadTitleFormat = getString(R.string.upload_title_duplicate);
View checkBoxView = View
Expand All @@ -517,7 +519,7 @@ public void showDuplicatePicturePopup(UploadItem uploadItem) {
defaultKvStore.putBoolean("showDuplicatePicturePopup", false);
}
});
DialogUtil.showAlertDialog(getActivity(),
DialogUtil.showAlertDialog(requireActivity(),
getString(R.string.duplicate_file_name),
String.format(Locale.getDefault(),
uploadTitleFormat,
Expand Down Expand Up @@ -597,7 +599,7 @@ public void showConnectionErrorPopup() {
}

@Override
public void showExternalMap(final UploadItem uploadItem) {
public void showExternalMap(@NotNull final UploadItem uploadItem) {
goToLocationPickerActivity(uploadItem);
}

Expand All @@ -612,7 +614,7 @@ public void showExternalMap(final UploadItem uploadItem) {
* is started using resultLauncher that handles the result in respective callback.
*/
@Override
public void showEditActivity(UploadItem uploadItem) {
public void showEditActivity(@NotNull UploadItem uploadItem) {
editableUploadItem = uploadItem;
Intent intent = new Intent(getContext(), EditActivity.class);
intent.putExtra("image", uploadableFile.getFilePath().toString());
Expand Down Expand Up @@ -789,7 +791,7 @@ public void editLocation(final String latitude, final String longitude, final do
}

@Override
public void updateMediaDetails(List<UploadMediaDetail> uploadMediaDetails) {
public void updateMediaDetails(@NotNull List<UploadMediaDetail> uploadMediaDetails) {
uploadMediaDetailAdapter.setItems(uploadMediaDetails);
showNearbyFound =
showNearbyFound && (
Expand Down Expand Up @@ -823,7 +825,7 @@ private boolean listContainsEmptyDetails(List<UploadMediaDetail> uploadMediaDeta
* @param onSkipClicked proceed for verifying image quality
*/
@Override
public void displayAddLocationDialog(final Runnable onSkipClicked) {
public void displayAddLocationDialog(@NotNull final Runnable onSkipClicked) {
isMissingLocationDialog = true;
DialogUtil.showAlertDialog(requireActivity(),
getString(R.string.no_location_found_title),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import fr.free.nrw.commons.upload.UploadMediaDetail
*/
interface UploadMediaDetailsContract {
interface View : SimilarImageInterface {
fun onImageProcessed(uploadItem: UploadItem?, place: Place?)
fun onImageProcessed(uploadItem: UploadItem)

fun onNearbyPlaceFound(uploadItem: UploadItem?, place: Place?)
fun onNearbyPlaceFound(uploadItem: UploadItem, place: Place?)

fun showProgress(shouldShow: Boolean)

Expand All @@ -27,7 +27,7 @@ interface UploadMediaDetailsContract {

fun showMessage(message: String?, colorResourceId: Int)

fun showDuplicatePicturePopup(uploadItem: UploadItem?)
fun showDuplicatePicturePopup(uploadItem: UploadItem)

/**
* Shows a dialog alerting the user that internet connection is required for upload process
Expand All @@ -42,13 +42,13 @@ interface UploadMediaDetailsContract {
*/
fun showConnectionErrorPopupForCaptionCheck()

fun showExternalMap(uploadItem: UploadItem?)
fun showExternalMap(uploadItem: UploadItem)

fun showEditActivity(uploadItem: UploadItem?)
fun showEditActivity(uploadItem: UploadItem)

fun updateMediaDetails(uploadMediaDetails: List<UploadMediaDetail?>?)
fun updateMediaDetails(uploadMediaDetails: List<UploadMediaDetail>)

fun displayAddLocationDialog(runnable: Runnable?)
fun displayAddLocationDialog(runnable: Runnable)
}

interface UserActionListener : BasePresenter<View?> {
Expand All @@ -59,7 +59,7 @@ interface UploadMediaDetailsContract {
)

fun setUploadMediaDetails(
uploadMediaDetails: List<UploadMediaDetail?>?,
uploadMediaDetails: List<UploadMediaDetail>,
uploadItemIndex: Int
)

Expand All @@ -74,7 +74,7 @@ interface UploadMediaDetailsContract {
fun getImageQuality(
uploadItemIndex: Int,
inAppPictureLocation: LatLng?,
activity: Activity?
activity: Activity
): Boolean

/**
Expand All @@ -87,7 +87,8 @@ interface UploadMediaDetailsContract {
* @param hasUserRemovedLocation True if user has removed location from the image
*/
fun displayLocDialog(
uploadItemIndex: Int, inAppPictureLocation: LatLng?,
uploadItemIndex: Int,
inAppPictureLocation: LatLng?,
hasUserRemovedLocation: Boolean
)

Expand All @@ -97,7 +98,7 @@ interface UploadMediaDetailsContract {
* @param uploadItem UploadItem whose quality is to be checked
* @param index Index of the UploadItem whose quality is to be checked
*/
fun checkImageQuality(uploadItem: UploadItem?, index: Int)
fun checkImageQuality(uploadItem: UploadItem, index: Int)

/**
* Updates the image qualities stored in JSON, whenever an image is deleted
Expand All @@ -111,7 +112,7 @@ interface UploadMediaDetailsContract {

fun fetchTitleAndDescription(indexInViewFlipper: Int)

fun useSimilarPictureCoordinates(imageCoordinates: ImageCoordinates?, uploadItemIndex: Int)
fun useSimilarPictureCoordinates(imageCoordinates: ImageCoordinates, uploadItemIndex: Int)

fun onMapIconClicked(indexInViewFlipper: Int)

Expand Down
Loading

0 comments on commit 69f8044

Please sign in to comment.