Pass Globals byRef to Class?

NFOBoy

Active Member
Licensed User
Longtime User
So, I am trying to make a multi-language Preference Manager Class.

Personally, I like Preferences to be declared in the Process_Globals of an Activity module I call ... Params.

I want to streamline my ability to build this Activity by making multi-language class types and have interaction be similiar to the Preference Manager functionality that Erel and Corwin42 provide, (not as dynamic right now as Corwin's!).

Basic idea is that there is a imgView that I am using as a Flag to show current Language selected. It "pulses" to show other available languages available for the user to select from.

When the user touches the flag, a scrollview appears with the available flags to choose from.

When chosen, I want the Params.prefUserLanguage to be updated (and this will also be trigger point to iterate thru the preference page views to change their text)

However, unless I hard code "params.prefUserLanguage = userChoice", I can't get the global to update. (Code is below, but I've tried the trick of passing as an array)

As I want to be able to build each view (including the flag), and have them change their associated Global Variable, this is starting to become a wall.

Before I go to much further down the rat hole... any ideas?

Thanks,

Ross

Here's the code that calls from Preferences Activity module
B4X:
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim imgFlag As cmlImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("Layout1")
   
   'first setup the Flag for user choice
   Dim prefLang(1) As String
   prefLang(0) = Params.prefUserLanguage
   
   imgFlag.Initialize(prefLang)
   imgFlag.addLanguageMap( "flagenglish.jpg", "EN")
   imgFlag.addLanguageMap( "flagjapan.png", "JN")
   imgFlag.addLanguageMap( "flagthailand.png", "TH")
'   imgFlag.addLanguageMap ("flagthailand.png", "EH")
'   imgFlag.addLanguageMap( "flagenglish.jpg", "dN")
'   imgFlag.addLanguageMap( "flagjapan.png", "Jd")
'   imgFlag.addLanguageMap( "flagthailand.png", "dH")
'   imgFlag.addLanguageMap ("flagthailand.png", "Ed")
   
   Dim width As Float
   Dim height As Float
   
   If Activity.width > Activity.height Then
      width = 10%x
      height = 3/4 * width
      imgFlag.AddView(Activity, 100%x - width-1dip, (100%y - height)/2, width, height)
   Else
      height = 10%y
      width = 4/3 * height
      imgFlag.AddView(Activity, (100%x - width)/2, 100%y - height - 1dip, width, height)
   End If

   'now go thru and add each preference
   
   
   
End Sub

Sub Activity_Resume
   imgFlag.StartRotate
End Sub

Here's the class module for the Flag
B4X:
'Class module
Sub Class_Globals
   Private m_imageView As ImageView
   Private m_mapImages As Map
   Private  prefLanguage(1) As String
   Private tmrSwitch As Timer
   
   Private currentFlag As Int 'to track which Flag is Chosen
   Private showingFlag As Int 'to track which flag is being shown during "heartbeat"
   Private Flags As Int 'to track how many flags there are
   
   Private m_width, m_height As Float
   
   Private m_top, m_left As Float
   
   Private m_Activity As Activity
   
   Private hsvFlags As HorizontalScrollView 'for flag display
   Private svFlags As ScrollView
   
   
   
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(langpref() As String)
'need to initialize the imageview

m_imageView.Initialize("m_imageView")
m_mapImages.Initialize
prefLanguage = langpref
tmrSwitch.Initialize("tmrSwitch", 2000)


End Sub


Sub m_imageView_click
'need to add code here to do scroll views of all the flags/languages

tmrSwitch.Enabled = False 'turn off timer
m_imageView.Visible = False 'hide the picking flag

If m_Activity.Width > m_Activity.Height Then
   svFlags.Initialize(m_height)
   m_Activity.AddView(svFlags, m_left, m_top, m_width, m_height)
   
   'need to add in each flag for a scroll view
   For i = 0 To m_mapImages.Size-1
      Dim newimageView As ImageView
      newimageView.Initialize("imgFlag")
      newimageView.Tag = i
      newimageView.Gravity = Gravity.FILL
      newimageView.Bitmap = LoadBitmapSample(File.DirAssets, m_mapImages.GetValueAt(i), m_width, m_height)
      svFlags.Panel.AddView(newimageView, 0 ,  5dip + i*m_height + i*5dip, m_width, m_height)
   Next
   svFlags.Panel.Height =  5dip + i*m_height + i*5dip
   
   If svFlags.Panel.Height > m_Activity.Height Then
      svFlags.Height = m_Activity.Height
      svFlags.Top = 0
   Else
      'ok so the total height is less than activity height... make it just tall enough
      svFlags.Height =  5dip + i*m_height + i*5dip
      'now center it over the original flag
      'take the total height and divide by 2
      'take the height of startpoint of the original and add half height to find point to subtract from
      Dim referencePoint As Float
      referencePoint = m_top + m_height/2
      
      Dim subtractAmount As Float
      subtractAmount = svFlags.Height/2
      
      svFlags.Top = referencePoint - subtractAmount
      
      'now handle if out of sight
      
      If svFlags.Top < 0 Then
         svFlags.Top = 0
      Else If svFlags.Top + svFlags.Height > m_Activity.Height Then
         svFlags.Top = m_Activity.Height - svFlags.Height
      End If
      
   End If
Else
   hsvFlags.Initialize(m_Activity.Width, "svFlags")
   m_Activity.AddView(hsvFlags, 0, m_top, m_Activity.Width, m_height)

   'need to add in each flag

   For i = 0 To m_mapImages.Size-1
      Dim newimageView As ImageView
      newimageView.Initialize("imgFlag")
      newimageView.Tag = i
      newimageView.Gravity = Gravity.FILL
      newimageView.Bitmap = LoadBitmapSample(File.DirAssets, m_mapImages.GetValueAt(i), m_width, m_height)
      hsvFlags.Panel.AddView(newimageView, 5dip + i * m_width + i*5dip, 0, m_width, m_height)
   Next
   hsvFlags.Panel.Width = 5dip + i * m_width + i*5dip
   
   If hsvFlags.Panel.Width > m_Activity.Width Then
      hsvFlags.Width = m_Activity.Width
         hsvFlags.Left = 0
   Else
      'so total width is less than activity width.. make it just wide enough
      hsvFlags.Width = 5dip + i * m_width + i*5dip
      'now center it over the original flag
      
      Dim referencePoint As Float
      referencePoint = m_left + m_width/2
      
      Dim subtractAmount As Float
      subtractAmount = hsvFlags.Width/2
      
      hsvFlags.Left = referencePoint - subtractAmount
      'now handle out of sight
      
      If hsvFlags.Left < 0 Then
         hsvFlags.Left = 0
      Else If hsvFlags.Left + hsvFlags.Width > m_Activity.Width Then
         hsvFlags.Left = m_Activity.Width - hsvFlags.Width
      End If
         
      
   End If
   
   
End If



End Sub


Public Sub AddView(someActivity As Activity, left As Float, top As Float, width As Float, height As Float)
   If someActivity.width > someActivity.height Then
      m_width = 10%x
      m_height = 3/4 * m_width
      someActivity.AddView(m_imageView, left, top, width, height)
   Else
      m_height = 10%y
      m_width = 4/3 * m_height
      someActivity.AddView(m_imageView,  left, top, width, height)
   End If
   
   m_top = m_imageView.top
   m_left = m_imageView.left
   
   m_imageView.Gravity = Gravity.FILL
   
   'now put the image based on current preference
   If m_mapImages.ContainsKey(prefLanguage(0)) Then
      'Msgbox("map exists", "yes")
      m_imageView.Bitmap = LoadBitmapSample(File.DirAssets, m_mapImages.Get(prefLanguage(0)), m_width, m_height)
      currentFlag = DetermineFlagIndex(prefLanguage(0))
      showingFlag = currentFlag
      m_imageView.Tag = currentFlag
   Else If m_mapImages.Size > 0 Then
      'load the first image available
      m_imageView.Bitmap = LoadBitmapSample(File.DirAssets, m_mapImages.GetValueAt(0), m_width, m_height)
      currentFlag = 0
      showingFlag = 0
      m_imageView.Tag = currentFlag
   Else
      Msgbox("no image available for multi-languageImageView", "nothing available")
   End If
   
   
   m_Activity = someActivity
End Sub

Public Sub addLanguageMap(filename As String, language As String)
'adds a reference to a map image and the associated string for that language
m_mapImages.Put(language, filename)
Flags = Flags + 1

End Sub

Public Sub StartRotate
tmrSwitch.Enabled = True
End Sub


Public Sub StopRotate
tmrSwitch.Enabled = False
End Sub


Private Sub tmrSwitch_tick
'this will go in to each available language Flag, and show it for a heartbeat

'first see what flag is displayed

tmrSwitch.Enabled = False

If m_imageView.Tag = currentFlag Then 'this means switch to smaller flag
   'increment the showingflag
   showingFlag = showingFlag + 1
   If showingFlag = currentFlag Then
      showingFlag = showingFlag+1
   End If
   If showingFlag > m_mapImages.Size-1 Then
      showingFlag = 0
   End If
   'change background
   m_imageView.Height = .8 * m_height
   m_imageView.Width = .8 * m_width
   m_imageView.Left = m_imageView.Left + .1*m_width
   m_imageView.Top = m_imageView.Top + .1*m_height
   
   m_imageView.Bitmap = LoadBitmapSample(File.DirAssets, m_mapImages.GetValueAt(showingFlag), m_width*.8, m_height*.8)
   m_imageView.Tag = showingFlag
   tmrSwitch.Interval = 500
Else 'this means go back to bigger/current flag

   m_imageView.Bitmap = LoadBitmapSample(File.DirAssets, m_mapImages.GetValueAt(currentFlag), m_width, m_height)
   m_imageView.Left = m_left
   m_imageView.Top = m_top
   m_imageView.Width = m_width
   m_imageView.Height = m_height
   m_imageView.Tag = currentFlag
   tmrSwitch.Interval = 2000
End If

tmrSwitch.Enabled = True

End Sub

Private Sub DetermineFlagIndex(someKey As String) As Int
For i = 0 To m_mapImages.Size - 1
   If m_mapImages.GetKeyAt(i) = someKey Then
      Return i
   End If
Next
   Return -1 'no key.. something wrong.. return a -1
End Sub

Private Sub imgFlag_click As String
   Dim imgFlag As ImageView
   imgFlag = Sender
   currentFlag = imgFlag.Tag
   showingFlag = currentFlag
   m_imageView.Bitmap = LoadBitmapSample(File.DirAssets, m_mapImages.GetValueAt(currentFlag), m_width, m_height)
   
   If hsvFlags.IsInitialized Then
      hsvFlags.RemoveView
   Else 
      svFlags.RemoveView
   End If
   
   m_imageView.Visible = True
   prefLanguage(0) = m_mapImages.GetKeyAt(currentFlag)
   
   Msgbox (Params.prefUserLanguage, "here")
   tmrSwitch.Enabled = True
End Sub
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…