diff --git a/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsListPresenter.kt b/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsListPresenter.kt index 1421c1757..1e36d416c 100644 --- a/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsListPresenter.kt +++ b/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsListPresenter.kt @@ -1,16 +1,20 @@ package fr.free.nrw.commons.contributions import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.Observer import androidx.paging.DataSource import androidx.paging.LivePagedListBuilder import androidx.paging.PagedList import androidx.swiperefreshlayout.widget.SwipeRefreshLayout +import fr.free.nrw.commons.auth.SessionManager import fr.free.nrw.commons.di.CommonsApplicationModule import io.reactivex.Scheduler import io.reactivex.disposables.CompositeDisposable import javax.inject.Inject import javax.inject.Named + /** * The presenter class for Contributions */ @@ -22,8 +26,15 @@ class ContributionsListPresenter @Inject internal constructor( ) : ContributionsListContract.UserActionListener { private val compositeDisposable = CompositeDisposable() + @Inject + lateinit var sessionManager: SessionManager + var contributionList: LiveData>? = null + private val liveData = MutableLiveData>() + + private val existingContributions: MutableList = ArrayList() + override fun onAttachView(view: ContributionsListContract.View) { } @@ -66,6 +77,13 @@ class ContributionsListPresenter @Inject internal constructor( } contributionList = livePagedListBuilder.build() + contributionList!!.observeForever { pagedList -> + pagedList?.let { + existingContributions.clear() + existingContributions.addAll(it) + liveData.value = existingContributions // Update liveData with the latest list + } + } } override fun onDetachView() { diff --git a/app/src/main/java/fr/free/nrw/commons/contributions/MainActivity.kt b/app/src/main/java/fr/free/nrw/commons/contributions/MainActivity.kt index a61567393..57be81f31 100644 --- a/app/src/main/java/fr/free/nrw/commons/contributions/MainActivity.kt +++ b/app/src/main/java/fr/free/nrw/commons/contributions/MainActivity.kt @@ -7,6 +7,8 @@ import android.os.Bundle import android.view.Menu import android.view.MenuItem import android.view.View +import android.view.animation.AnimationUtils +import android.widget.ImageView import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentManager import androidx.work.ExistingWorkPolicy @@ -16,6 +18,7 @@ import fr.free.nrw.commons.WelcomeActivity import fr.free.nrw.commons.auth.SessionManager import fr.free.nrw.commons.bookmarks.BookmarkFragment import fr.free.nrw.commons.contributions.ContributionsFragment.Companion.newInstance +import fr.free.nrw.commons.databinding.FragmentContributionsListBinding import fr.free.nrw.commons.databinding.MainBinding import fr.free.nrw.commons.explore.ExploreFragment import fr.free.nrw.commons.kvstore.JsonKvStore @@ -31,6 +34,7 @@ import fr.free.nrw.commons.nearby.fragments.NearbyParentFragment import fr.free.nrw.commons.nearby.fragments.NearbyParentFragment.NearbyParentFragmentInstanceReadyCallback import fr.free.nrw.commons.notification.NotificationActivity.Companion.startYourself import fr.free.nrw.commons.notification.NotificationController +import fr.free.nrw.commons.profile.ProfileActivity import fr.free.nrw.commons.quiz.QuizChecker import fr.free.nrw.commons.settings.SettingsFragment import fr.free.nrw.commons.theme.BaseActivity @@ -39,8 +43,10 @@ import fr.free.nrw.commons.upload.worker.WorkRequestHelper.Companion.makeOneTime import fr.free.nrw.commons.utils.ViewUtilWrapper import io.reactivex.Completable import io.reactivex.schedulers.Schedulers +import org.apache.commons.codec.binary.StringUtils import timber.log.Timber import java.util.Calendar +import java.util.Objects import javax.inject.Inject import javax.inject.Named @@ -58,6 +64,12 @@ class MainActivity : BaseActivity(), FragmentManager.OnBackStackChangedListener @Inject var contributionDao: ContributionDao? = null + @Inject + lateinit var contributionsListPresenter: ContributionsListPresenter + + @Inject + lateinit var dataSource: ContributionsRemoteDataSource + private var contributionsFragment: ContributionsFragment? = null private var nearbyParentFragment: NearbyParentFragment? = null private var exploreFragment: ExploreFragment? = null @@ -96,6 +108,9 @@ class MainActivity : BaseActivity(), FragmentManager.OnBackStackChangedListener var tabLayout: NavTabLayout? = null + private var refresh: FragmentContributionsListBinding? = null + + private var userName: String? = null override fun onSupportNavigateUp(): Boolean { if (activeFragment == ActiveFragment.CONTRIBUTIONS) { @@ -150,6 +165,14 @@ after opening the app. loadFragment(newInstance(), false) } } + refresh = FragmentContributionsListBinding.inflate(getLayoutInflater()); + if (getIntent().getExtras() != null) { + userName = getIntent().getExtras()?.getString(ProfileActivity.KEY_USERNAME); + } + + if (userName.isNullOrEmpty()) { + userName = sessionManager?.userName + } setUpPager() /** * Ask the user for media location access just after login @@ -455,10 +478,44 @@ after opening the app. return true } + R.id.menu_refresh-> { + return true + } else -> return super.onOptionsItemSelected(item) } } + override fun onCreateOptionsMenu(menu: Menu): Boolean { + val inflater = menuInflater + inflater.inflate(R.menu.contribution_activity_notification_menu, menu) + + val refreshItem = menu.findItem(R.id.menu_refresh) + refreshItem?.actionView?.let { actionView -> + val refreshIcon = actionView.findViewById(R.id.refresh_icon) + refreshIcon?.setOnClickListener { v -> + v.clearAnimation() + val rotateAnimation = AnimationUtils.loadAnimation(this, R.anim.rotate) + v.startAnimation(rotateAnimation) + + // Initialize userName if it's null + val localUserName = userName ?: sessionManager?.userName + + if (sessionManager?.userName == localUserName) { + refresh?.swipeRefreshLayout?.let { swipeRefresh -> + swipeRefresh.isRefreshing = true + swipeRefresh.isEnabled = true + contributionsListPresenter?.refreshList(swipeRefresh) + } + } else { + refresh?.swipeRefreshLayout?.isEnabled = false + } + } + } + + return true + } + + fun centerMapToPlace(place: Place?) { setSelectedItemId(NavTab.NEARBY.code()) nearbyParentFragment!!.setNearbyParentFragmentInstanceReadyCallback( @@ -516,6 +573,23 @@ after opening the app. } retryAllFailedUploads() + //check for new contributions + // Initialize userName if it's null + if (userName == null) { + userName = sessionManager?.userName; + } + + if (Objects.equals(sessionManager?.userName, userName)) { + if (refresh != null && refresh!!.swipeRefreshLayout != null) { + refresh!!.swipeRefreshLayout.setRefreshing(true); + refresh!!.swipeRefreshLayout.setEnabled(true); + contributionsListPresenter?.refreshList(refresh!!.swipeRefreshLayout); + } + } else { + if (refresh != null && refresh!!.swipeRefreshLayout != null) { + refresh!!.swipeRefreshLayout.setEnabled(false); + } + } } override fun onDestroy() {