Skip to content

Commit

Permalink
feat: added the external router for deep links
Browse files Browse the repository at this point in the history
  • Loading branch information
dixidroid committed May 17, 2024
1 parent ae777dd commit 2031380
Show file tree
Hide file tree
Showing 27 changed files with 510 additions and 46 deletions.
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
android:exported="true"
android:fitsSystemWindows="true"
android:theme="@style/Theme.App.Starting"
android:windowSoftInputMode="adjustPan">
android:windowSoftInputMode="adjustPan"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
11 changes: 8 additions & 3 deletions app/src/main/java/org/openedx/app/AppActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,15 @@ class AppActivity : AppCompatActivity(), InsetHolder, WindowSizeHolder {
super.onStart()

if (viewModel.isBranchEnabled) {
val callback = BranchUniversalReferralInitListener { _, linkProperties, error ->
if (linkProperties != null) {
val callback = BranchUniversalReferralInitListener { branchUniversalObject, _, error ->
if (branchUniversalObject?.contentMetadata?.customMetadata != null) {
branchLogger.i { "Branch init complete." }
branchLogger.i { linkProperties.controlParams.toString() }
branchLogger.i { branchUniversalObject.contentMetadata.customMetadata.toString() }
viewModel.makeExternalRoute(
supportFragmentManager,
branchUniversalObject.contentMetadata.customMetadata
)

} else if (error != null) {
branchLogger.e { "Branch init failed. Caused by -" + error.message }
}
Expand Down
299 changes: 299 additions & 0 deletions app/src/main/java/org/openedx/app/AppExternalRouter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,299 @@
package org.openedx.app

import androidx.fragment.app.FragmentManager
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.openedx.auth.presentation.signin.SignInFragment
import org.openedx.core.CourseTab
import org.openedx.core.FragmentViewType
import org.openedx.core.HomeTab
import org.openedx.core.config.Config
import org.openedx.core.data.storage.CorePreferences
import org.openedx.core.presentation.course.CourseViewMode
import org.openedx.course.domain.interactor.CourseInteractor
import org.openedx.course.presentation.handouts.HandoutsType
import org.openedx.discovery.presentation.catalog.WebViewLink
import org.openedx.discussion.domain.interactor.DiscussionInteractor
import org.openedx.discussion.presentation.topics.DiscussionTopicsViewModel
import kotlin.coroutines.CoroutineContext

class AppExternalRouter(
private val appRouter: AppRouter,
private val config: Config,
private val corePreferences: CorePreferences,
private val discussionInteractor: DiscussionInteractor,
private val courseInteractor: CourseInteractor
) : CoroutineScope {

private val isUserLoggedIn get() = corePreferences.user != null

override val coroutineContext: CoroutineContext
get() = Dispatchers.Default

fun makeRoute(fm: FragmentManager, params: Map<String, String>) {
val screenName = params["screen_name"] ?: return
when (screenName) {
// Discovery
Screen.DISCOVERY.screenName -> {
navigateToDiscoveryScreen(fm = fm)
}

Screen.DISCOVERY_COURSE_DETAIL.screenName -> {
params["course_id"]?.let { courseId ->
navigateToDiscoveryScreen(fm = fm)
appRouter.navigateToCourseInfo(
fm = fm,
courseId = courseId,
infoType = WebViewLink.Authority.COURSE_INFO.name
)
}
}

Screen.DISCOVERY_PROGRAM_DETAIL.screenName -> {
params["path_id"]?.let { pathId ->
navigateToDiscoveryScreen(fm = fm)
appRouter.navigateToCourseInfo(
fm = fm,
courseId = pathId,
infoType = WebViewLink.Authority.PROGRAM_INFO.name
)
}
}
}

if (!isUserLoggedIn) {
if (appRouter.getVisibleFragment(fm = fm) !is SignInFragment) {
appRouter.navigateToSignIn(
fm = fm,
courseId = null,
infoType = null
)
}
return
}

when (screenName) {
// Course
Screen.COURSE_DASHBOARD.screenName -> {
params["course_id"]?.let { courseId ->
appRouter.navigateToCourseOutline(
fm = fm,
courseId = courseId,
courseTitle = "",
enrollmentMode = "",
selectedTab = CourseTab.COURSE
)
}
}

Screen.COURSE_VIDEOS.screenName -> {
params["course_id"]?.let { courseId ->
appRouter.navigateToCourseOutline(
fm = fm,
courseId = courseId,
courseTitle = "",
enrollmentMode = "",
selectedTab = CourseTab.VIDEOS
)
}
}

Screen.COURSE_DISCUSSION.screenName -> {
params["course_id"]?.let { courseId ->
appRouter.navigateToCourseOutline(
fm = fm,
courseId = courseId,
courseTitle = "",
enrollmentMode = "",
selectedTab = CourseTab.DISCUSSION
)
}
}

Screen.COURSE_DATES.screenName -> {
params["course_id"]?.let { courseId ->
appRouter.navigateToCourseOutline(
fm = fm,
courseId = courseId,
courseTitle = "",
enrollmentMode = "",
selectedTab = CourseTab.DATES
)
}
}

Screen.COURSE_HANDOUT.screenName -> {
params["course_id"]?.let { courseId ->
appRouter.navigateToHandoutsWebView(
fm = fm,
courseId = courseId,
type = HandoutsType.Handouts
)
}
}

Screen.COURSE_ANNOUNCEMENT.screenName -> {
params["course_id"]?.let { courseId ->
appRouter.navigateToHandoutsWebView(
fm = fm,
courseId = courseId,
type = HandoutsType.Announcements
)
}
}

Screen.COURSE_COMPONENT.screenName -> {
params["course_id"]?.let { courseId ->
params["component_id"]?.let { componentId ->
launch {
try {
val courseStructure = courseInteractor.getCourseStructure(courseId)
courseStructure.blockData
.find { it.descendants.contains(componentId) }?.let { block ->
appRouter.navigateToCourseContainer(
fm = fm,
courseId = courseId,
unitId = block.id,
componentId = componentId,
mode = CourseViewMode.FULL
)
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
}
}

// Program
Screen.PROGRAM.screenName -> {
val pathId = params["path_id"]
if (pathId == null) {
appRouter.navigateToMain(
fm = fm,
courseId = null,
infoType = null,
selectedTab = HomeTab.PROGRAMS
)
} else {
appRouter.navigateToEnrolledProgramInfo(
fm = fm,
pathId = pathId
)
}
}

// Discussions
Screen.DISCUSSION_TOPIC.screenName -> {
params["course_id"]?.let { courseId ->
params["topic_id"]?.let { topicId ->
launch {
try {
discussionInteractor.getCourseTopics(courseId)
.find { it.id == topicId }?.let { topic ->
appRouter.navigateToDiscussionThread(
fm = fm,
action = DiscussionTopicsViewModel.TOPIC,
courseId = courseId,
topicId = topicId,
title = topic.name,
viewType = FragmentViewType.FULL_CONTENT
)
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
}
}

Screen.DISCUSSION_POST.screenName -> {
params["course_id"]?.let { courseId ->
params["topic_id"]?.let { topicId ->
params["thread_id"]?.let { threadId ->
launch {
try {
val thread = discussionInteractor.getThread(
threadId,
courseId,
topicId
)
launch(Dispatchers.Main) {
appRouter.navigateToDiscussionComments(
fm = fm,
thread = thread
)
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
}
}
}

Screen.DISCUSSION_COMMENT.screenName -> {
params["comment_id"]?.let { commentId ->
launch {
try {
val commentsData = discussionInteractor.getThreadComment(commentId)
commentsData.results.firstOrNull()?.let { comment ->
launch(Dispatchers.Main) {
appRouter.navigateToDiscussionResponses(
fm = fm,
comment = comment,
isClosed = false
)
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
}

// Profile
Screen.PROFILE.screenName,
Screen.USER_PROFILE.screenName -> {
appRouter.navigateToMain(
fm = fm,
courseId = null,
infoType = null,
selectedTab = HomeTab.PROFILE
)
}
}
}

private fun navigateToDiscoveryScreen(fm: FragmentManager) {
if (isUserLoggedIn) {
fm.popBackStack()
fm.beginTransaction()
.replace(R.id.container, MainFragment.newInstance())
.commitNow()
} else if (config.isPreLoginExperienceEnabled()) {
if (appRouter.getVisibleFragment(fm = fm) !is SignInFragment) {
appRouter.navigateToSignIn(
fm = fm,
courseId = null,
infoType = null
)
}
} else if (config.getDiscoveryConfig().isViewTypeWebView()) {
appRouter.navigateToWebDiscoverCourses(
fm = fm,
querySearch = ""
)
} else {
appRouter.navigateToNativeDiscoverCourses(
fm = fm,
querySearch = ""
)
}
}
}
Loading

0 comments on commit 2031380

Please sign in to comment.