Skip to content

Commit

Permalink
Merge branch 'release/2.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Vojtkovszky committed Jan 4, 2021
2 parents 5d5693f + fee0ea6 commit 41a2296
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ getCurrentFragment()?.let {
<br/>Let the fragment define behavioural patterns of it's own by overriding open properties:
``` kotlin
open val isModal: Boolean
open val mustBeValidToInvokeNavigation: Boolean
open val overridesBackPress: Boolean
open val animationEnter: Int
open val animationExit: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ class MainFragment : BaseSingleFragment() {
private var _binding: FragmentMainBinding? = null
private val binding get() = _binding!!

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
override val mustBeValidToInvokeNavigation: Boolean
get() = true

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
_binding = FragmentMainBinding.inflate(inflater, container, false)
return binding.root
}
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = "1.4.20"
ext.kotlin_version = "1.4.21"
repositories {
google()
jcenter()
Expand Down
4 changes: 2 additions & 2 deletions singleactivitynavigation/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {

android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
buildToolsVersion "30.0.3"

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
Expand All @@ -20,7 +20,7 @@ android {
defaultConfig {
minSdkVersion 17
targetSdkVersion 30
versionName "2.2.0"
versionName "2.3.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ abstract class BaseSingleFragment: Fragment() {
open val isModal: Boolean
get() = fragmentType == FragmentType.MODAL

/**
* If set to true, any navigation call from this fragment will check for fragment validity
* first; meaning fragment has to be added, its activity not null, not destroyed, not finishing.
* This ensures navigation methods are never invoked outside of fragment lifecycle.
* false is default.
*/
open val mustBeValidToInvokeNavigation: Boolean
get() = false

/**
* If set to true, [BaseSingleActivity]'s onBackPressed method will be prevented from invoking.
*/
Expand Down Expand Up @@ -82,65 +91,83 @@ abstract class BaseSingleFragment: Fragment() {
* Shortcut to [BaseSingleActivity.navigateBackTo]
*/
fun navigateBackTo(fragmentName: String) {
baseSingleActivity?.navigateBackTo(fragmentName)
if (canProceedWithNavigation()) {
baseSingleActivity?.navigateBackTo(fragmentName)
}
}

/**
* Shortcut to [BaseSingleActivity.navigateBack]
*/
fun navigateBack() {
baseSingleActivity?.navigateBack()
if (canProceedWithNavigation()) {
baseSingleActivity?.navigateBack()
}
}

/**
* Shortcut to [BaseSingleActivity.navigateBackToRoot]
*/
fun navigateBackToRoot() {
baseSingleActivity?.navigateBackToRoot()
if (canProceedWithNavigation()) {
baseSingleActivity?.navigateBackToRoot()
}
}

/**
* Shortcut to [BaseSingleActivity.selectRootFragment]
*/
fun selectRootFragment(positionIndex: Int = 0, popStack: Boolean = true) {
baseSingleActivity?.selectRootFragment(positionIndex, popStack)
if (canProceedWithNavigation()) {
baseSingleActivity?.selectRootFragment(positionIndex, popStack)
}
}

/**
* Shortcut to [BaseSingleActivity.navigateTo]
*/
fun navigateTo(fragment: BaseSingleFragment, openAsModal: Boolean = false,
ignoreIfAlreadyInStack: Boolean = false) {
baseSingleActivity?.navigateTo(fragment, openAsModal, ignoreIfAlreadyInStack)
if (canProceedWithNavigation()) {
baseSingleActivity?.navigateTo(fragment, openAsModal, ignoreIfAlreadyInStack)
}
}

/**
* Shortcut to [BaseSingleActivity.openBottomSheet]
*/
fun openBottomSheet(fragment: BaseSingleFragment) {
baseSingleActivity?.openBottomSheet(fragment)
if (canProceedWithNavigation()) {
baseSingleActivity?.openBottomSheet(fragment)
}
}

/**
* Shortcut to [BaseSingleActivity.openDialog]
*/
fun openDialog(fragment: BaseSingleFragment, anchorView: View? = null, useFullWidth: Boolean = true,
dialogStyle: Int = DialogFragment.STYLE_NORMAL, dialogTheme: Int = 0) {
baseSingleActivity?.openDialog(fragment, anchorView, useFullWidth, dialogStyle, dialogTheme)
if (canProceedWithNavigation()) {
baseSingleActivity?.openDialog(fragment, anchorView, useFullWidth, dialogStyle, dialogTheme)
}
}

/**
* Shortcut to [BaseSingleActivity.closeCurrentlyOpenBottomSheet]
*/
fun closeCurrentlyOpenBottomSheet() {
baseSingleActivity?.closeCurrentlyOpenBottomSheet()
if (canProceedWithNavigation()) {
baseSingleActivity?.closeCurrentlyOpenBottomSheet()
}
}

/**
* Shortcut to [BaseSingleActivity.closeCurrentlyOpenDialog]
*/
fun closeCurrentlyOpenDialog() {
baseSingleActivity?.closeCurrentlyOpenDialog()
if (canProceedWithNavigation()) {
baseSingleActivity?.closeCurrentlyOpenDialog()
}
}
// endregion shortcuts to baseSingleActivity methods

Expand All @@ -165,6 +192,17 @@ abstract class BaseSingleFragment: Fragment() {
}
// endregion add bundle

// region private methods
private fun canProceedWithNavigation(): Boolean {
return if (mustBeValidToInvokeNavigation) isValid() else true
}

private fun isValid(): Boolean {
return baseSingleActivity != null && !baseSingleActivity!!.isDestroyed &&
!baseSingleActivity!!.isFinishing && isAdded
}
// endregion private methods

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
ViewCompat.setTranslationZ(view, translationZ)
super.onViewCreated(view, savedInstanceState)
Expand Down

0 comments on commit 41a2296

Please sign in to comment.