Samples / Learn / Timer

Timer

A ProgressIndicator during a long backend call, driven by follow-up actions rather than by a frozen screen.

Class
z2ui5_cl_smp_app_064
Repository
abap2UI5/samples
UI5 libraries
sap.m, sap.ui.core, sap.ui.layout
Lowest UI5 release
1.71
In the browser
Runs in the playground
Controls it builds
sap.m.Button, sap.m.MessageStrip, sap.m.Page, sap.m.ProgressIndicator, sap.m.Shell, sap.m.VBox, sap.ui.core.mvc.View, sap.ui.layout.VerticalLayout
Documentation
cookbook/browser_interaction/timer ↗

The class

" @keywords progressindicator busy wait long running backend start_timer
" @summary A ProgressIndicator during a long backend call, driven by follow-up actions rather than by a frozen screen.
" @docs https://abap2ui5.github.io/docs/cookbook/browser_interaction/timer
CLASS z2ui5_cl_smp_app_064 DEFINITION PUBLIC.

  PUBLIC SECTION.
    INTERFACES z2ui5_if_app.

    DATA mv_check_enabled TYPE abap_bool.
    DATA mv_percent TYPE i.

  PROTECTED SECTION.
    DATA mv_check_active TYPE abap_bool.
    DATA client          TYPE REF TO z2ui5_if_client.

    METHODS on_event.
    METHODS view_display.

  PRIVATE SECTION.
ENDCLASS.


CLASS z2ui5_cl_smp_app_064 IMPLEMENTATION.

  METHOD z2ui5_if_app~main.

    me->client = client.
    IF client->check_on_init( ).

      mv_check_enabled = abap_true.
      view_display( ).

    ELSEIF client->check_on_navigated( ).
      view_display( ).
    ELSEIF client->check_on_event( ).
      on_event( ).
    ENDIF.

  ENDMETHOD.


  METHOD on_event.

    IF client->check_on_event( `LOAD` ).

      " a press starts the bar over, a timer tick carries it on
      IF mv_check_active = abap_false.
        mv_percent = 0.
      ENDIF.

      " the long-running backend step this bar reports on
      WAIT UP TO 2 SECONDS.
      mv_percent = mv_percent + 25.

      IF mv_percent < 100.

        mv_check_active  = abap_true.
        mv_check_enabled = abap_false.
        client->follow_up_action(
            val   = z2ui5_if_client=>cs_event-start_timer
            t_arg = VALUE #( ( `LOAD` ) ( `0` ) ) ).

      ELSE.

        mv_check_active  = abap_false.
        mv_check_enabled = abap_true.
        client->message_toast_display( `loaded` ).
      ENDIF.

    ENDIF.

  ENDMETHOD.


  METHOD view_display.

    DATA(view)             = z2ui5_cl_ui5_view_builder=>factory(
        )->ele( n = `View` ns = `mvc`
            )->a( n = `displayBlock` v = `true`
            )->a( n = `height`       v = `100%`
            )->a( n = `xmlns`        v = `sap.m`
            )->a( n = `xmlns:mvc`    v = `sap.ui.core.mvc`
            )->a( n = `xmlns:layout` v = `sap.ui.layout` ).

    DATA(page)          = view->ele( `Shell`
        )->ele( `Page`
            )->a( n = `title`          v = `abap2UI5 - Timer - Progress Indicator during a Backend Call`
            )->a( n = `showNavButton`  b = client->check_app_prev_stack( )
            )->a( n = `navButtonPress` v = client->_event_nav_app_leave( )
            )->a( n = `class`          v = `sapUiContentPadding`
            )->a( n = `id`             v = `page_main` ).

    page->tag( `MessageStrip`
        )->a( n = `text`     v = `A ProgressIndicator driven from the backend: pressing Load runs a WAIT-delayed server ` &&
                   `step and re-arms a client timer (follow_up_action), advancing the bar in 25% steps until it stands at 100%.`
        )->a( n = `type`     v = `Information`
        )->a( n = `showIcon` b = abap_true
        )->a( n = `class`    v = `sapUiSmallMargin` ).

    DATA(layout) = page->ele( n = `VerticalLayout` ns = `layout`
        )->a( n = `class` v = `sapUiContentPadding`
        )->a( n = `width` v = `100%` ).
    layout->ele( `VBox`
        )->tag( `ProgressIndicator`
            )->a( n = `percentValue` v = client->_bind( mv_percent )
            )->a( n = `displayValue` v = |\{{ client->_bind( val = mv_percent path = abap_true ) }\} %|
            )->a( n = `showValue`    b = abap_true
            )->a( n = `state`        v = `Success` ).

    layout->tag( `Button`
        )->a( n = `press`   v = client->_event( `LOAD` )
        )->a( n = `text`    v = `Load`
        )->a( n = `enabled` v = client->_bind( mv_check_enabled ) ).

    client->view_display( view->stringify( ) ).

  ENDMETHOD.

ENDCLASS.