Created
January 29, 2020 17:46
-
-
Save BenHenning/806b67b0dd6450944febb825465b9858 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@BenHenning This worked. Thanks