Migrated ContributionsContract,ContributionFragment,ContributionListAdapter,ContributionsListContract from java to Kotlin

This commit is contained in:
Sujal-Gupta-SG 2025-01-31 22:00:39 +05:30
parent 177394b8fd
commit 66bb4509ca
10 changed files with 723 additions and 659 deletions

View file

@ -1,23 +1,19 @@
package fr.free.nrw.commons.contributions;
package fr.free.nrw.commons.contributions
import android.content.Context;
import fr.free.nrw.commons.BasePresenter;
import android.content.Context
import fr.free.nrw.commons.BasePresenter
/**
* The contract for Contributions View & Presenter
*/
public class ContributionsContract {
interface ContributionsContract {
public interface View {
void showMessage(String localizedMessage);
Context getContext();
interface View {
fun showMessage(localizedMessage: String)
fun getContext(): Context
}
public interface UserActionListener extends BasePresenter<ContributionsContract.View> {
Contribution getContributionsWithTitle(String uri);
interface UserActionListener : BasePresenter<View> {
fun getContributionsWithTitle(uri: String): Contribution
}
}

View file

@ -1,77 +1,72 @@
package fr.free.nrw.commons.contributions;
package fr.free.nrw.commons.contributions
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.paging.PagedListAdapter;
import androidx.recyclerview.widget.DiffUtil;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.media.MediaClient;
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.DiffUtil
import fr.free.nrw.commons.R
import fr.free.nrw.commons.media.MediaClient
/**
/**
* Represents The View Adapter for the List of Contributions
*/
public class ContributionsListAdapter extends
PagedListAdapter<Contribution, ContributionViewHolder> {
private final Callback callback;
private final MediaClient mediaClient;
ContributionsListAdapter(final Callback callback,
final MediaClient mediaClient) {
super(DIFF_CALLBACK);
this.callback = callback;
this.mediaClient = mediaClient;
}
/**
* Uses DiffUtil to calculate the changes in the list
* It has methods that check ID and the content of the items to determine if its a new item
*/
private static final DiffUtil.ItemCallback<Contribution> DIFF_CALLBACK =
new DiffUtil.ItemCallback<Contribution>() {
@Override
public boolean areItemsTheSame(final Contribution oldContribution, final Contribution newContribution) {
return oldContribution.getPageId().equals(newContribution.getPageId());
}
@Override
public boolean areContentsTheSame(final Contribution oldContribution, final Contribution newContribution) {
return oldContribution.equals(newContribution);
}
};
class ContributionsListAdapter internal constructor(
private val callback: Callback,
private val mediaClient: MediaClient
) : PagedListAdapter<Contribution, ContributionViewHolder>(DIFF_CALLBACK) {
/**
* Initializes the view holder with contribution data
*/
@Override
public void onBindViewHolder(@NonNull ContributionViewHolder holder, int position) {
holder.init(position, getItem(position));
override fun onBindViewHolder(holder: ContributionViewHolder, position: Int) {
holder.init(position, getItem(position))
}
Contribution getContributionForPosition(final int position) {
return getItem(position);
fun getContributionForPosition(position: Int): Contribution? {
return getItem(position)
}
/**
* Creates the new View Holder which will be used to display items(contributions) using the
* onBindViewHolder(viewHolder,position)
*/
@NonNull
@Override
public ContributionViewHolder onCreateViewHolder(@NonNull final ViewGroup parent,
final int viewType) {
final ContributionViewHolder viewHolder = new ContributionViewHolder(
LayoutInflater.from(parent.getContext())
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): ContributionViewHolder {
val viewHolder = ContributionViewHolder(
LayoutInflater.from(parent.context)
.inflate(R.layout.layout_contribution, parent, false),
callback, mediaClient);
return viewHolder;
callback, mediaClient
)
return viewHolder
}
public interface Callback {
interface Callback {
fun openMediaDetail(contribution: Int, isWikipediaPageExists: Boolean)
void openMediaDetail(int contribution, boolean isWikipediaPageExists);
fun addImageToWikipedia(contribution: Contribution?)
}
void addImageToWikipedia(Contribution contribution);
companion object {
/**
* Uses DiffUtil to calculate the changes in the list
* It has methods that check ID and the content of the items to determine if its a new item
*/
private val DIFF_CALLBACK: DiffUtil.ItemCallback<Contribution> =
object : DiffUtil.ItemCallback<Contribution>() {
override fun areItemsTheSame(
oldContribution: Contribution,
newContribution: Contribution
): Boolean {
return oldContribution.pageId == newContribution.pageId
}
override fun areContentsTheSame(
oldContribution: Contribution,
newContribution: Contribution
): Boolean {
return oldContribution == newContribution
}
}
}
}

View file

@ -1,25 +1,21 @@
package fr.free.nrw.commons.contributions;
package fr.free.nrw.commons.contributions
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import fr.free.nrw.commons.BasePresenter;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import fr.free.nrw.commons.BasePresenter
/**
* The contract for Contributions list View & Presenter
*/
public class ContributionsListContract {
class ContributionsListContract {
interface View {
fun showWelcomeTip(numberOfUploads: Boolean)
public interface View {
fun showProgress(shouldShow: Boolean)
void showWelcomeTip(boolean numberOfUploads);
void showProgress(boolean shouldShow);
void showNoContributionsUI(boolean shouldShow);
fun showNoContributionsUI(shouldShow: Boolean)
}
public interface UserActionListener extends BasePresenter<View> {
void refreshList(SwipeRefreshLayout swipeRefreshLayout);
interface UserActionListener : BasePresenter<View?> {
fun refreshList(swipeRefreshLayout: SwipeRefreshLayout?)
}
}

View file

@ -1,15 +1,26 @@
package fr.free.nrw.commons.contributions;
import javax.inject.Named;
import dagger.Binds;
import dagger.Module;
import dagger.Provides;
import fr.free.nrw.commons.kvstore.JsonKvStore;
/**
* The Dagger Module for contributions related presenters and (some other objects maybe in future)
* The Dagger Module for contributions-related presenters and other dependencies
*/
@Module
public abstract class ContributionsModule {
@Binds
public abstract ContributionsContract.UserActionListener bindsContibutionsPresenter(
ContributionsPresenter presenter);
public abstract ContributionsContract.UserActionListener bindsContributionsPresenter(
ContributionsPresenter presenter
);
@Provides
static JsonKvStore providesApplicationKvStore(
@Named("default_preferences") JsonKvStore kvStore
) {
return kvStore;
}
}

View file

@ -15,8 +15,8 @@ abstract class CommonsDaggerSupportFragment : Fragment(), HasSupportFragmentInje
@Inject @JvmField
var childFragmentInjector: DispatchingAndroidInjector<Fragment>? = null
@JvmField
protected var compositeDisposable: CompositeDisposable = CompositeDisposable()
// Removed @JvmField to allow overriding
protected open var compositeDisposable: CompositeDisposable = CompositeDisposable()
override fun onAttach(context: Context) {
inject()
@ -63,4 +63,9 @@ abstract class CommonsDaggerSupportFragment : Fragment(), HasSupportFragmentInje
return getInstance(activity.applicationContext)
}
// Ensure getContext() returns a non-null Context
override fun getContext(): Context {
return super.getContext() ?: throw IllegalStateException("Context is null")
}
}

View file

@ -411,7 +411,7 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
nearbyPlacesInfoObservable = presenter.loadAttractionsFromLocation(getLastMapFocus(),
currentLatLng, false);
}
compositeDisposable.add(nearbyPlacesInfoObservable
getCompositeDisposable().add(nearbyPlacesInfoObservable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(explorePlacesInfo -> {

View file

@ -493,7 +493,7 @@ class MediaDetailFragment : CommonsDaggerSupportFragment(), CategoryEditHelper.C
val contributionsFragment: ContributionsFragment? = this.getContributionsFragmentParent()
if (contributionsFragment?.binding != null) {
contributionsFragment.binding.cardViewNearby.visibility = View.GONE
contributionsFragment.binding!!.cardViewNearby.visibility = View.GONE
}
// detail provider is null when fragment is shown in review activity

View file

@ -686,7 +686,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
public void onPause() {
super.onPause();
binding.map.onPause();
compositeDisposable.clear();
getCompositeDisposable().clear();
presenter.detachView();
registerUnregisterLocationListener(true);
try {
@ -801,7 +801,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
0.75);
binding.nearbyFilterList.searchListView.setAdapter(nearbyFilterSearchRecyclerViewAdapter);
LayoutUtils.setLayoutHeightAlignedToWidth(1.25, binding.nearbyFilterList.getRoot());
compositeDisposable.add(
getCompositeDisposable().add(
RxSearchView.queryTextChanges(binding.nearbyFilter.searchViewLayout.searchView)
.takeUntil(RxView.detaches(binding.nearbyFilter.searchViewLayout.searchView))
.debounce(500, TimeUnit.MILLISECONDS)
@ -1179,7 +1179,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
*/
private void emptyCache() {
// reload the map once the cache is cleared
compositeDisposable.add(
getCompositeDisposable().add(
placesRepository.clearCache()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
@ -1214,7 +1214,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
final Observable<String> savePlacesObservable = Observable
.fromCallable(() -> nearbyController
.getPlacesAsKML(getMapFocus()));
compositeDisposable.add(savePlacesObservable
getCompositeDisposable().add(savePlacesObservable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(kmlString -> {
@ -1248,7 +1248,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
final Observable<String> savePlacesObservable = Observable
.fromCallable(() -> nearbyController
.getPlacesAsGPX(getMapFocus()));
compositeDisposable.add(savePlacesObservable
getCompositeDisposable().add(savePlacesObservable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(gpxString -> {
@ -1349,7 +1349,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
final Observable<List<Place>> getPlaceObservable = Observable
.fromCallable(() -> nearbyController
.getPlaces(List.of(place)));
compositeDisposable.add(getPlaceObservable
getCompositeDisposable().add(getPlaceObservable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(placeList -> {
@ -1393,7 +1393,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
searchLatLng,
false, true, Utils.isMonumentsEnabled(new Date()), customQuery));
compositeDisposable.add(nearbyPlacesInfoObservable
getCompositeDisposable().add(nearbyPlacesInfoObservable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(nearbyPlacesInfo -> {
@ -1430,7 +1430,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
searchLatLng,
false, true, Utils.isMonumentsEnabled(new Date()), customQuery));
compositeDisposable.add(nearbyPlacesInfoObservable
getCompositeDisposable().add(nearbyPlacesInfoObservable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(nearbyPlacesInfo -> {
@ -1462,7 +1462,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
}
public void savePlaceToDatabase(Place place) {
compositeDisposable.add(placesRepository
getCompositeDisposable().add(placesRepository
.save(place)
.subscribeOn(Schedulers.io())
.subscribe());
@ -1476,7 +1476,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
@Override
public void stopQuery() {
stopQuery = true;
compositeDisposable.clear();
getCompositeDisposable().clear();
}
/**

View file

@ -1,5 +1,6 @@
package fr.free.nrw.commons.upload.categories
import android.annotation.SuppressLint
import android.app.Activity
import android.app.ProgressDialog
import android.content.Context
@ -89,6 +90,7 @@ class UploadCategoriesFragment : UploadBaseFragment(), CategoriesContract.View {
}
}
@SuppressLint("StringFormatMatches")
private fun init() {
if (binding == null) {
return
@ -372,8 +374,9 @@ class UploadCategoriesFragment : UploadBaseFragment(), CategoriesContract.View {
(requireActivity() as AppCompatActivity).supportActionBar?.hide()
if (parentFragment?.parentFragment?.parentFragment is ContributionsFragment) {
((parentFragment?.parentFragment?.parentFragment) as ContributionsFragment).binding.cardViewNearby.visibility = View.GONE
((parentFragment?.parentFragment?.parentFragment) as ContributionsFragment).binding?.cardViewNearby?.visibility = View.GONE
}
}
}