Skip to content

Commit

Permalink
Merge pull request #5 from streetcomplete/quest-order
Browse files Browse the repository at this point in the history
Quest order
  • Loading branch information
mnalis committed Jul 8, 2021
2 parents 42a5331 + 5f7fe41 commit 1d59e91
Show file tree
Hide file tree
Showing 17 changed files with 454 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ object ApplicationConstants {

const val NOTE_MIN_ZOOM = 15

/** when new quests that are appearing due to download of an area, show the hint that he can
* disable quests in the settings if more than X quests did appear */
const val QUEST_COUNT_AT_WHICH_TO_SHOW_QUEST_SELECTION_HINT = 600

/** the max age of the undo history - one cannot undo changes older than X */
const val MAX_UNDO_HISTORY_AGE = 12L * 60 * 60 * 1000 // 12 hours in ms

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package de.westnordost.streetcomplete

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/de/westnordost/streetcomplete/Prefs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ object Prefs {
const val LAST_VERSION = "lastVersion"
const val LAST_VERSION_DATA = "lastVersion_data"
const val HAS_SHOWN_TUTORIAL = "hasShownTutorial"
const val QUEST_SELECTION_HINT_STATE = "questSelectionHintState"

const val PIN_SPRITES_VERSION = "TangramPinsSpriteSheet.version"
const val PIN_SPRITES = "TangramPinsSpriteSheet.sprites"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ sealed class Notification

data class OsmUnreadMessagesNotification(val unreadMessages: Int) : Notification()
data class NewAchievementNotification(val achievement: Achievement, val level: Int): Notification()
data class NewVersionNotification(val sinceVersion: String): Notification()
data class NewVersionNotification(val sinceVersion: String): Notification()
object QuestSelectionHintNotification: Notification()
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import javax.inject.Singleton
@Singleton class NotificationsSource @Inject constructor(
private val userStore: UserStore,
private val newUserAchievementsDao: NewUserAchievementsDao,
private val questSelectionHintController: QuestSelectionHintController,
@Named("Achievements") achievements: List<Achievement>,
private val prefs: SharedPreferences
) {
Expand All @@ -41,6 +42,11 @@ import javax.inject.Singleton
onNumberOfNotificationsUpdated()
}
})
questSelectionHintController.addListener(object : QuestSelectionHintController.Listener {
override fun onQuestSelectionHintStateChanged() {
onNumberOfNotificationsUpdated()
}
})
}

fun addListener(listener: UpdateListener) {
Expand All @@ -54,11 +60,13 @@ import javax.inject.Singleton
val hasUnreadMessages = userStore.unreadMessagesCount > 0
val lastVersion = prefs.getString(Prefs.LAST_VERSION, null)
val hasNewVersion = lastVersion != null && BuildConfig.VERSION_NAME != lastVersion
val shouldShowQuestSelectionHint = questSelectionHintController.state == QuestSelectionHintState.SHOULD_SHOW
if (lastVersion == null) {
prefs.edit().putString(Prefs.LAST_VERSION, BuildConfig.VERSION_NAME).apply()
}

var notifications = 0
if (shouldShowQuestSelectionHint) notifications++
if (hasUnreadMessages) notifications++
if (hasNewVersion) notifications++
notifications += newUserAchievementsDao.getCount()
Expand Down Expand Up @@ -91,6 +99,12 @@ import javax.inject.Singleton
return OsmUnreadMessagesNotification(unreadOsmMessages)
}

val shouldShowQuestSelectionHint = questSelectionHintController.state == QuestSelectionHintState.SHOULD_SHOW
if (shouldShowQuestSelectionHint) {
questSelectionHintController.state = QuestSelectionHintState.SHOWN
return QuestSelectionHintNotification
}

return null
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package de.westnordost.streetcomplete.data.notifications

import android.content.SharedPreferences
import de.westnordost.streetcomplete.ApplicationConstants.QUEST_COUNT_AT_WHICH_TO_SHOW_QUEST_SELECTION_HINT
import de.westnordost.streetcomplete.Prefs
import de.westnordost.streetcomplete.data.quest.Quest
import de.westnordost.streetcomplete.data.quest.QuestKey
import de.westnordost.streetcomplete.data.quest.VisibleQuestsSource
import de.westnordost.streetcomplete.data.notifications.QuestSelectionHintState.*
import java.util.concurrent.CopyOnWriteArrayList
import javax.inject.Inject
import javax.inject.Singleton

@Singleton class QuestSelectionHintController @Inject constructor(
private val visibleQuestsSource: VisibleQuestsSource,
private val prefs: SharedPreferences
) {

interface Listener {
fun onQuestSelectionHintStateChanged()
}
private val listeners: MutableList<Listener> = CopyOnWriteArrayList()

var state: QuestSelectionHintState
set(value) {
prefs.edit().putString(Prefs.QUEST_SELECTION_HINT_STATE, value.toString()).apply()
listeners.forEach { it.onQuestSelectionHintStateChanged() }
}
get() {
val str = prefs.getString(Prefs.QUEST_SELECTION_HINT_STATE, null)
return if (str == null) NOT_SHOWN else QuestSelectionHintState.valueOf(str)
}

init {
visibleQuestsSource.addListener(object : VisibleQuestsSource.Listener {
override fun onUpdatedVisibleQuests(added: Collection<Quest>, removed: Collection<QuestKey>) {
if (state == NOT_SHOWN && added.size >= QUEST_COUNT_AT_WHICH_TO_SHOW_QUEST_SELECTION_HINT) {
state = SHOULD_SHOW
}
}

override fun onVisibleQuestsInvalidated() {}
})
}

fun addListener(listener: Listener) {
listeners.add(listener)
}
fun removeListener(listener: Listener) {
listeners.remove(listener)
}
}

enum class QuestSelectionHintState {
NOT_SHOWN, SHOULD_SHOW, SHOWN
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package de.westnordost.streetcomplete.notifications

import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import de.westnordost.streetcomplete.HandlesOnBackPressed
import de.westnordost.streetcomplete.R
import de.westnordost.streetcomplete.about.WhatsNewDialog
import de.westnordost.streetcomplete.data.notifications.NewAchievementNotification
import de.westnordost.streetcomplete.data.notifications.NewVersionNotification
import de.westnordost.streetcomplete.data.notifications.Notification
import de.westnordost.streetcomplete.data.notifications.OsmUnreadMessagesNotification
import de.westnordost.streetcomplete.data.notifications.*
import de.westnordost.streetcomplete.settings.SettingsActivity
import de.westnordost.streetcomplete.user.AchievementInfoFragment

/** A fragment that contains any fragments that would show notifications.
Expand All @@ -32,6 +31,16 @@ class NotificationsContainerFragment : Fragment(R.layout.fragment_notifications_
val f: Fragment = childFragmentManager.findFragmentById(R.id.achievement_info_fragment)!!
(f as AchievementInfoFragment).showNew(notification.achievement, notification.level)
}
is QuestSelectionHintNotification -> {
AlertDialog.Builder(ctx)
.setTitle(R.string.quest_selection_hint_title)
.setMessage(R.string.quest_selection_hint_message)
.setPositiveButton(R.string.quest_streetName_cantType_open_settings) { _, _ ->
startActivity(SettingsActivity.createLaunchQuestSettingsIntent(ctx))
}
.setNegativeButton(android.R.string.ok, null)
.show()
}
}
}

Expand Down
Loading

0 comments on commit 1d59e91

Please sign in to comment.