Repository navigation
Generate share links for CYOA tools (GT-3080) #4588
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,19 +6,26 @@ import android.os.Bundle | |
| import android.view.MenuItem | ||
| import androidx.activity.viewModels | ||
| import androidx.annotation.VisibleForTesting | ||
| import androidx.fragment.app.Fragment | ||
| import androidx.fragment.app.FragmentManager | ||
| import androidx.fragment.app.FragmentManager.POP_BACK_STACK_INCLUSIVE | ||
| import androidx.fragment.app.commit | ||
| import androidx.lifecycle.MutableLiveData | ||
| import androidx.lifecycle.asFlow | ||
| import androidx.lifecycle.asLiveData | ||
| import androidx.lifecycle.switchMap | ||
| import dagger.hilt.android.AndroidEntryPoint | ||
| import java.util.Locale | ||
| import kotlinx.coroutines.flow.combine | ||
| import org.ccci.gto.android.common.androidx.fragment.app.backStackEntries | ||
| import org.ccci.gto.android.common.androidx.fragment.app.hasPendingActions | ||
| import org.ccci.gto.android.common.androidx.lifecycle.ImmutableLiveData | ||
| import org.ccci.gto.android.common.androidx.lifecycle.combineWith | ||
| import org.ccci.gto.android.common.androidx.lifecycle.toggleValue | ||
| import org.cru.godtools.base.HOST_DYNALINKS | ||
| import org.cru.godtools.base.HOST_GODTOOLSAPP_COM | ||
| import org.cru.godtools.base.SCHEME_GODTOOLS | ||
| import org.cru.godtools.base.URI_SHARE_BASE | ||
| import org.cru.godtools.base.tool.activity.MultiLanguageToolActivity | ||
| import org.cru.godtools.base.tool.model.Event | ||
| import org.cru.godtools.shared.tool.parser.model.Manifest | ||
|
|
@@ -49,6 +56,14 @@ class CyoaActivity : | |
| // track this tool open | ||
| if (savedInstanceState == null) dataModel.toolCode.value?.let { trackToolOpen(it, Manifest.Type.CYOA) } | ||
|
|
||
| supportFragmentManager.registerFragmentLifecycleCallbacks( | ||
| object : FragmentManager.FragmentLifecycleCallbacks() { | ||
| override fun onFragmentStarted(fm: FragmentManager, f: Fragment) = updatePageFragmentLiveData() | ||
| override fun onFragmentDestroyed(fm: FragmentManager, f: Fragment) = updatePageFragmentLiveData() | ||
| }, | ||
| false | ||
| ) | ||
|
|
||
| dataModel.activeManifest.observe(this) { it?.let { showInitialPageIfNecessary(it) } } | ||
| } | ||
|
|
||
|
|
@@ -181,6 +196,31 @@ class CyoaActivity : | |
| // endregion Training Tips | ||
| // endregion UI | ||
|
|
||
| // region Share Link Logic | ||
| @VisibleForTesting(otherwise = VisibleForTesting.PROTECTED) | ||
| public override val shareLinkUriLiveData by lazy { | ||
| dataModel.activeManifest.combineWith(activePageLiveData) { manifest, page -> | ||
| manifest?.buildShareLink(page)?.build()?.toString() | ||
| } | ||
| } | ||
|
|
||
| private val activePageLiveData by lazy { | ||
| pageFragmentLiveData.switchMap { it?.page ?: ImmutableLiveData<Page?>(null) } | ||
| } | ||
|
|
||
| private fun Manifest.buildShareLink(page: Page?): Uri.Builder? { | ||
| val tool = code ?: return null | ||
| val locale = locale ?: return null | ||
| return URI_SHARE_BASE.buildUpon() | ||
| .appendEncodedPath(locale.toString().lowercase(Locale.ENGLISH)) | ||
| .appendPath("tool") | ||
| .appendPath("v2") | ||
| .appendPath(tool) | ||
| .apply { page?.let { appendPath(it.id) } } | ||
|
Comment on lines
+216
to
+219
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you are properly handling page collection pages here or card collection pages |
||
| .appendQueryParameter("icid", "gtshare") | ||
| } | ||
| // endregion Share Link Logic | ||
|
|
||
| // region Page management | ||
| @VisibleForTesting | ||
| internal val pageFragment | ||
|
|
@@ -190,6 +230,13 @@ class CyoaActivity : | |
| } | ||
| private val activePage get() = pageFragment?.page?.value | ||
|
|
||
| private val pageFragmentLiveData = MutableLiveData<CyoaPageFragment<*, *>?>(null) | ||
|
|
||
| private fun updatePageFragmentLiveData() { | ||
| val fragment = supportFragmentManager.primaryNavigationFragment as? CyoaPageFragment<*, *> | ||
| if (pageFragmentLiveData.value !== fragment) pageFragmentLiveData.value = fragment | ||
| } | ||
|
Comment on lines
+233
to
+238
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you shouldn't be storing the You should probably just store what page the fragment is for |
||
|
|
||
| private fun showInitialPageIfNecessary(manifest: Manifest) { | ||
| if (pageFragment != null) return | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
locale.toString()might be wrong for locales with regions, if it's a javaLocaleobject you will want to uselocale.toLanguageTag(), if it's the fluidsonicLocaleobject, I'm not totally sure what format it uses for.toString()