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 Jun 7, 2024
1 parent 09b422a commit db98d4b
Show file tree
Hide file tree
Showing 25 changed files with 730 additions and 39 deletions.
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,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 @@ -18,6 +18,7 @@ import io.branch.referral.Branch.BranchUniversalReferralInitListener
import org.koin.android.ext.android.inject
import org.koin.androidx.viewmodel.ext.android.viewModel
import org.openedx.app.databinding.ActivityAppBinding
import org.openedx.app.deeplink.DeepLink
import org.openedx.auth.presentation.logistration.LogistrationFragment
import org.openedx.auth.presentation.signin.SignInFragment
import org.openedx.core.data.storage.CorePreferences
Expand Down Expand Up @@ -145,10 +146,14 @@ 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(
fm = supportFragmentManager,
deepLink = DeepLink(branchUniversalObject.contentMetadata.customMetadata)
)
} else if (error != null) {
branchLogger.e { "Branch init failed. Caused by -" + error.message }
}
Expand Down
16 changes: 12 additions & 4 deletions app/src/main/java/org/openedx/app/AppRouter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,15 @@ class AppRouter : AuthRouter, DiscoveryRouter, DashboardRouter, CourseRouter, Di
ProfileRouter, AppUpgradeRouter, WhatsNewRouter {

//region AuthRouter
override fun navigateToMain(fm: FragmentManager, courseId: String?, infoType: String?) {
override fun navigateToMain(
fm: FragmentManager,
courseId: String?,
infoType: String?,
openTab: String
) {
fm.popBackStack()
fm.beginTransaction()
.replace(R.id.container, MainFragment.newInstance(courseId, infoType))
.replace(R.id.container, MainFragment.newInstance(courseId, infoType, openTab))
.commit()
}

Expand Down Expand Up @@ -286,12 +291,11 @@ class AppRouter : AuthRouter, DiscoveryRouter, DashboardRouter, CourseRouter, Di
override fun navigateToHandoutsWebView(
fm: FragmentManager,
courseId: String,
title: String,
type: HandoutsType,
) {
replaceFragmentWithBackStack(
fm,
HandoutsWebViewFragment.newInstance(title, type.name, courseId)
HandoutsWebViewFragment.newInstance(type.name, courseId)
)
}
//endregion
Expand Down Expand Up @@ -409,6 +413,10 @@ class AppRouter : AuthRouter, DiscoveryRouter, DashboardRouter, CourseRouter, Di
}
//endregion

fun getVisibleFragment(fm: FragmentManager): Fragment? {
return fm.fragments.firstOrNull { it.isVisible }
}

private fun replaceFragmentWithBackStack(fm: FragmentManager, fragment: Fragment) {
fm.beginTransaction()
.replace(R.id.container, fragment, fragment.javaClass.simpleName)
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/java/org/openedx/app/AppViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package org.openedx.app

import androidx.fragment.app.FragmentManager
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import androidx.lifecycle.viewModelScope
import androidx.room.RoomDatabase
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.openedx.app.deeplink.DeepLink
import org.openedx.app.deeplink.DeepLinkRouter
import org.openedx.app.system.notifier.AppNotifier
import org.openedx.app.system.notifier.LogoutEvent
import org.openedx.core.BaseViewModel
Expand All @@ -22,6 +25,7 @@ class AppViewModel(
private val preferencesManager: CorePreferences,
private val dispatcher: CoroutineDispatcher,
private val analytics: AppAnalytics,
private val deepLinkRouter: DeepLinkRouter,
private val fileUtil: FileUtil,
) : BaseViewModel() {

Expand Down Expand Up @@ -71,6 +75,10 @@ class AppViewModel(
preferencesManager.canResetAppDirectory = false
}

fun makeExternalRoute(fm: FragmentManager, deepLink: DeepLink) {
deepLinkRouter.makeRoute(fm, deepLink)
}

private fun setUserId() {
preferencesManager.user?.let {
analytics.setUserIdForSession(it.id)
Expand Down
38 changes: 33 additions & 5 deletions app/src/main/java/org/openedx/app/MainFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import kotlinx.coroutines.launch
import org.koin.android.ext.android.inject
import org.koin.androidx.viewmodel.ext.android.viewModel
import org.openedx.app.databinding.FragmentMainBinding
import org.openedx.app.deeplink.HomeTab
import org.openedx.core.adapter.NavigationFragmentAdapter
import org.openedx.core.presentation.global.app_upgrade.UpgradeRequiredFragment
import org.openedx.core.presentation.global.viewBinding
import org.openedx.discovery.presentation.DiscoveryRouter
import org.openedx.learn.presentation.LearnFragment
import org.openedx.learn.presentation.LearnTab
import org.openedx.profile.presentation.profile.ProfileFragment

class MainFragment : Fragment(R.layout.fragment_main) {
Expand Down Expand Up @@ -60,8 +62,6 @@ class MainFragment : Fragment(R.layout.fragment_main) {
}
true
}
// Trigger click event for the first tab on initial load
binding.bottomNavView.selectedItemId = binding.bottomNavView.selectedItemId

viewModel.isBottomBarEnabled.observe(viewLifecycleOwner) { isBottomBarEnabled ->
enableBottomBar(isBottomBarEnabled)
Expand Down Expand Up @@ -89,15 +89,37 @@ class MainFragment : Fragment(R.layout.fragment_main) {
putString(ARG_COURSE_ID, "")
putString(ARG_INFO_TYPE, "")
}

when (requireArguments().getString(ARG_OPEN_TAB, HomeTab.LEARN.name)) {
HomeTab.LEARN.name,
HomeTab.PROGRAMS.name -> {
binding.bottomNavView.selectedItemId = R.id.fragmentLearn
}

HomeTab.DISCOVER.name -> {
binding.bottomNavView.selectedItemId = R.id.fragmentDiscover
}

HomeTab.PROFILE.name -> {
binding.bottomNavView.selectedItemId = R.id.fragmentProfile
}
}
requireArguments().remove(ARG_OPEN_TAB)
}
}

private fun initViewPager() {
binding.viewPager.orientation = ViewPager2.ORIENTATION_HORIZONTAL
binding.viewPager.offscreenPageLimit = 4

val openTab = requireArguments().getString(ARG_OPEN_TAB, HomeTab.LEARN.name)
val learnTab = if (openTab == HomeTab.PROGRAMS.name) {
LearnTab.PROGRAMS
} else {
LearnTab.COURSES
}
adapter = NavigationFragmentAdapter(this).apply {
addFragment(LearnFragment())
addFragment(LearnFragment.newInstance(openTab = learnTab.name))
addFragment(viewModel.getDiscoveryFragment)
addFragment(ProfileFragment())
}
Expand All @@ -114,11 +136,17 @@ class MainFragment : Fragment(R.layout.fragment_main) {
companion object {
private const val ARG_COURSE_ID = "courseId"
private const val ARG_INFO_TYPE = "info_type"
fun newInstance(courseId: String? = null, infoType: String? = null): MainFragment {
private const val ARG_OPEN_TAB = "open_tab"
fun newInstance(
courseId: String? = null,
infoType: String? = null,
openTab: String = HomeTab.LEARN.name
): MainFragment {
val fragment = MainFragment()
fragment.arguments = bundleOf(
ARG_COURSE_ID to courseId,
ARG_INFO_TYPE to infoType
ARG_INFO_TYPE to infoType,
ARG_OPEN_TAB to openTab
)
return fragment
}
Expand Down
22 changes: 22 additions & 0 deletions app/src/main/java/org/openedx/app/deeplink/DeepLink.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.openedx.app.deeplink

class DeepLink(params: Map<String, String>) {

val screenName = params[Keys.SCREEN_NAME.value]
val courseId = params[Keys.COURSE_ID.value]
val pathId = params[Keys.PATH_ID.value]
val componentId = params[Keys.COMPONENT_ID.value]
val topicId = params[Keys.TOPIC_ID.value]
val threadId = params[Keys.THREAD_ID.value]
val commentId = params[Keys.COMMENT_ID.value]

enum class Keys(val value: String) {
SCREEN_NAME("screen_name"),
COURSE_ID("course_id"),
PATH_ID("path_id"),
COMPONENT_ID("component_id"),
TOPIC_ID("topic_id"),
THREAD_ID("thread_id"),
COMMENT_ID("comment_id")
}
}
Loading

0 comments on commit db98d4b

Please sign in to comment.