Monument utils converted to kotlin

This commit is contained in:
Paul Hawke 2025-07-02 21:11:42 -05:00
parent f8f0a94500
commit eca14b8ffe
6 changed files with 56 additions and 68 deletions

View file

@ -60,10 +60,12 @@ import fr.free.nrw.commons.utils.ConfigUtils.isBetaFlavour
import fr.free.nrw.commons.utils.DialogUtil.showAlertDialog import fr.free.nrw.commons.utils.DialogUtil.showAlertDialog
import fr.free.nrw.commons.utils.LengthUtils.computeBearing import fr.free.nrw.commons.utils.LengthUtils.computeBearing
import fr.free.nrw.commons.utils.LengthUtils.formatDistanceBetween import fr.free.nrw.commons.utils.LengthUtils.formatDistanceBetween
import fr.free.nrw.commons.utils.Monuments
import fr.free.nrw.commons.utils.NetworkUtils.isInternetConnectionEstablished import fr.free.nrw.commons.utils.NetworkUtils.isInternetConnectionEstablished
import fr.free.nrw.commons.utils.PermissionUtils.hasPermission import fr.free.nrw.commons.utils.PermissionUtils.hasPermission
import fr.free.nrw.commons.utils.ViewUtil.showLongToast import fr.free.nrw.commons.utils.ViewUtil.showLongToast
import fr.free.nrw.commons.utils.isMonumentsEnabled
import fr.free.nrw.commons.utils.wLMEndDate
import fr.free.nrw.commons.utils.wLMStartDate
import io.reactivex.Observable import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable import io.reactivex.disposables.CompositeDisposable
@ -242,8 +244,8 @@ class ContributionsFragment : CommonsDaggerSupportFragment(), FragmentManager.On
private fun initWLMCampaign() { private fun initWLMCampaign() {
wlmCampaign = Campaign( wlmCampaign = Campaign(
getString(R.string.wlm_campaign_title), getString(R.string.wlm_campaign_title),
getString(R.string.wlm_campaign_description), Monuments.getWLMStartDate().toString(), getString(R.string.wlm_campaign_description), wLMStartDate,
Monuments.getWLMEndDate().toString(), NearbyParentFragment.WLM_URL, true wLMEndDate, NearbyParentFragment.WLM_URL, true
) )
} }
@ -729,7 +731,7 @@ class ContributionsFragment : CommonsDaggerSupportFragment(), FragmentManager.On
* of campaigns on the campaigns card * of campaigns on the campaigns card
*/ */
private fun fetchCampaigns() { private fun fetchCampaigns() {
if (Monuments.isMonumentsEnabled(Date())) { if (isMonumentsEnabled) {
if (binding != null) { if (binding != null) {
binding!!.campaignsView.setCampaign(wlmCampaign) binding!!.campaignsView.setCampaign(wlmCampaign)
binding!!.campaignsView.visibility = View.VISIBLE binding!!.campaignsView.visibility = View.VISIBLE

View file

@ -91,13 +91,11 @@ import fr.free.nrw.commons.nearby.contract.NearbyParentFragmentContract
import fr.free.nrw.commons.nearby.model.BottomSheetItem import fr.free.nrw.commons.nearby.model.BottomSheetItem
import fr.free.nrw.commons.nearby.presenter.NearbyParentFragmentPresenter import fr.free.nrw.commons.nearby.presenter.NearbyParentFragmentPresenter
import fr.free.nrw.commons.upload.FileUtils import fr.free.nrw.commons.upload.FileUtils
import fr.free.nrw.commons.utils.ClipboardUtils
import fr.free.nrw.commons.utils.DialogUtil.showAlertDialog import fr.free.nrw.commons.utils.DialogUtil.showAlertDialog
import fr.free.nrw.commons.utils.ExecutorUtils.get import fr.free.nrw.commons.utils.ExecutorUtils.get
import fr.free.nrw.commons.utils.LayoutUtils.getScreenWidth import fr.free.nrw.commons.utils.LayoutUtils.getScreenWidth
import fr.free.nrw.commons.utils.LayoutUtils.setLayoutHeightAlignedToWidth import fr.free.nrw.commons.utils.LayoutUtils.setLayoutHeightAlignedToWidth
import fr.free.nrw.commons.utils.MapUtils.defaultLatLng import fr.free.nrw.commons.utils.MapUtils.defaultLatLng
import fr.free.nrw.commons.utils.Monuments
import fr.free.nrw.commons.utils.NearbyFABUtils.addAnchorToBigFABs import fr.free.nrw.commons.utils.NearbyFABUtils.addAnchorToBigFABs
import fr.free.nrw.commons.utils.NearbyFABUtils.addAnchorToSmallFABs import fr.free.nrw.commons.utils.NearbyFABUtils.addAnchorToSmallFABs
import fr.free.nrw.commons.utils.NearbyFABUtils.removeAnchorFromFAB import fr.free.nrw.commons.utils.NearbyFABUtils.removeAnchorFromFAB
@ -107,6 +105,7 @@ import fr.free.nrw.commons.utils.UrlUtils
import fr.free.nrw.commons.utils.ViewUtil.showLongToast import fr.free.nrw.commons.utils.ViewUtil.showLongToast
import fr.free.nrw.commons.utils.copyToClipboard import fr.free.nrw.commons.utils.copyToClipboard
import fr.free.nrw.commons.utils.handleGeoCoordinates import fr.free.nrw.commons.utils.handleGeoCoordinates
import fr.free.nrw.commons.utils.isMonumentsEnabled
import fr.free.nrw.commons.wikidata.WikidataConstants import fr.free.nrw.commons.wikidata.WikidataConstants
import fr.free.nrw.commons.wikidata.WikidataEditListener import fr.free.nrw.commons.wikidata.WikidataEditListener
import fr.free.nrw.commons.wikidata.WikidataEditListener.WikidataP18EditListener import fr.free.nrw.commons.wikidata.WikidataEditListener.WikidataP18EditListener
@ -468,7 +467,7 @@ class NearbyParentFragment : CommonsDaggerSupportFragment(),
} }
} }
_isDarkTheme = systemThemeUtils?.isDeviceInNightMode() == true _isDarkTheme = systemThemeUtils?.isDeviceInNightMode() == true
if (Monuments.isMonumentsEnabled(Date())) { if (isMonumentsEnabled) {
binding?.rlContainerWlmMonthMessage?.visibility = View.VISIBLE binding?.rlContainerWlmMonthMessage?.visibility = View.VISIBLE
} else { } else {
binding?.rlContainerWlmMonthMessage?.visibility = View.GONE binding?.rlContainerWlmMonthMessage?.visibility = View.GONE
@ -837,7 +836,7 @@ class NearbyParentFragment : CommonsDaggerSupportFragment(),
loadAnimations() loadAnimations()
setBottomSheetCallbacks() setBottomSheetCallbacks()
addActionToTitle() addActionToTitle()
if (!Monuments.isMonumentsEnabled(Date())) { if (!isMonumentsEnabled) {
NearbyFilterState.setWlmSelected(false) NearbyFilterState.setWlmSelected(false)
} }
} }
@ -1580,7 +1579,7 @@ class NearbyParentFragment : CommonsDaggerSupportFragment(),
searchLatLng, searchLatLng,
false, false,
true, true,
Monuments.isMonumentsEnabled(Date()), isMonumentsEnabled,
customQuery customQuery
) )
} }
@ -1633,7 +1632,7 @@ class NearbyParentFragment : CommonsDaggerSupportFragment(),
searchLatLng, searchLatLng,
false, false,
true, true,
Monuments.isMonumentsEnabled(Date()), isMonumentsEnabled,
customQuery customQuery
) )
} }

View file

@ -5,7 +5,7 @@ import fr.free.nrw.commons.contributions.Contribution
import fr.free.nrw.commons.filepicker.UploadableFile.DateTimeWithSource import fr.free.nrw.commons.filepicker.UploadableFile.DateTimeWithSource
import fr.free.nrw.commons.settings.Prefs.Licenses import fr.free.nrw.commons.settings.Prefs.Licenses
import fr.free.nrw.commons.utils.ConfigUtils.getVersionNameWithSha import fr.free.nrw.commons.utils.ConfigUtils.getVersionNameWithSha
import fr.free.nrw.commons.utils.Monuments import fr.free.nrw.commons.utils.getWikiLovesMonumentsYear
import org.apache.commons.lang3.StringUtils import org.apache.commons.lang3.StringUtils
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.Calendar import java.util.Calendar
@ -49,7 +49,7 @@ class PageContentsCreator @Inject constructor(private val context: Context) {
String.format( String.format(
Locale.ENGLISH, Locale.ENGLISH,
"{{Wiki Loves Monuments %d|1= %s}}\n", "{{Wiki Loves Monuments %d|1= %s}}\n",
Monuments.getWikiLovesMonumentsYear(Calendar.getInstance()), getWikiLovesMonumentsYear(Calendar.getInstance()),
contribution.countryCode contribution.countryCode
) )
) )

