diff --git a/android/beagle/src/main/java/br/com/zup/beagle/android/components/ListView.kt b/android/beagle/src/main/java/br/com/zup/beagle/android/components/ListView.kt index 5729798d7f..fe1c7c2ae2 100644 --- a/android/beagle/src/main/java/br/com/zup/beagle/android/components/ListView.kt +++ b/android/beagle/src/main/java/br/com/zup/beagle/android/components/ListView.kt @@ -194,7 +194,7 @@ constructor( val width = if (orientation == RecyclerView.VERTICAL) MATCH_PARENT else WRAP_CONTENT val layoutParams = ViewGroup.LayoutParams(width, WRAP_CONTENT) it.layoutParams = layoutParams - it.addServerDrivenComponent(children[position]) + it.addView(children[position]) } return ViewHolder(view) } diff --git a/android/beagle/src/main/java/br/com/zup/beagle/android/components/TabItem.kt b/android/beagle/src/main/java/br/com/zup/beagle/android/components/TabItem.kt index bbf1d86e79..2eb17405ca 100644 --- a/android/beagle/src/main/java/br/com/zup/beagle/android/components/TabItem.kt +++ b/android/beagle/src/main/java/br/com/zup/beagle/android/components/TabItem.kt @@ -51,7 +51,7 @@ data class TabItem( override fun buildView(rootView: RootView): View { return viewFactory.makeBeagleFlexView(rootView).also { - it.addServerDrivenComponent(child) + it.addView(child) } } } diff --git a/android/beagle/src/main/java/br/com/zup/beagle/android/components/TabView.kt b/android/beagle/src/main/java/br/com/zup/beagle/android/components/TabView.kt index 350a1b7140..e8b3401b5e 100644 --- a/android/beagle/src/main/java/br/com/zup/beagle/android/components/TabView.kt +++ b/android/beagle/src/main/java/br/com/zup/beagle/android/components/TabView.kt @@ -184,7 +184,7 @@ internal class ContentAdapter( override fun instantiateItem(container: ViewGroup, position: Int): Any { val view = viewFactory.makeBeagleFlexView(rootView).also { - it.addServerDrivenComponent(children[position].child) + it.addView(children[position].child) } container.addView(view) return view diff --git a/android/beagle/src/main/java/br/com/zup/beagle/android/components/form/SimpleForm.kt b/android/beagle/src/main/java/br/com/zup/beagle/android/components/form/SimpleForm.kt index d67e6e07f0..b28a8276a1 100644 --- a/android/beagle/src/main/java/br/com/zup/beagle/android/components/form/SimpleForm.kt +++ b/android/beagle/src/main/java/br/com/zup/beagle/android/components/form/SimpleForm.kt @@ -70,7 +70,7 @@ data class SimpleForm( simpleFormViewCreated = viewFactory.makeBeagleFlexView(rootView, style ?: Style()) .apply { beagleComponent = this@SimpleForm - addChildrenForm(this) + addView(children) } return simpleFormViewCreated } @@ -81,12 +81,6 @@ data class SimpleForm( handleEvent(rootView, view, actions, analyticsValue = "onSubmit") } - private fun addChildrenForm(beagleFlexView: BeagleFlexView) { - children.forEach { child -> - beagleFlexView.addServerDrivenComponent(child) - } - } - private fun searchErrorInHierarchy(parent: ViewGroup): Boolean { var result = false diff --git a/android/beagle/src/main/java/br/com/zup/beagle/android/components/layout/ComposeComponent.kt b/android/beagle/src/main/java/br/com/zup/beagle/android/components/layout/ComposeComponent.kt index dffb936dbb..7e81dfe7c2 100644 --- a/android/beagle/src/main/java/br/com/zup/beagle/android/components/layout/ComposeComponent.kt +++ b/android/beagle/src/main/java/br/com/zup/beagle/android/components/layout/ComposeComponent.kt @@ -35,7 +35,7 @@ abstract class ComposeComponent : WidgetView() { override fun buildView(rootView: RootView): View { return viewFactory.makeBeagleFlexView(rootView).apply { - addServerDrivenComponent(build()) + addView(build()) } } diff --git a/android/beagle/src/main/java/br/com/zup/beagle/android/components/layout/Container.kt b/android/beagle/src/main/java/br/com/zup/beagle/android/components/layout/Container.kt index f5e9a43627..75953b8c5b 100644 --- a/android/beagle/src/main/java/br/com/zup/beagle/android/components/layout/Container.kt +++ b/android/beagle/src/main/java/br/com/zup/beagle/android/components/layout/Container.kt @@ -24,7 +24,6 @@ import br.com.zup.beagle.android.context.ContextComponent import br.com.zup.beagle.android.context.ContextData import br.com.zup.beagle.android.utils.StyleManager import br.com.zup.beagle.android.view.ViewFactory -import br.com.zup.beagle.android.view.custom.BeagleFlexView import br.com.zup.beagle.android.widget.RootView import br.com.zup.beagle.android.widget.WidgetView import br.com.zup.beagle.annotation.RegisterWidget @@ -63,13 +62,7 @@ data class Container( handleOnInit(rootView, view) return view.apply { - addChildren(this) - } - } - - private fun addChildren(beagleFlexView: BeagleFlexView) { - children.forEach { child -> - beagleFlexView.addServerDrivenComponent(child) + addView(children) } } } diff --git a/android/beagle/src/main/java/br/com/zup/beagle/android/components/layout/ScreenComponent.kt b/android/beagle/src/main/java/br/com/zup/beagle/android/components/layout/ScreenComponent.kt index 4ef7c41257..27306ed616 100644 --- a/android/beagle/src/main/java/br/com/zup/beagle/android/components/layout/ScreenComponent.kt +++ b/android/beagle/src/main/java/br/com/zup/beagle/android/components/layout/ScreenComponent.kt @@ -59,7 +59,7 @@ internal data class ScreenComponent( addNavigationBarIfNecessary(rootView, navigationBar, container) - container.addServerDrivenComponent(child) + container.addView(child) screenAnalyticsEvent?.let { container.addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener { diff --git a/android/beagle/src/main/java/br/com/zup/beagle/android/components/layout/ScrollView.kt b/android/beagle/src/main/java/br/com/zup/beagle/android/components/layout/ScrollView.kt index 2e2aa1d894..15142c041f 100644 --- a/android/beagle/src/main/java/br/com/zup/beagle/android/components/layout/ScrollView.kt +++ b/android/beagle/src/main/java/br/com/zup/beagle/android/components/layout/ScrollView.kt @@ -90,9 +90,7 @@ data class ScrollView( ) { val viewGroup = viewFactory.makeBeagleFlexView(rootView, styleChild) - children.forEach { component -> - viewGroup.addServerDrivenComponent(component, false) - } + viewGroup.addView(children, false) scrollView.addView(viewGroup) if (isHorizontal) { diff --git a/android/beagle/src/main/java/br/com/zup/beagle/android/components/list/ListAdapter.kt b/android/beagle/src/main/java/br/com/zup/beagle/android/components/list/ListAdapter.kt index 84b501de7b..49a75bdfaa 100644 --- a/android/beagle/src/main/java/br/com/zup/beagle/android/components/list/ListAdapter.kt +++ b/android/beagle/src/main/java/br/com/zup/beagle/android/components/list/ListAdapter.kt @@ -123,7 +123,7 @@ internal class ListAdapter( private fun generateView(newTemplate: ServerDrivenComponent) = viewFactory.makeBeagleFlexView(listViewModels.rootView).apply { setIsAutoGenerateIdEnabled(false) - addServerDrivenComponent(newTemplate, false) + addView(newTemplate, false) layoutParams = generateLayoutParams() if (orientation == RecyclerView.VERTICAL) { setHeightAutoAndDirtyAllViews() diff --git a/android/beagle/src/main/java/br/com/zup/beagle/android/components/page/PageView.kt b/android/beagle/src/main/java/br/com/zup/beagle/android/components/page/PageView.kt index 41c14efeb7..11bade5e0f 100644 --- a/android/beagle/src/main/java/br/com/zup/beagle/android/components/page/PageView.kt +++ b/android/beagle/src/main/java/br/com/zup/beagle/android/components/page/PageView.kt @@ -157,7 +157,7 @@ internal class PageViewAdapter( override fun instantiateItem(container: ViewGroup, position: Int): Any { val view = viewFactory.makeBeagleFlexView(rootView).also { - it.addServerDrivenComponent(children[position]) + it.addView(children[position]) } container.addView(view) return view diff --git a/android/beagle/src/main/java/br/com/zup/beagle/android/components/page/PageViewTwo.kt b/android/beagle/src/main/java/br/com/zup/beagle/android/components/page/PageViewTwo.kt index b456ee3e53..afc72b0154 100644 --- a/android/beagle/src/main/java/br/com/zup/beagle/android/components/page/PageViewTwo.kt +++ b/android/beagle/src/main/java/br/com/zup/beagle/android/components/page/PageViewTwo.kt @@ -121,7 +121,7 @@ internal class PageViewAdapterTwo( override fun instantiateItem(container: ViewGroup, position: Int): Any { val view = viewFactory.makeBeagleFlexView(rootView).also { - it.addServerDrivenComponent(children[position]) + it.addView(children[position]) } container.addView(view) return view diff --git a/android/beagle/src/main/java/br/com/zup/beagle/android/context/ContextComponentHandler.kt b/android/beagle/src/main/java/br/com/zup/beagle/android/context/ContextComponentHandler.kt index 6575f3d07c..7941be99aa 100644 --- a/android/beagle/src/main/java/br/com/zup/beagle/android/context/ContextComponentHandler.kt +++ b/android/beagle/src/main/java/br/com/zup/beagle/android/context/ContextComponentHandler.kt @@ -17,7 +17,7 @@ package br.com.zup.beagle.android.context import android.view.View -import br.com.zup.beagle.android.view.custom.BeagleFlexView +import br.com.zup.beagle.android.view.custom.InternalBeagleFlexView import br.com.zup.beagle.android.view.viewmodel.ScreenContextViewModel import br.com.zup.beagle.core.ServerDrivenComponent @@ -33,7 +33,7 @@ internal class ContextComponentHandler { } private fun addListenerToHandleContext(viewModel: ScreenContextViewModel, view: View) { - if (view !is BeagleFlexView) { + if (view !is InternalBeagleFlexView) { view.addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener { override fun onViewDetachedFromWindow(v: View?) {} diff --git a/android/beagle/src/main/java/br/com/zup/beagle/android/engine/renderer/RootView.kt b/android/beagle/src/main/java/br/com/zup/beagle/android/engine/renderer/RootView.kt deleted file mode 100644 index 7485409477..0000000000 --- a/android/beagle/src/main/java/br/com/zup/beagle/android/engine/renderer/RootView.kt +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 2020 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package br.com.zup.beagle.android.engine.renderer - -import android.content.Context -import androidx.appcompat.app.AppCompatActivity -import androidx.fragment.app.Fragment -import androidx.lifecycle.LifecycleOwner -import androidx.lifecycle.ViewModelStoreOwner -import br.com.zup.beagle.android.widget.RootView - -/** - * The FragmentRootView holder the reference of a fragment. - * - * @param fragment that is the parent of a view. - * @param parentId parent view id. - */ -class FragmentRootView( - val fragment: Fragment, - private val parentId: Int, - private val screenId : String -) : RootView { - - override fun getContext(): Context = fragment.requireContext() - - override fun getLifecycleOwner(): LifecycleOwner = fragment.viewLifecycleOwner - - override fun getViewModelStoreOwner(): ViewModelStoreOwner = fragment - - override fun getParentId(): Int = parentId - - override fun getScreenId(): String = screenId -} - -/** - * The ActivityRootView holder the reference of activity. - * - * @param activity that is the parent of a view. - * @param parentId parent view id. - */ -class ActivityRootView( - val activity: AppCompatActivity, - private val parentId: Int, - private val screenId : String -) : RootView { - - override fun getContext(): Context = activity - - override fun getLifecycleOwner(): LifecycleOwner = activity - - override fun getViewModelStoreOwner(): ViewModelStoreOwner = activity - - override fun getParentId(): Int = parentId - - override fun getScreenId(): String = screenId -} diff --git a/android/beagle/src/main/java/br/com/zup/beagle/android/utils/GenerateIdManager.kt b/android/beagle/src/main/java/br/com/zup/beagle/android/utils/GenerateIdManager.kt index dd06ae7314..896b6c06a8 100644 --- a/android/beagle/src/main/java/br/com/zup/beagle/android/utils/GenerateIdManager.kt +++ b/android/beagle/src/main/java/br/com/zup/beagle/android/utils/GenerateIdManager.kt @@ -17,7 +17,7 @@ package br.com.zup.beagle.android.utils import android.view.View -import br.com.zup.beagle.android.view.custom.BeagleFlexView +import br.com.zup.beagle.android.view.custom.InternalBeagleFlexView import br.com.zup.beagle.android.view.viewmodel.GenerateIdViewModel import br.com.zup.beagle.android.view.viewmodel.ListViewIdViewModel import br.com.zup.beagle.android.view.viewmodel.OnInitViewModel @@ -49,7 +49,7 @@ internal class GenerateIdManager( onInitViewModel.markToRerun() } - fun manageId(component: ServerDrivenComponent, view: BeagleFlexView) { + fun manageId(component: ServerDrivenComponent, view: InternalBeagleFlexView) { (component as? IdentifierComponent)?.let { identifierComponent -> if (view.isAutoGenerateIdEnabled()) { if (identifierComponent.id.isNullOrEmpty()) { diff --git a/android/beagle/src/main/java/br/com/zup/beagle/android/utils/ViewGroupExtensions.kt b/android/beagle/src/main/java/br/com/zup/beagle/android/utils/ViewGroupExtensions.kt index 4b4355a985..f8fac6b6fd 100644 --- a/android/beagle/src/main/java/br/com/zup/beagle/android/utils/ViewGroupExtensions.kt +++ b/android/beagle/src/main/java/br/com/zup/beagle/android/utils/ViewGroupExtensions.kt @@ -23,8 +23,6 @@ import androidx.appcompat.app.AppCompatActivity import androidx.fragment.app.Fragment import br.com.zup.beagle.android.components.utils.viewExtensionsViewFactory import br.com.zup.beagle.android.data.serializer.BeagleSerializer -import br.com.zup.beagle.android.engine.renderer.ActivityRootView -import br.com.zup.beagle.android.engine.renderer.FragmentRootView import br.com.zup.beagle.android.networking.RequestData import br.com.zup.beagle.android.view.BeagleFragment import br.com.zup.beagle.android.view.ScreenRequest @@ -33,6 +31,8 @@ import br.com.zup.beagle.android.view.custom.OnServerStateChanged import br.com.zup.beagle.android.view.custom.OnStateChanged import br.com.zup.beagle.android.view.mapper.toRequestData import br.com.zup.beagle.android.view.viewmodel.ScreenContextViewModel +import br.com.zup.beagle.android.widget.ActivityRootView +import br.com.zup.beagle.android.widget.FragmentRootView import br.com.zup.beagle.android.widget.RootView internal var beagleSerializerFactory = BeagleSerializer() diff --git a/android/beagle/src/main/java/br/com/zup/beagle/android/utils/WidgetExtensions.kt b/android/beagle/src/main/java/br/com/zup/beagle/android/utils/WidgetExtensions.kt index 55489a6b95..7ac8817b73 100644 --- a/android/beagle/src/main/java/br/com/zup/beagle/android/utils/WidgetExtensions.kt +++ b/android/beagle/src/main/java/br/com/zup/beagle/android/utils/WidgetExtensions.kt @@ -23,9 +23,9 @@ import br.com.zup.beagle.R import br.com.zup.beagle.android.action.Action import br.com.zup.beagle.android.context.Bind import br.com.zup.beagle.android.context.ContextData -import br.com.zup.beagle.android.engine.renderer.ActivityRootView -import br.com.zup.beagle.android.engine.renderer.FragmentRootView import br.com.zup.beagle.android.view.ViewFactory +import br.com.zup.beagle.android.widget.ActivityRootView +import br.com.zup.beagle.android.widget.FragmentRootView import br.com.zup.beagle.android.widget.RootView import br.com.zup.beagle.core.IdentifierComponent import br.com.zup.beagle.core.ServerDrivenComponent @@ -189,9 +189,9 @@ internal fun ServerDrivenComponent.toView( generateIdManager.createSingleManagerByRootViewId() val view = viewFactory.makeBeagleFlexView(rootView).apply { id = rootView.getParentId() - addServerDrivenComponent(this@toView) + addView(this@toView) } - view.listenerOnViewDetachedFromWindow = { + view.addListenerOnViewDetachedFromWindow { generateIdManager.onViewDetachedFromWindow(view) } return view diff --git a/android/beagle/src/main/java/br/com/zup/beagle/android/view/YogaLayout.java b/android/beagle/src/main/java/br/com/zup/beagle/android/view/YogaLayout.java index f6b298e6a2..439edac682 100755 --- a/android/beagle/src/main/java/br/com/zup/beagle/android/view/YogaLayout.java +++ b/android/beagle/src/main/java/br/com/zup/beagle/android/view/YogaLayout.java @@ -39,7 +39,7 @@ import java.util.HashMap; import java.util.Map; -import br.com.zup.beagle.android.view.custom.BeagleFlexView; +import br.com.zup.beagle.android.view.custom.InternalBeagleFlexView; @SuppressLint("ViewConstructor") public class YogaLayout extends ViewGroup { @@ -187,6 +187,43 @@ public void removeAllViewsInLayout() { super.removeAllViewsInLayout(); } + @Override + public void onLayout(boolean changed, int l, int t, int r, int b) { + // Either we are a root of a tree, or this function is called by our owner's onLayout, in which + // case our r-l and b-t are the size of our node. + if (!(getParent() instanceof YogaLayout)) { + createLayout( + MeasureSpec.makeMeasureSpec(r - l, MeasureSpec.EXACTLY), + MeasureSpec.makeMeasureSpec(b - t, MeasureSpec.EXACTLY)); + } + + applyLayoutRecursive(mYogaNode, 0, 0); + } + + /** + * This function is mostly unneeded, because Yoga is doing the measuring. Hence we only need to + * return accurate results if we are the root. + * + * @param widthMeasureSpec the suggested specification for the width + * @param heightMeasureSpec the suggested specification for the height + */ + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + if (!(getParent() instanceof YogaLayout)) { + createLayout(widthMeasureSpec, heightMeasureSpec); + } + + setMeasuredDimension( + Math.round(mYogaNode.getLayoutWidth()), + Math.round(mYogaNode.getLayoutHeight())); + } + + @Override + protected LayoutParams generateDefaultLayoutParams() { + return new LayoutParams(LayoutParams.MATCH_PARENT, + LayoutParams.MATCH_PARENT); + } + /** * Marks a particular view as "dirty" and to be relaid out. If the view is not a child of this * {@link YogaLayout}, the entire tree is traversed to find it. @@ -194,7 +231,7 @@ public void removeAllViewsInLayout() { * @param view the view to mark as dirty */ public void invalidate(View view) { - if (mYogaNodes.containsKey(view) && !(view instanceof BeagleFlexView)) { + if (mYogaNodes.containsKey(view) && !(view instanceof InternalBeagleFlexView)) { mYogaNodes.get(view).dirty(); return; } @@ -209,6 +246,41 @@ public void invalidate(View view) { invalidate(); } + public void setHeightAutoAndDirtyAllViews() { + addOnLayoutChangeListener(new OnLayoutChangeListener() { + @Override + public void onLayoutChange(View view, int left, int top, int right, + int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { + mYogaNode.setHeightAuto(); + ((YogaNodeJNIBase) mYogaNode).dirtyAllDescendants(); + } + }); + } + + public void setWidthAutoAndDirtyAllViews() { + addOnLayoutChangeListener(new OnLayoutChangeListener() { + @Override + public void onLayoutChange(View view, int left, int top, int right, + int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { + mYogaNode.setWidthAuto(); + ((YogaNodeJNIBase) mYogaNode).dirtyAllDescendants(); + } + }); + } + + + public void setWidthAndHeightAutoAndDirtyAllViews() { + addOnLayoutChangeListener(new OnLayoutChangeListener() { + @Override + public void onLayoutChange(View view, int left, int top, int right, + int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { + mYogaNode.setWidthAuto(); + mYogaNode.setHeightAuto(); + ((YogaNodeJNIBase) mYogaNode).dirtyAllDescendants(); + } + }); + } + private void removeViewFromYogaTree(View view, boolean inLayout) { final YogaNode node = mYogaNodes.get(view); if (node == null) { @@ -266,37 +338,6 @@ private void applyLayoutRecursive(YogaNode node, float xOffset, float yOffset) { } } - @Override - protected void onLayout(boolean changed, int l, int t, int r, int b) { - // Either we are a root of a tree, or this function is called by our owner's onLayout, in which - // case our r-l and b-t are the size of our node. - if (!(getParent() instanceof YogaLayout)) { - createLayout( - MeasureSpec.makeMeasureSpec(r - l, MeasureSpec.EXACTLY), - MeasureSpec.makeMeasureSpec(b - t, MeasureSpec.EXACTLY)); - } - - applyLayoutRecursive(mYogaNode, 0, 0); - } - - /** - * This function is mostly unneeded, because Yoga is doing the measuring. Hence we only need to - * return accurate results if we are the root. - * - * @param widthMeasureSpec the suggested specification for the width - * @param heightMeasureSpec the suggested specification for the height - */ - @Override - protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { - if (!(getParent() instanceof YogaLayout)) { - createLayout(widthMeasureSpec, heightMeasureSpec); - } - - setMeasuredDimension( - Math.round(mYogaNode.getLayoutWidth()), - Math.round(mYogaNode.getLayoutHeight())); - } - private void createLayout(int widthMeasureSpec, int heightMeasureSpec) { final int widthSize = MeasureSpec.getSize(widthMeasureSpec); final int heightSize = MeasureSpec.getSize(heightMeasureSpec); @@ -318,12 +359,6 @@ private void createLayout(int widthMeasureSpec, int heightMeasureSpec) { mYogaNode.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED); } - @Override - protected LayoutParams generateDefaultLayoutParams() { - return new LayoutParams(LayoutParams.MATCH_PARENT, - LayoutParams.MATCH_PARENT); - } - /** * Wrapper around measure function for yoga leaves. */ @@ -374,36 +409,4 @@ private int viewMeasureSpecFromYogaMeasureMode(YogaMeasureMode mode) { } } } - - public void setHeightAutoAndDirtyAllViews() { - addOnLayoutChangeListener(new OnLayoutChangeListener() { - @Override - public void onLayoutChange(View view, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { - mYogaNode.setHeightAuto(); - ((YogaNodeJNIBase) mYogaNode).dirtyAllDescendants(); - } - }); - } - - public void setWidthAutoAndDirtyAllViews() { - addOnLayoutChangeListener(new OnLayoutChangeListener() { - @Override - public void onLayoutChange(View view, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { - mYogaNode.setWidthAuto(); - ((YogaNodeJNIBase) mYogaNode).dirtyAllDescendants(); - } - }); - } - - - public void setWidthAndHeightAutoAndDirtyAllViews() { - addOnLayoutChangeListener(new OnLayoutChangeListener() { - @Override - public void onLayoutChange(View view, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { - mYogaNode.setWidthAuto(); - mYogaNode.setHeightAuto(); - ((YogaNodeJNIBase) mYogaNode).dirtyAllDescendants(); - } - }); - } } diff --git a/android/beagle/src/main/java/br/com/zup/beagle/android/view/custom/BeagleFlexView.kt b/android/beagle/src/main/java/br/com/zup/beagle/android/view/custom/BeagleFlexView.kt index 9049d85c1c..9440e8f885 100644 --- a/android/beagle/src/main/java/br/com/zup/beagle/android/view/custom/BeagleFlexView.kt +++ b/android/beagle/src/main/java/br/com/zup/beagle/android/view/custom/BeagleFlexView.kt @@ -17,85 +17,81 @@ package br.com.zup.beagle.android.view.custom import android.annotation.SuppressLint -import android.view.ContextThemeWrapper import android.view.View -import br.com.zup.beagle.android.engine.mapper.FlexMapper -import br.com.zup.beagle.android.engine.renderer.ViewRendererFactory -import br.com.zup.beagle.android.utils.GenerateIdManager -import br.com.zup.beagle.android.utils.generateViewModelInstance -import br.com.zup.beagle.android.view.YogaLayout -import br.com.zup.beagle.android.view.viewmodel.ScreenContextViewModel import br.com.zup.beagle.android.widget.RootView -import br.com.zup.beagle.core.GhostComponent import br.com.zup.beagle.core.ServerDrivenComponent import br.com.zup.beagle.core.Style -import br.com.zup.beagle.core.StyleComponent -import com.facebook.yoga.YogaNode -import com.facebook.yoga.YogaNodeJNIBase -@Suppress("LeakingThis", "LongParameterList") +/** + * The Beagle Flex View is a view group that support style options + * + * @param rootView holder the reference of current context. + * @param style class will enable a few visual options to be changed. + * + */ @SuppressLint("ViewConstructor") -internal open class BeagleFlexView( - private val rootView: RootView, - style: Style, - private val flexMapper: FlexMapper = FlexMapper(), - private val viewRendererFactory: ViewRendererFactory = ViewRendererFactory(), - private val viewModel: ScreenContextViewModel = rootView.generateViewModelInstance(), - private val generateIdManager: GenerateIdManager = GenerateIdManager(rootView), +class BeagleFlexView private constructor( + rootView: RootView, + style: Style = Style(), styleId: Int = 0, -) : YogaLayout(if (styleId == 0) rootView.getContext() else ContextThemeWrapper(rootView.getContext(), styleId), - flexMapper.makeYogaNode(style)) { - - init { - observeStyleChanges(style, this, yogaNode) - } +) : InternalBeagleFlexView( + rootView = rootView, + style = style, + styleId = styleId, +) { - constructor(rootView: RootView, flexMapper: FlexMapper = FlexMapper()) : this(rootView, Style(), flexMapper) + companion object { - var listenerOnViewDetachedFromWindow: (() -> Unit)? = null + operator fun invoke( + rootView: RootView, + style: Style = Style(), + styleId: Int = 0, + ) = BeagleFlexView( + rootView = rootView, + style = style, + styleId = styleId, + ) + } - fun addView(child: View, style: Style) { - addViewWithBind(style, child, this) + /** + * Adds a child view with the specified style. + * @property child view will be added + * @property style style will be applied in this child. + */ + fun addView( + child: View, + style: Style, + ) { + addViewWithStyle(child, style) } - fun addServerDrivenComponent( - serverDrivenComponent: ServerDrivenComponent, + /** + * Adds a list of components in view. + * @property components views will be added + * @property addLayoutChangeListener force recalculate layout when view change, prefer use true always + */ + fun addView( + components: List, addLayoutChangeListener: Boolean = true, ) { - val component = if (serverDrivenComponent is GhostComponent) { - serverDrivenComponent.child - } else { - serverDrivenComponent + components.forEach { + addView(it, addLayoutChangeListener) } - generateIdManager.manageId(component, this) - - val style = (component as? StyleComponent)?.style ?: Style() - val view = viewRendererFactory.make(serverDrivenComponent).build(rootView) - if (addLayoutChangeListener) { - view.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ -> - (yogaNode as YogaNodeJNIBase).dirtyAllDescendants() - } - } - addViewWithBind(style, view, view) - } - - private fun addViewWithBind(style: Style, child: View, viewBind: View) { - val childYogaNode = flexMapper.makeYogaNode(style) - observeStyleChanges(style, viewBind, childYogaNode) - super.addView(child, childYogaNode) } - private fun observeStyleChanges(style: Style, view: View, yogaNode: YogaNode) { - flexMapper.observeBindChangesFlex(style, rootView, view, yogaNode) - } - - override fun onAttachedToWindow() { - super.onAttachedToWindow() - viewModel.linkBindingToContextAndEvaluateThem(this) + /** + * Adds a component in view. + * @property component view will be added + * @property addLayoutChangeListener force recalculate layout when view change, prefer use true always + */ + fun addView( + component: ServerDrivenComponent, + addLayoutChangeListener: Boolean = true, + ) { + addServerDrivenComponent(component, addLayoutChangeListener) } - override fun onDetachedFromWindow() { - super.onDetachedFromWindow() - listenerOnViewDetachedFromWindow?.invoke() + fun addListenerOnViewDetachedFromWindow(listener: (() -> Unit)) { + listenerOnViewDetachedFromWindow = listener } -} +} \ No newline at end of file diff --git a/android/beagle/src/main/java/br/com/zup/beagle/android/view/custom/BeagleView.kt b/android/beagle/src/main/java/br/com/zup/beagle/android/view/custom/BeagleView.kt index a5d2c4b17d..5dd7bc800a 100644 --- a/android/beagle/src/main/java/br/com/zup/beagle/android/view/custom/BeagleView.kt +++ b/android/beagle/src/main/java/br/com/zup/beagle/android/view/custom/BeagleView.kt @@ -51,7 +51,7 @@ sealed class BeagleViewState { internal class BeagleView( private val rootView: RootView, private val viewModel: BeagleViewModel = rootView.generateViewModelInstance(), -) : BeagleFlexView(rootView) { +) : InternalBeagleFlexView(rootView) { @Deprecated("It was deprecated in version 1.2.0 and will be removed in a future version." + " Use serverStateChangedListener instead.") diff --git a/android/beagle/src/main/java/br/com/zup/beagle/android/view/custom/InternalBeagleFlexView.kt b/android/beagle/src/main/java/br/com/zup/beagle/android/view/custom/InternalBeagleFlexView.kt new file mode 100644 index 0000000000..9de6343d28 --- /dev/null +++ b/android/beagle/src/main/java/br/com/zup/beagle/android/view/custom/InternalBeagleFlexView.kt @@ -0,0 +1,99 @@ +/* + * Copyright 2020 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package br.com.zup.beagle.android.view.custom + +import android.annotation.SuppressLint +import android.view.ContextThemeWrapper +import android.view.View +import br.com.zup.beagle.android.engine.mapper.FlexMapper +import br.com.zup.beagle.android.engine.renderer.ViewRendererFactory +import br.com.zup.beagle.android.utils.GenerateIdManager +import br.com.zup.beagle.android.utils.generateViewModelInstance +import br.com.zup.beagle.android.view.YogaLayout +import br.com.zup.beagle.android.view.viewmodel.ScreenContextViewModel +import br.com.zup.beagle.android.widget.RootView +import br.com.zup.beagle.core.GhostComponent +import br.com.zup.beagle.core.ServerDrivenComponent +import br.com.zup.beagle.core.Style +import br.com.zup.beagle.core.StyleComponent +import com.facebook.yoga.YogaNode +import com.facebook.yoga.YogaNodeJNIBase + +@Suppress("LeakingThis", "LongParameterList") +@SuppressLint("ViewConstructor") +open class InternalBeagleFlexView internal constructor( + private val rootView: RootView, + style: Style = Style(), + private val flexMapper: FlexMapper = FlexMapper(), + private val viewRendererFactory: ViewRendererFactory = ViewRendererFactory(), + private val viewModel: ScreenContextViewModel = rootView.generateViewModelInstance(), + private val generateIdManager: GenerateIdManager = GenerateIdManager(rootView), + styleId: Int = 0, +) : YogaLayout(if (styleId == 0) rootView.getContext() else ContextThemeWrapper(rootView.getContext(), styleId), + flexMapper.makeYogaNode(style)) { + + init { + observeStyleChanges(style, this, yogaNode) + } + + internal var listenerOnViewDetachedFromWindow: (() -> Unit)? = null + + internal fun addViewWithStyle(child: View, style: Style) { + addViewWithBind(style, child, this) + } + + internal fun addServerDrivenComponent( + serverDrivenComponent: ServerDrivenComponent, + addLayoutChangeListener: Boolean = true, + ) { + val component = if (serverDrivenComponent is GhostComponent) { + serverDrivenComponent.child + } else { + serverDrivenComponent + } + generateIdManager.manageId(component, this) + + val style = (component as? StyleComponent)?.style ?: Style() + val view = viewRendererFactory.make(serverDrivenComponent).build(rootView) + if (addLayoutChangeListener) { + view.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ -> + (yogaNode as YogaNodeJNIBase).dirtyAllDescendants() + } + } + addViewWithBind(style, view, view) + } + + private fun addViewWithBind(style: Style, child: View, viewBind: View) { + val childYogaNode = flexMapper.makeYogaNode(style) + observeStyleChanges(style, viewBind, childYogaNode) + super.addView(child, childYogaNode) + } + + private fun observeStyleChanges(style: Style, view: View, yogaNode: YogaNode) { + flexMapper.observeBindChangesFlex(style, rootView, view, yogaNode) + } + + override fun onAttachedToWindow() { + super.onAttachedToWindow() + viewModel.linkBindingToContextAndEvaluateThem(this) + } + + override fun onDetachedFromWindow() { + super.onDetachedFromWindow() + listenerOnViewDetachedFromWindow?.invoke() + } +} diff --git a/android/beagle/src/main/java/br/com/zup/beagle/android/widget/RootView.kt b/android/beagle/src/main/java/br/com/zup/beagle/android/widget/RootView.kt index 67f2da142a..feb08f1127 100644 --- a/android/beagle/src/main/java/br/com/zup/beagle/android/widget/RootView.kt +++ b/android/beagle/src/main/java/br/com/zup/beagle/android/widget/RootView.kt @@ -17,6 +17,8 @@ package br.com.zup.beagle.android.widget import android.content.Context +import androidx.appcompat.app.AppCompatActivity +import androidx.fragment.app.Fragment import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.ViewModelStoreOwner @@ -49,3 +51,50 @@ interface RootView { */ fun getScreenId(): String } + +/** + * The FragmentRootView holder the reference of a fragment. + * + * @param fragment that is the parent of a view. + * @param parentId parent view id. + */ +class FragmentRootView( + val fragment: Fragment, + private val parentId: Int, + private val screenId : String +) : RootView { + + override fun getContext(): Context = fragment.requireContext() + + override fun getLifecycleOwner(): LifecycleOwner = fragment.viewLifecycleOwner + + override fun getViewModelStoreOwner(): ViewModelStoreOwner = fragment + + override fun getParentId(): Int = parentId + + override fun getScreenId(): String = screenId +} + +/** + * The ActivityRootView holder the reference of activity. + * + * @param activity that is the parent of a view. + * @param parentId parent view id. + */ +class ActivityRootView( + val activity: AppCompatActivity, + private val parentId: Int, + private val screenId : String +) : RootView { + + override fun getContext(): Context = activity + + override fun getLifecycleOwner(): LifecycleOwner = activity + + override fun getViewModelStoreOwner(): ViewModelStoreOwner = activity + + override fun getParentId(): Int = parentId + + override fun getScreenId(): String = screenId +} + diff --git a/android/beagle/src/test/java/br/com/zup/beagle/android/BaseTest.kt b/android/beagle/src/test/java/br/com/zup/beagle/android/BaseTest.kt index 2f5a1eaef0..80629111e8 100644 --- a/android/beagle/src/test/java/br/com/zup/beagle/android/BaseTest.kt +++ b/android/beagle/src/test/java/br/com/zup/beagle/android/BaseTest.kt @@ -18,9 +18,9 @@ package br.com.zup.beagle.android import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider -import br.com.zup.beagle.android.engine.renderer.ActivityRootView import br.com.zup.beagle.android.setup.BeagleEnvironment import br.com.zup.beagle.android.setup.BeagleSdk +import br.com.zup.beagle.android.widget.ActivityRootView import io.mockk.MockKAnnotations import io.mockk.every import io.mockk.mockk diff --git a/android/beagle/src/test/java/br/com/zup/beagle/android/action/AddChildrenTest.kt b/android/beagle/src/test/java/br/com/zup/beagle/android/action/AddChildrenTest.kt index e771786092..96975c279b 100644 --- a/android/beagle/src/test/java/br/com/zup/beagle/android/action/AddChildrenTest.kt +++ b/android/beagle/src/test/java/br/com/zup/beagle/android/action/AddChildrenTest.kt @@ -21,7 +21,6 @@ import android.view.ViewGroup import androidx.appcompat.app.AppCompatActivity import br.com.zup.beagle.android.setup.BeagleEnvironment import br.com.zup.beagle.android.setup.BeagleSdk -import br.com.zup.beagle.android.utils.toView import br.com.zup.beagle.android.utils.viewFactory import br.com.zup.beagle.android.view.ViewFactory import br.com.zup.beagle.android.view.custom.BeagleFlexView diff --git a/android/beagle/src/test/java/br/com/zup/beagle/android/action/SetContextTest.kt b/android/beagle/src/test/java/br/com/zup/beagle/android/action/SetContextTest.kt index 9896f9ede3..26109f9cae 100644 --- a/android/beagle/src/test/java/br/com/zup/beagle/android/action/SetContextTest.kt +++ b/android/beagle/src/test/java/br/com/zup/beagle/android/action/SetContextTest.kt @@ -18,11 +18,11 @@ package br.com.zup.beagle.android.action import android.view.View import androidx.lifecycle.ViewModelProvider -import br.com.zup.beagle.android.engine.renderer.ActivityRootView import br.com.zup.beagle.android.logger.BeagleLoggerProxy import br.com.zup.beagle.android.testutil.RandomData import br.com.zup.beagle.android.utils.evaluateExpression import br.com.zup.beagle.android.view.viewmodel.ScreenContextViewModel +import br.com.zup.beagle.android.widget.ActivityRootView import io.mockk.Runs import io.mockk.every import io.mockk.just diff --git a/android/beagle/src/test/java/br/com/zup/beagle/android/components/ListViewTest.kt b/android/beagle/src/test/java/br/com/zup/beagle/android/components/ListViewTest.kt index 131e8c1c85..c16217e764 100644 --- a/android/beagle/src/test/java/br/com/zup/beagle/android/components/ListViewTest.kt +++ b/android/beagle/src/test/java/br/com/zup/beagle/android/components/ListViewTest.kt @@ -26,6 +26,7 @@ import br.com.zup.beagle.android.context.ContextData import br.com.zup.beagle.android.context.expressionOf import br.com.zup.beagle.android.testutil.InstantExecutorExtension import br.com.zup.beagle.android.view.ViewFactory +import br.com.zup.beagle.core.ServerDrivenComponent import br.com.zup.beagle.widget.core.ListDirection import io.mockk.Runs import io.mockk.every @@ -78,7 +79,7 @@ class ListViewTest : BaseComponentTest() { deprecatedListView = ListView(children, ListDirection.VERTICAL) listView = ListView(ListDirection.VERTICAL, context, onInit, dataSource, template, onScrollEnd, iteratorName = iteratorName, key = key) - every { beagleFlexView.addView(any()) } just Runs + every { beagleFlexView.addView(any()) } just Runs every { anyConstructed().makeRecyclerView(rootView.getContext()) } returns recyclerView every { anyConstructed().makeBeagleRecyclerView(rootView.getContext()) } returns beagleRecyclerView every { anyConstructed().makeBeagleRecyclerViewScrollIndicatorVertical(rootView.getContext()) } returns beagleRecyclerView diff --git a/android/beagle/src/test/java/br/com/zup/beagle/android/components/TabViewTest.kt b/android/beagle/src/test/java/br/com/zup/beagle/android/components/TabViewTest.kt index 46693f0380..cd060e8aa2 100644 --- a/android/beagle/src/test/java/br/com/zup/beagle/android/components/TabViewTest.kt +++ b/android/beagle/src/test/java/br/com/zup/beagle/android/components/TabViewTest.kt @@ -24,6 +24,7 @@ import br.com.zup.beagle.android.utils.StyleManager import br.com.zup.beagle.android.view.ViewFactory import br.com.zup.beagle.android.view.custom.BeaglePageView import br.com.zup.beagle.android.view.custom.BeagleTabLayout +import br.com.zup.beagle.core.ServerDrivenComponent import io.mockk.every import io.mockk.just import io.mockk.mockk @@ -65,7 +66,7 @@ class TabViewTest : BaseComponentTest() { every { viewPager.adapter = any() } just runs every { viewPager.addOnPageChangeListener(any()) } just runs - every { beagleFlexView.addView(any()) } just runs + every { beagleFlexView.addView(any()) } just runs tabView = TabView(listOf(tabItem), DEFAULT_STYLE) diff --git a/android/beagle/src/test/java/br/com/zup/beagle/android/components/form/SimpleFormTest.kt b/android/beagle/src/test/java/br/com/zup/beagle/android/components/form/SimpleFormTest.kt index 1537adc53f..4c32e2b876 100644 --- a/android/beagle/src/test/java/br/com/zup/beagle/android/components/form/SimpleFormTest.kt +++ b/android/beagle/src/test/java/br/com/zup/beagle/android/components/form/SimpleFormTest.kt @@ -75,7 +75,7 @@ internal class SimpleFormTest : BaseComponentTest() { simpleForm.buildView(rootView) // Then - verify(exactly = once()) { beagleFlexView.addServerDrivenComponent(children[0]) } + verify(exactly = once()) { beagleFlexView.addView(children) } } } diff --git a/android/beagle/src/test/java/br/com/zup/beagle/android/components/layout/ComposeComponentTest.kt b/android/beagle/src/test/java/br/com/zup/beagle/android/components/layout/ComposeComponentTest.kt index f2f761f7e2..22d78bb9d9 100644 --- a/android/beagle/src/test/java/br/com/zup/beagle/android/components/layout/ComposeComponentTest.kt +++ b/android/beagle/src/test/java/br/com/zup/beagle/android/components/layout/ComposeComponentTest.kt @@ -64,6 +64,6 @@ class ComposeComponentTest : BaseComponentTest() { composeComponent.buildView(rootView) // THEN - verify(exactly = once()) { beagleFlexView.addServerDrivenComponent(child) } + verify(exactly = once()) { beagleFlexView.addView(child) } } } diff --git a/android/beagle/src/test/java/br/com/zup/beagle/android/components/layout/ContainerTest.kt b/android/beagle/src/test/java/br/com/zup/beagle/android/components/layout/ContainerTest.kt index 533c4fa7ba..d9f46eaa3c 100644 --- a/android/beagle/src/test/java/br/com/zup/beagle/android/components/layout/ContainerTest.kt +++ b/android/beagle/src/test/java/br/com/zup/beagle/android/components/layout/ContainerTest.kt @@ -93,7 +93,7 @@ class ContainerTest : BaseComponentTest() { container.buildView(rootView) // Then - verify(exactly = once()) { beagleFlexView.addServerDrivenComponent(containerChildren[0]) } + verify(exactly = once()) { beagleFlexView.addView(containerChildren) } } } diff --git a/android/beagle/src/test/java/br/com/zup/beagle/android/components/layout/ScreenComponentTest.kt b/android/beagle/src/test/java/br/com/zup/beagle/android/components/layout/ScreenComponentTest.kt index febbf67897..4c1024ea03 100644 --- a/android/beagle/src/test/java/br/com/zup/beagle/android/components/layout/ScreenComponentTest.kt +++ b/android/beagle/src/test/java/br/com/zup/beagle/android/components/layout/ScreenComponentTest.kt @@ -105,7 +105,7 @@ class ScreenComponentTest : BaseComponentTest() { screenComponent.buildView(rootView) // Then - verify(atLeast = once()) { beagleFlexView.addServerDrivenComponent(component) } + verify(atLeast = once()) { beagleFlexView.addView(component) } } @Test diff --git a/android/beagle/src/test/java/br/com/zup/beagle/android/components/layout/ScrollViewTest.kt b/android/beagle/src/test/java/br/com/zup/beagle/android/components/layout/ScrollViewTest.kt index ef569f7d6d..1ec69ecc50 100644 --- a/android/beagle/src/test/java/br/com/zup/beagle/android/components/layout/ScrollViewTest.kt +++ b/android/beagle/src/test/java/br/com/zup/beagle/android/components/layout/ScrollViewTest.kt @@ -58,7 +58,7 @@ class ScrollViewTest : BaseComponentTest() { every { scrollView.addView(any()) } just Runs every { horizontalScrollView.addView(any()) } just Runs every { anyConstructed().makeBeagleFlexView(any(), capture(style)) } returns beagleFlexView - every { beagleFlexView.addServerDrivenComponent(any(), false) } just Runs + every { beagleFlexView.addView(any(), false) } just Runs every { beagleFlexView.context } returns context every { anyConstructed().makeScrollView(any()) } returns scrollView every { anyConstructed().makeHorizontalScrollView(any()) } returns horizontalScrollView @@ -77,7 +77,7 @@ class ScrollViewTest : BaseComponentTest() { // Then verify { anyConstructed().makeBeagleFlexView(rootView, style.first()) - beagleFlexView.addServerDrivenComponent(components[0], false) + beagleFlexView.addView(components, false) beagleFlexView.setHeightAutoAndDirtyAllViews() } verify(exactly = once()) { anyConstructed().makeScrollView(context) } @@ -106,7 +106,7 @@ class ScrollViewTest : BaseComponentTest() { } verify(exactly = once()) { anyConstructed().makeHorizontalScrollView(context) - beagleFlexView.addServerDrivenComponent(components[0], false) + beagleFlexView.addView(components, false) beagleFlexView.setWidthAndHeightAutoAndDirtyAllViews() } assertEquals(false, scrollBarEnabled.captured) diff --git a/android/beagle/src/test/java/br/com/zup/beagle/android/components/list/ListAdapterTest.kt b/android/beagle/src/test/java/br/com/zup/beagle/android/components/list/ListAdapterTest.kt index 7e9762ae46..3371af1015 100644 --- a/android/beagle/src/test/java/br/com/zup/beagle/android/components/list/ListAdapterTest.kt +++ b/android/beagle/src/test/java/br/com/zup/beagle/android/components/list/ListAdapterTest.kt @@ -49,7 +49,6 @@ import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith - @RunWith(AndroidJUnit4::class) class ListAdapterTest : BaseTest() { diff --git a/android/beagle/src/test/java/br/com/zup/beagle/android/components/page/PageViewTest.kt b/android/beagle/src/test/java/br/com/zup/beagle/android/components/page/PageViewTest.kt index 2f71623475..99cb4f95a0 100644 --- a/android/beagle/src/test/java/br/com/zup/beagle/android/components/page/PageViewTest.kt +++ b/android/beagle/src/test/java/br/com/zup/beagle/android/components/page/PageViewTest.kt @@ -16,6 +16,7 @@ package br.com.zup.beagle.android.components.page +import android.view.View import androidx.viewpager.widget.ViewPager import br.com.zup.beagle.android.action.Action import br.com.zup.beagle.android.components.BaseComponentTest @@ -26,7 +27,7 @@ import br.com.zup.beagle.core.ServerDrivenComponent import br.com.zup.beagle.android.extensions.once import br.com.zup.beagle.android.utils.handleEvent import br.com.zup.beagle.android.view.ViewFactory -import br.com.zup.beagle.android.view.custom.BeagleFlexView +import br.com.zup.beagle.android.view.custom.InternalBeagleFlexView import br.com.zup.beagle.android.view.custom.BeaglePageView import br.com.zup.beagle.core.Style import io.mockk.Runs @@ -101,7 +102,7 @@ class PageViewTest : BaseComponentTest() { pageView.buildView(rootView) // THEN - verify(exactly = once()) { beagleFlexView.addView(any()) } + verify(exactly = once()) { beagleFlexView.addView(any()) } } @Test @@ -126,7 +127,7 @@ class PageViewTest : BaseComponentTest() { @Test fun buildView_should_return_page_view_two_when_current_page_is_not_null() { // GIVEN - val mockedView: BeagleFlexView = mockk() + val mockedView: InternalBeagleFlexView = mockk() mockkConstructor(PageViewTwo::class) every { anyConstructed().buildView(any()) } returns mockedView pageView = PageView(children, context, actions, currentPage) diff --git a/android/beagle/src/test/java/br/com/zup/beagle/android/components/utils/ViewExtensionsKtTest.kt b/android/beagle/src/test/java/br/com/zup/beagle/android/components/utils/ViewExtensionsKtTest.kt index 5601925fa4..7196e3e42f 100644 --- a/android/beagle/src/test/java/br/com/zup/beagle/android/components/utils/ViewExtensionsKtTest.kt +++ b/android/beagle/src/test/java/br/com/zup/beagle/android/components/utils/ViewExtensionsKtTest.kt @@ -26,8 +26,6 @@ import androidx.appcompat.app.AppCompatActivity import androidx.core.widget.TextViewCompat import androidx.fragment.app.Fragment import br.com.zup.beagle.android.BaseTest -import br.com.zup.beagle.android.engine.renderer.ActivityRootView -import br.com.zup.beagle.android.engine.renderer.FragmentRootView import br.com.zup.beagle.android.extensions.once import br.com.zup.beagle.android.networking.RequestData import br.com.zup.beagle.android.setup.DesignSystem @@ -46,6 +44,8 @@ import br.com.zup.beagle.android.view.custom.OnStateChanged import br.com.zup.beagle.android.view.mapper.toRequestData import br.com.zup.beagle.android.view.viewmodel.GenerateIdViewModel import br.com.zup.beagle.android.view.viewmodel.ScreenContextViewModel +import br.com.zup.beagle.android.widget.ActivityRootView +import br.com.zup.beagle.android.widget.FragmentRootView import br.com.zup.beagle.android.widget.RootView import br.com.zup.beagle.core.Style import br.com.zup.beagle.core.StyleComponent diff --git a/android/beagle/src/test/java/br/com/zup/beagle/android/context/ContextComponentHandlerTest.kt b/android/beagle/src/test/java/br/com/zup/beagle/android/context/ContextComponentHandlerTest.kt index d5fc19b9b9..76fceac700 100644 --- a/android/beagle/src/test/java/br/com/zup/beagle/android/context/ContextComponentHandlerTest.kt +++ b/android/beagle/src/test/java/br/com/zup/beagle/android/context/ContextComponentHandlerTest.kt @@ -19,8 +19,8 @@ package br.com.zup.beagle.android.context import android.view.View import androidx.lifecycle.ViewModelProvider import br.com.zup.beagle.android.components.layout.Container -import br.com.zup.beagle.android.engine.renderer.ActivityRootView import br.com.zup.beagle.android.view.viewmodel.ScreenContextViewModel +import br.com.zup.beagle.android.widget.ActivityRootView import io.mockk.Runs import io.mockk.every import io.mockk.just diff --git a/android/beagle/src/test/java/br/com/zup/beagle/android/utils/GenerateIdManagerTest.kt b/android/beagle/src/test/java/br/com/zup/beagle/android/utils/GenerateIdManagerTest.kt index 59c810b8d2..26da159512 100644 --- a/android/beagle/src/test/java/br/com/zup/beagle/android/utils/GenerateIdManagerTest.kt +++ b/android/beagle/src/test/java/br/com/zup/beagle/android/utils/GenerateIdManagerTest.kt @@ -20,20 +20,17 @@ import android.view.View import br.com.zup.beagle.android.BaseTest import br.com.zup.beagle.android.components.Text import br.com.zup.beagle.android.components.layout.Container -import br.com.zup.beagle.android.view.custom.BeagleFlexView +import br.com.zup.beagle.android.view.custom.InternalBeagleFlexView import br.com.zup.beagle.android.view.viewmodel.GenerateIdViewModel import br.com.zup.beagle.android.view.viewmodel.ListViewIdViewModel import br.com.zup.beagle.android.view.viewmodel.OnInitViewModel -import br.com.zup.beagle.android.widget.RootView import br.com.zup.beagle.core.ServerDrivenComponent import br.com.zup.beagle.ext.setId import io.mockk.every import io.mockk.mockk import io.mockk.mockkStatic -import io.mockk.unmockkStatic import io.mockk.verify import org.junit.Assert.assertEquals -import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Nested @@ -45,7 +42,7 @@ class GenerateIdManagerTest: BaseTest() { private val generateIdViewModel = mockk(relaxed = true) private val listViewIdViewModel = mockk(relaxed = true) private val onInitViewModel = mockk(relaxed = true) - private val view = mockk() + private val view = mockk() private val generatedId = 1 private lateinit var generateIdManager: GenerateIdManager diff --git a/android/beagle/src/test/java/br/com/zup/beagle/android/utils/StringExtensionsKtTest.kt b/android/beagle/src/test/java/br/com/zup/beagle/android/utils/StringExtensionsKtTest.kt index 12ee07212f..76f39e76b8 100644 --- a/android/beagle/src/test/java/br/com/zup/beagle/android/utils/StringExtensionsKtTest.kt +++ b/android/beagle/src/test/java/br/com/zup/beagle/android/utils/StringExtensionsKtTest.kt @@ -17,18 +17,7 @@ package br.com.zup.beagle.android.utils import android.graphics.Color -import androidx.appcompat.app.AppCompatActivity -import androidx.lifecycle.ViewModelProvider -import br.com.zup.beagle.android.BaseTest -import br.com.zup.beagle.android.context.ContextDataManager -import br.com.zup.beagle.android.engine.renderer.ActivityRootView -import br.com.zup.beagle.android.setup.BeagleEnvironment -import br.com.zup.beagle.android.view.viewmodel.ScreenContextViewModel -import io.mockk.Runs import io.mockk.every -import io.mockk.just -import io.mockk.mockk -import io.mockk.mockkObject import io.mockk.mockkStatic import io.mockk.slot import io.mockk.unmockkAll diff --git a/android/beagle/src/test/java/br/com/zup/beagle/android/utils/ViewGroupExtensionsKtTest.kt b/android/beagle/src/test/java/br/com/zup/beagle/android/utils/ViewGroupExtensionsKtTest.kt index ece2650fbe..b6bdf6c3d5 100644 --- a/android/beagle/src/test/java/br/com/zup/beagle/android/utils/ViewGroupExtensionsKtTest.kt +++ b/android/beagle/src/test/java/br/com/zup/beagle/android/utils/ViewGroupExtensionsKtTest.kt @@ -21,12 +21,12 @@ import androidx.appcompat.app.AppCompatActivity import androidx.fragment.app.Fragment import br.com.zup.beagle.android.BaseTest import br.com.zup.beagle.android.components.utils.viewExtensionsViewFactory -import br.com.zup.beagle.android.engine.renderer.ActivityRootView -import br.com.zup.beagle.android.engine.renderer.FragmentRootView import br.com.zup.beagle.android.networking.RequestData import br.com.zup.beagle.android.view.ViewFactory import br.com.zup.beagle.android.view.custom.BeagleView import br.com.zup.beagle.android.view.custom.OnServerStateChanged +import br.com.zup.beagle.android.widget.ActivityRootView +import br.com.zup.beagle.android.widget.FragmentRootView import io.mockk.every import io.mockk.mockk import io.mockk.verifySequence diff --git a/android/beagle/src/test/java/br/com/zup/beagle/android/utils/WidgetExtensionsKtTest.kt b/android/beagle/src/test/java/br/com/zup/beagle/android/utils/WidgetExtensionsKtTest.kt index bd7d2d8ab2..feb54222cc 100644 --- a/android/beagle/src/test/java/br/com/zup/beagle/android/utils/WidgetExtensionsKtTest.kt +++ b/android/beagle/src/test/java/br/com/zup/beagle/android/utils/WidgetExtensionsKtTest.kt @@ -113,8 +113,8 @@ class WidgetExtensionsKtTest : BaseTest() { verifySequence { generateIdViewModel.createIfNotExisting(0) beagleFlexView.id = 0 - beagleFlexView.addServerDrivenComponent(component) - beagleFlexView.listenerOnViewDetachedFromWindow = any() + beagleFlexView.addView(component) + beagleFlexView.addListenerOnViewDetachedFromWindow(any()) } assertEquals(beagleFlexView, actual) diff --git a/android/beagle/src/test/java/br/com/zup/beagle/android/view/custom/BeagleFlexViewTest.kt b/android/beagle/src/test/java/br/com/zup/beagle/android/view/custom/BeagleFlexViewTest.kt index 72b1a2befd..210a4434b7 100644 --- a/android/beagle/src/test/java/br/com/zup/beagle/android/view/custom/BeagleFlexViewTest.kt +++ b/android/beagle/src/test/java/br/com/zup/beagle/android/view/custom/BeagleFlexViewTest.kt @@ -17,12 +17,7 @@ package br.com.zup.beagle.android.view.custom import android.view.View -import br.com.zup.beagle.android.engine.mapper.FlexMapper -import br.com.zup.beagle.android.engine.renderer.ViewRenderer -import br.com.zup.beagle.android.engine.renderer.ViewRendererFactory -import br.com.zup.beagle.android.utils.GenerateIdManager -import br.com.zup.beagle.android.view.viewmodel.ScreenContextViewModel -import br.com.zup.beagle.android.widget.RootView +import br.com.zup.beagle.android.BaseTest import br.com.zup.beagle.core.ServerDrivenComponent import br.com.zup.beagle.core.Style import com.facebook.yoga.YogaNode @@ -30,123 +25,135 @@ import io.mockk.Runs import io.mockk.every import io.mockk.just import io.mockk.mockk +import io.mockk.mockkStatic import io.mockk.spyk import io.mockk.verify -import io.mockk.verifySequence +import io.mockk.verifyOrder +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test -class BeagleFlexViewTest { - - private val rootViewMock = mockk(relaxed = true, relaxUnitFun = true) - private val flexMapperMock = mockk(relaxUnitFun = true, relaxed = true) - private val styleMock = mockk