11package org .pwss .controller ;
22
3+ import java .util .ArrayList ;
4+ import java .util .Collections ;
5+ import java .util .List ;
36import org .pwss .controller .util .NavigationContext ;
47import org .pwss .view .screen .BaseScreen ;
58
@@ -20,10 +23,15 @@ public abstract class BaseController<Screen extends BaseScreen> {
2023 * The navigation context for passing data between different parts of the application during navigation.
2124 */
2225 private NavigationContext context ;
26+ /**
27+ * List of SubControllers managed by this BaseController.
28+ */
29+ private final List <SubController <?, ? extends BaseController <?>>> subControllers ;
2330 /**
2431 * Logger instance for logging purposes.
2532 */
2633 private final org .slf4j .Logger log ;
34+
2735 /**
2836 * Constructs a `BaseController` with the specified view.
2937 * Initializes the view and sets up event listeners.
@@ -33,6 +41,7 @@ public abstract class BaseController<Screen extends BaseScreen> {
3341 public BaseController (Screen screen ) {
3442 this .screen = screen ;
3543 this .log = org .slf4j .LoggerFactory .getLogger (BaseController .class );
44+ this .subControllers = new ArrayList <>();
3645 // Run onCreate lifecycle method
3746 onCreate ();
3847 // Initialize event listeners
@@ -54,6 +63,19 @@ protected NavigationContext getContext() {
5463 public void setContext (NavigationContext context ) {
5564 this .context = context ;
5665 }
66+ /**
67+ * Adds a subcontroller to this controller.
68+ *
69+ * @param subController The subcontroller to add.
70+ */
71+ public void addSubController (SubController <?, ? extends BaseController <?>> subController ) {
72+ if (subController == null ) {
73+ log .warn ("Attempted to add a null subcontroller to {}" , getClass ().getSimpleName ());
74+ return ;
75+ }
76+ subControllers .add (subController );
77+ log .debug ("Added SubController: {} to {}" , subController .getClass ().getSimpleName (), getClass ().getSimpleName ());
78+ }
5779 /**
5880 * Abstract method to initialize event listeners for the Screen.
5981 * Subclasses must provide an implementation for this method.
@@ -70,8 +92,8 @@ public void setContext(NavigationContext context) {
7092 */
7193 public void reloadData () {
7294 log .debug ("reloadData called for {}" , screen .getScreenName ());
95+ subControllers .forEach (SubController ::reloadData );
7396 }
74-
7597 /**
7698 * Retrieves the view managed by this controller.
7799 *
@@ -86,13 +108,12 @@ public Screen getScreen() {
86108 */
87109 public void onShow () {
88110 log .debug ("onShow called for {}" , screen .getScreenName ());
111+ subControllers .forEach (SubController ::onShow );
89112 }
90113
91114 /**
92- * Method called when the view is created.
93- * Subclasses can override this method to perform actions during the creation of the view .
115+ * Method called when the controller is created.
116+ * Subclasses can override this method to perform setup logic .
94117 */
95- public void onCreate () {
96- log .debug ("onCreate called for {}" , screen .getScreenName ());
97- }
118+ abstract void onCreate ();
98119}
0 commit comments