View file

@ -1,52 +0,0 @@
package fr.free.nrw.commons.utils;
import java.util.Calendar;
import java.util.Date;
public final class Monuments {
/**
* For now we are enabling the monuments only when the date lies between 1 Sept & 31 OCt
* @param date
* @return
*/
public static boolean isMonumentsEnabled(final Date date) {
if (date.getMonth() == 8) {
return true;
}
return false;
}
/**
* Util function to get the start date of wlm monument
* For this release we are hardcoding it to be 1st September
* @return
*/
public static String getWLMStartDate() {
return "1 Sep";
}
/***
* Util function to get the end date of wlm monument
* For this release we are hardcoding it to be 31st October
* @return
*/
public static String getWLMEndDate() {
return "30 Sep";
}
/***
* Function to get the current WLM year
* It increments at the start of September in line with the other WLM functions
* (No consideration of locales for now)
* @param calendar
* @return
*/
public static int getWikiLovesMonumentsYear(Calendar calendar) {
int year = calendar.get(Calendar.YEAR);
if (calendar.get(Calendar.MONTH) < Calendar.SEPTEMBER) {
year -= 1;
}
return year;
}
}

View file

@ -0,0 +1,39 @@
package fr.free.nrw.commons.utils
import java.util.Calendar
import java.util.Date
/**
* Get the start date of wlm monument
* For this release we are hardcoding it to be 1st September
* @return
*/
const val wLMStartDate: String = "1 Sep"
/***
* Get the end date of wlm monument
* For this release we are hardcoding it to be 31st October
* @return
*/
const val wLMEndDate: String = "30 Sep"
/**
* For now we are enabling the monuments only when the date lies between 1 Sept & 31 OCt
*/
val isMonumentsEnabled: Boolean
get() = Date().month == 8
/***
* Function to get the current WLM year
* It increments at the start of September in line with the other WLM functions
* (No consideration of locales for now)
* @param calendar
* @return
*/
fun getWikiLovesMonumentsYear(calendar: Calendar): Int {
var year = calendar[Calendar.YEAR]
if (calendar[Calendar.MONTH] < Calendar.SEPTEMBER) {
year -= 1
}
return year
}

View file

@ -1,6 +1,6 @@
package fr.free.nrw.commons package fr.free.nrw.commons
import fr.free.nrw.commons.utils.Monuments import fr.free.nrw.commons.utils.getWikiLovesMonumentsYear
import org.junit.Test import org.junit.Test
import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Assertions
import java.util.Calendar import java.util.Calendar
@ -10,20 +10,20 @@ class UtilsTest {
fun wikiLovesMonumentsYearBeforeSeptember() { fun wikiLovesMonumentsYearBeforeSeptember() {
val cal = Calendar.getInstance() val cal = Calendar.getInstance()
cal.set(2022, Calendar.FEBRUARY, 1) cal.set(2022, Calendar.FEBRUARY, 1)
Assertions.assertEquals(2021, Monuments.getWikiLovesMonumentsYear(cal)) Assertions.assertEquals(2021, getWikiLovesMonumentsYear(cal))
} }
@Test @Test
fun wikiLovesMonumentsYearInSeptember() { fun wikiLovesMonumentsYearInSeptember() {
val cal = Calendar.getInstance() val cal = Calendar.getInstance()
cal.set(2022, Calendar.SEPTEMBER, 1) cal.set(2022, Calendar.SEPTEMBER, 1)
Assertions.assertEquals(2022, Monuments.getWikiLovesMonumentsYear(cal)) Assertions.assertEquals(2022, getWikiLovesMonumentsYear(cal))
} }
@Test @Test
fun wikiLovesMonumentsYearAfterSeptember() { fun wikiLovesMonumentsYearAfterSeptember() {
val cal = Calendar.getInstance() val cal = Calendar.getInstance()
cal.set(2022, Calendar.DECEMBER, 1) cal.set(2022, Calendar.DECEMBER, 1)
Assertions.assertEquals(2022, Monuments.getWikiLovesMonumentsYear(cal)) Assertions.assertEquals(2022, getWikiLovesMonumentsYear(cal))
} }
} }