Moved Monuments related utils into their own class

This commit is contained in:
Paul Hawke 2025-07-02 16:08:55 -05:00
parent f1cf82ee34
commit 1fdc70cd9f
6 changed files with 67 additions and 60 deletions

View file

@ -30,7 +30,6 @@ import androidx.work.WorkManager
import fr.free.nrw.commons.MapController.NearbyPlacesInfo
import fr.free.nrw.commons.Media
import fr.free.nrw.commons.R
import fr.free.nrw.commons.utils.Utils
import fr.free.nrw.commons.auth.SessionManager
import fr.free.nrw.commons.campaigns.CampaignView
import fr.free.nrw.commons.campaigns.CampaignsPresenter
@ -61,6 +60,7 @@ import fr.free.nrw.commons.utils.ConfigUtils.isBetaFlavour
import fr.free.nrw.commons.utils.DialogUtil.showAlertDialog
import fr.free.nrw.commons.utils.LengthUtils.computeBearing
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.PermissionUtils.hasPermission
import fr.free.nrw.commons.utils.ViewUtil.showLongToast
@ -242,8 +242,8 @@ class ContributionsFragment : CommonsDaggerSupportFragment(), FragmentManager.On
private fun initWLMCampaign() {
wlmCampaign = Campaign(
getString(R.string.wlm_campaign_title),
getString(R.string.wlm_campaign_description), Utils.getWLMStartDate().toString(),
Utils.getWLMEndDate().toString(), NearbyParentFragment.WLM_URL, true
getString(R.string.wlm_campaign_description), Monuments.getWLMStartDate().toString(),
Monuments.getWLMEndDate().toString(), NearbyParentFragment.WLM_URL, true
)
}
@ -729,7 +729,7 @@ class ContributionsFragment : CommonsDaggerSupportFragment(), FragmentManager.On
* of campaigns on the campaigns card
*/
private fun fetchCampaigns() {
if (Utils.isMonumentsEnabled(Date())) {
if (Monuments.isMonumentsEnabled(Date())) {
if (binding != null) {
binding!!.campaignsView.setCampaign(wlmCampaign)
binding!!.campaignsView.visibility = View.VISIBLE

View file

@ -98,6 +98,7 @@ import fr.free.nrw.commons.utils.GeoCoordinates
import fr.free.nrw.commons.utils.LayoutUtils.getScreenWidth
import fr.free.nrw.commons.utils.LayoutUtils.setLayoutHeightAlignedToWidth
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.addAnchorToSmallFABs
import fr.free.nrw.commons.utils.NearbyFABUtils.removeAnchorFromFAB
@ -465,7 +466,7 @@ class NearbyParentFragment : CommonsDaggerSupportFragment(),
}
}
_isDarkTheme = systemThemeUtils?.isDeviceInNightMode() == true
if (Utils.isMonumentsEnabled(Date())) {
if (Monuments.isMonumentsEnabled(Date())) {
binding?.rlContainerWlmMonthMessage?.visibility = View.VISIBLE
} else {
binding?.rlContainerWlmMonthMessage?.visibility = View.GONE
@ -834,7 +835,7 @@ class NearbyParentFragment : CommonsDaggerSupportFragment(),
loadAnimations()
setBottomSheetCallbacks()
addActionToTitle()
if (!Utils.isMonumentsEnabled(Date())) {
if (!Monuments.isMonumentsEnabled(Date())) {
NearbyFilterState.setWlmSelected(false)
}
}
@ -1578,7 +1579,7 @@ class NearbyParentFragment : CommonsDaggerSupportFragment(),
searchLatLng,
false,
true,
Utils.isMonumentsEnabled(Date()),
Monuments.isMonumentsEnabled(Date()),
customQuery
)
}
@ -1631,7 +1632,7 @@ class NearbyParentFragment : CommonsDaggerSupportFragment(),
searchLatLng,
false,
true,
Utils.isMonumentsEnabled(Date()),
Monuments.isMonumentsEnabled(Date()),
customQuery
)
}

View file

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

View file

@ -0,0 +1,52 @@
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

@ -16,8 +16,6 @@ import androidx.browser.customtabs.CustomTabsIntent;
import androidx.core.content.ContextCompat;
import fr.free.nrw.commons.R;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Pattern;
@ -119,48 +117,4 @@ public class Utils {
textView.setText(content);
}
/**
* 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

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