How do you develop Roku Apps using the Stack Methodology?

Table of Contents

  • What is Roku?
  • What is Stack Methodology?
  • Advantages
  • Disadvantages
  • Stack Implementation
  • Stack utilization
  • Sample Channel discussion and execution
  • References

What is Roku? 

Roku is a brand and platform known for its streaming media players and smart TVs. It was developed by Roku, Inc., an American company founded in 2002. Roku devices are designed to provide easy access to a wide range of streaming content, including movies, TV shows, music, and more, via various streaming services and apps.

Roku devices typically connect to your television and provide a user-friendly interface that allows you to browse different streaming services. Users can choose and install apps for services like Netflix, Hulu, Amazon Prime Video, Disney+, and many others, depending on their geographic location and availability. Roku devices also often include features like voice search, customizable home screens, and the ability to cast content from mobile devices to the TV.

In addition to its hardware products, Roku has developed the Roku Channel, a free, ad-supported streaming service that offers a selection of movies, TV shows, and other content.

What is Stack & Stack Methodology?

Stack

A basic linear data structure, the stack is employed for data storage, adhering to the Last In First Out (LIFO) principle, wherein the last-inserted element is the first to be removed. To illustrate, envision a stack of plates where the latest plate placed is on top; when removing a plate, the one at the top is taken first. Implementation is feasible through arrays or linked lists. Key operations include push(), pop(), top(), isEmpty(), size(), and more.

Stack Methodology

The approach in which nodes are upheld in a stack is called Stack Methodology. In Roku, we append all children in a group and maintain a nodes stack to play around it.

Advantages of Stack Methodology

Eliminate code duplicity:

Stack methodology eliminates the code duplicity that happens for node display and close operations.

Easy implementation: 

Implementing the stack data structure is straightforward with arrays or linked lists, and its operations are easy to comprehend and execute.

Efficient memory utilization: 

The stack employs a contiguous memory block, enhancing its memory utilization efficiency compared to other data structures.

Fast access time: 

The stack data structure facilitates swift access time for adding and removing elements, as these operations occur at the top of the stack.

Supports backtracking: 

The stack data structure is conducive to supporting backtracking algorithms employed in problem-solving. These algorithms explore all possible solutions by preserving previous states.

Helps in function calls: 

Stack data structure is used to store function calls and their states, which helps in the efficient implementation of recursive function calls.

 

Disadvantage of Stack Methodology

Limited capacity: 

The stack data structure is constrained by a fixed capacity, capable of holding only a predetermined number of elements. When the stack reaches its full capacity, attempting to add new elements may trigger a stack overflow, potentially resulting in data loss.

No random access: 

The stack data structure does not support random access to its elements; rather, it exclusively permits the addition and removal of elements from the top of the stack. Accessing an element in the middle necessitates the removal of all the elements above it in the stack.

Memory management: 

The stack data structure relies on a contiguous block of memory, and frequent addition and removal of elements can lead to memory fragmentation.

Not suitable for certain applications: 

The stack data structure is ill-suited for applications that demand access to elements in the middle of the stack, such as searching or sorting algorithms.

 

Stack Implementation

‘Initialize Screen Stack

sub initScreenStack()

    m.screens = m.top.findNode("screens")

    m.screenStack = []

end sub

‘Create Object of the new Screen

function createScreen(screenName as string, id) as object

    m[screenName] = CreateObject("roSGNode", id)

    m[screenName].id = id

    m[screenName].name = screenName

    showScreen(m.[screenName])

    return m[screenName]

end function

‘Display new screen on top of Scene.

sub showScreen(node as object)

    m.screens.AppendChild(node) ' add new screen to scene

    ' show new screen

    m.screenStack.Push(node) ' add new screen to the screen stack

end sub

‘Close the current screen and make visible the previous screen

sub closeScreen()

    if m.screenStack.Count() > 1

        cur = m.screenStack[m.screens.getChildren(m.screens.getChildCount(), 0).Count() - 1]

        m.screens.removeChildIndex(m.screens.getChildren(m.screens.getChildCount(), 0).Count() - 1)

        m.screenStack.Pop() ' remove screen from screenStack

        m[cur.name] = invalid

        ' take previous screen and make it visible

        prev = m.screenStack[m.screens.getChildren(m.screens.getChildCount(), 0).Count() - 1]

        if prev <> invalid

            prev.visible = true

            prev.setFocus = true

        end if

    else

        m.screens.removeChildIndex(m.screens.getChildren(m.screens.getChildCount(), 0).Count() - 1)

    end if

    screenObj = m.screenStack[m.screens.getChildren(3000, 0).Count() - 1]

    if screenObj <> invalid

        screenObj.setFocus = true

    else

        getMainScene().setFocus = true

    end if

end sub
sub hideScreen()

    prev = m.screenStack[m.screens.getChildren(m.screens.getChildCount(), 0).Count() - 1]

    if prev <> invalid

        prev.visible = false

    end if
end sub

‘Add new screen to Scene and stack

sub addScreen(node as object)

    m.screens.AppendChild(node) ' add new screen to scene

    m.screenStack.Push(node) ' add new screen to the screen stack

end sub

‘Remove screen from stack and hide it.

sub clearScreenStack()

    if m.screenStack.Count() > 1

        while m.screenStack.Count() > 1

            last = m.screenStack.Pop() ' remove screen from screenStack

            if last.visible = true

                last.visible = false ' hide screen

            end if

            m.screens.RemoveChild(last)

        end while

    else

        m.screenStack.Peek().visible = false ' take current screen from screen stack but don't delete it

    end if

end sub

'Get the current screen but do not delete it from stack
function getCurrentScreen()

    return m.screenStack.Peek()

end function

'Check if screen is available in Stack
function isScreenInScreenStack(node as object) as boolean

    ' check if screen stack contains specified node

    for each screen in m.screenStack

        result = screen.IsSameNode(node)

        if result = true

            return true

        end if

    end for

    return false

end function


Utilization of Stack

MainScene.brs
‘Initialize the Application components

sub init()

‘Initialize Application main components

End sub
sub launchScreenA()

    hideScreen()

    createScreen("ScreenA", "ScreenA")

end sub


sub launchScreenB()

    hideScreen()

    createScreen("ScreenB", "ScreenB")

end sub

‘Back event handling

sub onKeyEvent(key as string, press as boolean) as boolean

result = false

    if press then

        if key = getRemoteEvents().back

            if m.screenStack.Count() > 1

                closeScreen()

                //displayOrCloseAnyScreen(true)

                result = true

   end if

end if

     end if 

end sub

Practical Implementation

  1. Sample code explanation
  2. Code execution
  3. Q&A

Sample Code Link: 

https://github.com/ersps25/StackMethodology-Roku

References

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *