Skip to content
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

Add activity stack on plugin #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package fr.afaucogney.mobile.android.flipper.flipperandroidbackstack

import android.os.Bundle
import com.google.android.material.bottomnavigation.BottomNavigationView
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.google.android.material.bottomnavigation.BottomNavigationView

class MainActivity : AppCompatActivity() {

Expand All @@ -26,4 +26,9 @@ class MainActivity : AppCompatActivity() {
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
}
}

override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.nav_host_fragment)
return navController.navigateUp() || super.onSupportNavigateUp()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.Navigation
import androidx.navigation.fragment.NavHostFragment
import fr.afaucogney.mobile.android.flipper.flipperandroidbackstack.R

class DashboardFragment : Fragment() {
Expand All @@ -26,6 +29,11 @@ class DashboardFragment : Fragment() {
dashboardViewModel.text.observe(viewLifecycleOwner, Observer {
textView.text = it
})
root.findViewById<Button>(R.id.settingsButton).setOnClickListener {
val navController =
Navigation.findNavController(requireActivity(), R.id.nav_host_fragment)
navController.navigate(R.id.action_navigation_dashboard_to_navigation_settings2)
}
return root
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package fr.afaucogney.mobile.android.flipper.flipperandroidbackstack.ui.settings

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import fr.afaucogney.mobile.android.flipper.flipperandroidbackstack.R

class SettingsFragment : Fragment() {

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_settings, container, false)
}
}
6 changes: 3 additions & 3 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
13 changes: 12 additions & 1 deletion app/src/main/res/layout/fragment_dashboard.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,15 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

<Button
android:id="@+id/settingsButton"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginBottom="36dp"
android:text="@string/settings"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
22 changes: 22 additions & 0 deletions app/src/main/res/layout/fragment_settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/text_dashboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="@string/settings"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
14 changes: 12 additions & 2 deletions app/src/main/res/navigation/mobile_navigation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,21 @@
android:id="@+id/navigation_dashboard"
android:name="fr.afaucogney.mobile.android.flipper.flipperandroidbackstack.ui.dashboard.DashboardFragment"
android:label="@string/title_dashboard"
tools:layout="@layout/fragment_dashboard" />
tools:layout="@layout/fragment_dashboard" >
<action
android:id="@+id/action_navigation_dashboard_to_navigation_settings2"
app:destination="@id/navigation_settings" />
</fragment>

<fragment
android:id="@+id/navigation_notifications"
android:name="fr.afaucogney.mobile.android.flipper.flipperandroidbackstack.ui.notifications.NotificationsFragment"
android:label="@string/title_notifications"
tools:layout="@layout/fragment_notifications" />
</navigation>

<fragment
android:id="@+id/navigation_settings"
android:name="fr.afaucogney.mobile.android.flipper.flipperandroidbackstack.ui.settings.SettingsFragment"
android:label="@string/title_settings"
tools:layout="@layout/fragment_settings" />
</navigation>
4 changes: 3 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
<string name="title_home">Home</string>
<string name="title_dashboard">Dashboard</string>
<string name="title_notifications">Notifications</string>
</resources>
<string name="settings">Settings</string>
<string name="title_settings">Settings</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,12 @@ class BackStackFlipperPlugin(app: Application) :
activityMap[this.fid]!!
.addLifeCycleEvent(event)
.addBackStackInfo(this)
.addStackInfo(this)
.addViewModelInfo(this)
.put(FRAGMENTS, fragmentMap.toFlipperObjectBuilder())
.let {
FlipperObject.Builder()
.put(this.fid, it)

}
.let {
FlipperObject.Builder()
Expand Down Expand Up @@ -246,7 +246,6 @@ class BackStackFlipperPlugin(app: Application) :
}
}


@SuppressLint("RestrictedApi")
private fun Fragment.saveAndMapToFlipperObjectBuilder(event: FragmentLifeCycle): FlipperObject.Builder {
return this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ package fr.afaucogney.mobile.flipper.internal.callback
import android.app.Activity
import android.app.Application
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import fr.afaucogney.mobile.android.flipper.R
import fr.afaucogney.mobile.flipper.internal.model.ActivityLifeCycle

internal class FlipperActivityCallback(
private val flipperPlugin: IActivityLifeCycleCallbackFlipperHandler
) : Application.ActivityLifecycleCallbacks {


override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
flipperPlugin.pushActivityEvent(activity, ActivityLifeCycle.ON_ACTIVITY_CREATED)
if (activity is FragmentActivity) {
Expand Down Expand Up @@ -61,4 +63,4 @@ internal class FlipperActivityCallback(
fun unregisterFragmentLifecycleCallback(activity: FragmentActivity)
fun unregisterFragmentBackStackLifecycleCallback(activity: FragmentActivity)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.app.Activity
import androidx.fragment.app.FragmentActivity
import com.facebook.flipper.core.FlipperObject


internal fun FlipperObject.Builder.addBackStackInfo(activity: Activity): FlipperObject.Builder {
return this.apply {
if (activity is FragmentActivity) {
Expand All @@ -16,4 +15,4 @@ internal fun FlipperObject.Builder.addBackStackInfo(activity: Activity): Flipper
put(BACK_STACK, backStack)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const val NEW_EVENT = "newEvent"
const val LIFE_CYCLE_EVENT = "lifeCycle"
const val BACK_STACK = "backStack"
const val TRASH = "trash"
const val STACK = "stack"

const val ACTIVITIES = "activities"
const val FRAGMENTS = "fragments"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package fr.afaucogney.mobile.flipper.internal.model

import android.app.Activity
import androidx.fragment.app.FragmentActivity
import com.facebook.flipper.core.FlipperObject

internal fun FlipperObject.Builder.addStackInfo(activity: Activity): FlipperObject.Builder {
return this.apply {
if (activity is FragmentActivity) {
val stack = FlipperObject.Builder()
activity.supportFragmentManager.fragments.forEachIndexed { index, fragment ->
stack.put(index.toString(), "${fragment.name} : ${fragment.id}")
}
put(STACK, stack)
}
}
}