Skip to content

Instantly share code, notes, and snippets.

@BenHenning
Created January 29, 2020 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BenHenning/806b67b0dd6450944febb825465b9858 to your computer and use it in GitHub Desktop.
Save BenHenning/806b67b0dd6450944febb825465b9858 to your computer and use it in GitHub Desktop.
// Based on structure in https://gist.github.com/BenHenning/0e3a3c4a798dbe4ffb884144e2b7e592.
private const val CACHE_NAME = "topic_progress_database"
private const val CHAPTER_PROGRESS_ID = "ChapterProgressDataProvider"
class StoryProgressController @Inject constructor(private val persistentCacheStoreFactory: PersistentCacheStore.Factory, private val dataProviders: DataProviders) {
private val cacheStoreMap = mutableMapOf<ProfileId, PersistentCacheStore<TopicProgressDatabase>>()
fun lookUpChapterProgress(val topicId, val storyId, val explorationId, profileId: ProfileId): DataProvider<ChapterPlayState> {
return dataProviders.transform(retrieveCacheStore(profileId), CHAPTER_PROGRESS_ID) { database ->
// Needs error checking & defaulting. Maybe proto does this directly?
database.getTopicProgressMap()[topicId].getStoreProgressMap()[storyId].getChapterProgressMap()[explorationId]
}
}
private fun retrieveCacheStore(profileId: ProfileId): PersistentCacheStore<TopicProgressDatabase> {
if (profileId in cacheStoreMap) {
return cacheStoreMap[profileId]
} else {
val cacheStore = persistentCacheStoreFactory.createPerProfile(CACHE_NAME, TopicProgressDatabase.getDefaultInstance(), profileId)
cacheStoreMap[profileId] = cacheStore
return cacheStore
}
}
}
@rt4914
Copy link

rt4914 commented Jan 30, 2020

@BenHenning This worked. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment