Skip to content

Commit 5f0da38

Browse files
committed
added support to attach this view to fragments too
1 parent 160acde commit 5f0da38

File tree

1 file changed

+21
-16
lines changed
  • formview/src/main/java/com/seed/widgets/formview

1 file changed

+21
-16
lines changed

formview/src/main/java/com/seed/widgets/formview/FormView.kt

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,24 +68,29 @@ class FormView @JvmOverloads constructor(
6868
}
6969

7070
private fun attachCallbacks(): FormCallbacks? {
71-
if (navHostFragmentId != 0) {
72-
val activity = context as AppCompatActivity
73-
val navHostFragment =
74-
activity.supportFragmentManager.findFragmentById(navHostFragmentId)
75-
if (navHostFragment != null) {
76-
return when (navHostFragment) {
77-
is NavHostFragment -> navHostFragment.childFragmentManager.primaryNavigationFragment
78-
else -> navHostFragment
79-
} as FormCallbacks
80-
} else {
81-
throw IllegalStateException("Nav Host Fragment was not found. Are you sure you are providing the correct id?")
82-
}
83-
}
84-
return when (context) {
85-
is AppCompatActivity -> (context as AppCompatActivity) as FormCallbacks
86-
is ContextThemeWrapper -> (((context as ContextThemeWrapper).baseContext) as AppCompatActivity) as FormCallbacks
71+
val ctx= when (context) {
72+
is AppCompatActivity -> (context as AppCompatActivity)
73+
is ContextThemeWrapper -> (((context as ContextThemeWrapper).baseContext) as AppCompatActivity)
8774
else -> null
8875
}
76+
return ctx?.let {
77+
if (navHostFragmentId != 0) {
78+
val fragment =
79+
it.supportFragmentManager.findFragmentById(navHostFragmentId)
80+
if (fragment != null) {
81+
return when (fragment) {
82+
is NavHostFragment -> fragment.childFragmentManager.primaryNavigationFragment
83+
else -> fragment
84+
} as FormCallbacks
85+
} else {
86+
throw IllegalStateException("Nav Host Fragment was not found. Are you sure you are providing the correct id?")
87+
}
88+
}
89+
it as FormCallbacks
90+
}
91+
92+
93+
8994
}
9095

9196

0 commit comments

Comments
 (0)