-
-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2872 from TurnrDev/feature/sunsetlit
Only show lit quests during night-time
- Loading branch information
Showing
9 changed files
with
91 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
app/src/main/java/de/westnordost/streetcomplete/data/quest/DayNightCycle.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package de.westnordost.streetcomplete.data.quest | ||
|
||
enum class DayNightCycle { DAY_AND_NIGHT, ONLY_DAY, ONLY_NIGHT } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
app/src/main/java/de/westnordost/streetcomplete/data/visiblequests/DayNightQuestFilter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package de.westnordost.streetcomplete.data.visiblequests | ||
|
||
import de.westnordost.streetcomplete.data.quest.DayNightCycle.* | ||
import de.westnordost.streetcomplete.data.quest.Quest | ||
import de.westnordost.streetcomplete.util.isDay | ||
import javax.inject.Inject | ||
|
||
class DayNightQuestFilter @Inject internal constructor() { | ||
/* | ||
Might be an idea to add a listener so this is reevaluated occasionally, or something like that. | ||
However, I think it's reevaluated everytime the displayed quests are updated? | ||
*/ | ||
fun isVisible(quest: Quest): Boolean { | ||
return when (quest.type.dayNightCycle) { | ||
DAY_AND_NIGHT -> true | ||
ONLY_DAY -> isDay(quest.position) | ||
ONLY_NIGHT -> !isDay(quest.position) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
app/src/main/java/de/westnordost/streetcomplete/util/CheckIfDay.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package de.westnordost.streetcomplete.util | ||
|
||
import com.luckycatlabs.sunrisesunset.Zenith | ||
import com.luckycatlabs.sunrisesunset.calculator.SolarEventCalculator | ||
import com.luckycatlabs.sunrisesunset.dto.Location | ||
import de.westnordost.streetcomplete.data.osm.mapdata.LatLon | ||
import java.time.LocalDate | ||
import java.time.LocalTime | ||
import java.time.ZoneId | ||
import java.time.ZonedDateTime | ||
import java.util.* | ||
|
||
fun localDateToCalendar(localDate: LocalDate): Calendar { | ||
val calendar = Calendar.getInstance() | ||
calendar.set(localDate.year, localDate.month.value, localDate.dayOfMonth) | ||
return calendar | ||
} | ||
|
||
fun isDay(pos: LatLon): Boolean { | ||
/* This functions job is to check if it's currently light out. | ||
It will use the location of the node and check the civil sunrise/sunset time. | ||
Sometimes sunset is after midnight. This would actually cause sunset to be before sunrise (as it's checking 00:00 to 23:59, it'll catch the day before!), so to gnt around this, we check the next day if that's the case. | ||
*/ | ||
|
||
val timezone = TimeZone.getDefault().id | ||
val location = Location(pos.latitude, pos.longitude) | ||
val calculator = SolarEventCalculator(location, timezone) | ||
val now = ZonedDateTime.now(ZoneId.of(timezone)) | ||
val today = now.toLocalDate() | ||
|
||
val sunrise = ZonedDateTime.of(today, LocalTime.parse(calculator.computeSunriseTime(Zenith.CIVIL, localDateToCalendar(today))), ZoneId.of(timezone)) | ||
val sunset = ZonedDateTime.of(today, LocalTime.parse(calculator.computeSunsetTime(Zenith.CIVIL, localDateToCalendar(today))), ZoneId.of(timezone)) | ||
return if (sunset < sunrise) { | ||
|
||
val sunset = ZonedDateTime.of(today.plusDays(1), LocalTime.parse(calculator.computeSunsetTime(Zenith.CIVIL, localDateToCalendar(today.plusDays(1)))), ZoneId.of(timezone)) | ||
now in sunrise..sunset | ||
} else { | ||
now in sunrise..sunset | ||
} | ||
} |