0

so I've checked everywhere and it's so hard to figure this one out. I'm writing an app in RubyMotion using RedPotion but for the most part the help I need here is with Ruby. The app is pretty much a book app so I'm trying to figure out how to properly store text. Basically, I have a set amount of characters per line and only 17 lines I can use on the screen. Once that fills up, I want the rest of the text to be stored for use with the same method to appear the next set onscreen when the page flips. Then if the user swipes back, for that text to go back as well. I've tried arrays, hashes. Different methods. Been going crazy for about 3 weeks on this one problem. Anyone can help with a ruby method or tweak mine to work?

def on_load
 @texts = [
            "In the beginning of God's creation of the heavens and the           earth.\nNow the earth was
            astonishingly empty, and darkness was on the face of the deep, and the spirit of God was
            hovering over the face of the water.\nAnd God said, 'Let there be light,' and there was light.
            \nAnd God saw the light that it was good, and God separated between the light and between the darkness.
            \nAnd God called the light day, and the darkness He called night, and it was evening and it was morning,
            one day.\nAnd God said, 'Let there be an expanse in the midst of the water, and let it be a separation
            between water and water.'"
          ]


  @recycle = [ @texts[ 0 ] ]

  @page_idx = 0


  @header = append!( UIImageView, :header )
  @text_view = append!( UITextView, :text_view )
  text_view( @texts[ 0 ] )

   @text_view.on(:swipe_right) do |sender|
     recycle_text_forward( @recycle[ -1 ] )
   end

   @text_view.on(:swipe_left) do |sender|
     recycle_text_back( @recycle[ @page_idx ] )
   end
end

def text_view( text )
   page_words = text.split( ' ' )

   number_of_lines_idx = 17
   each_line_chars = 27
   chars = ""
   setter = ""
   idx = 0
   all_idx = page_words.length - 1

   @text_view.text = ""

   while number_of_lines_idx != 0
     until chars.length >= each_line_chars
     break if page_words[ idx ] == nil
     chars << page_words[ idx ] + " "
     chars << "\n" if chars.length >= each_line_chars
     idx += 1
   end

   break if page_words[ idx ] == nil

   number_of_lines_idx -= 1

   if number_of_lines_idx == 0
     @recycle << page_words[ idx..all_idx ]
     @page_idx = @recycle.length - 2
   end

   setter = chars
   chars = ""
   @text_view.text += setter
  end
end

def recycle_text_forward( text )
 text_view( text.join( ' ' ) )
end

def recycle_text_back( text )
 text_view( text )
end

1 Answers1

1

I am not sure I understood the question properly, but here is what I could suggest:

input = "In the beginning of God's creation..."

_, lines = input.split(/\s+/).each_with_object(["", []]) do |word, (line, lines)|
  if line.length + word.length + 1 <= 27
    line << " " << word
  else
    lines << line.dup
    line.replace(word)
  end
end #⇒ Here we got an array of lines, now split by screen size:

lines.each_slice(17).map { |*lines| lines.join(' ').strip }
#⇒ ["In the beginning...", "and it was evening and"]

I believe, this would be a good start for further tweaking.

Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
  • Wow! Thank you so much for everything. I can't tell you how much I appreciate your answer and so quickly too! This helps a lot especially with reformatting. The only thing left is for me to store the data. Those 17 lines is only part of the full amount of text. So I need to be able to store the rest of the text for when the user swipes so I can get the next 17 lines. But then when the user swipes right, it needs to also go back to the previous 17 lines. I hope I am making a little bit more sense. – Elan Kvitko Mar 13 '17 at 16:08
  • It’s already stored in the result of last line. It produces an array of _screens_. Try it out yourself. – Aleksei Matiushkin Mar 13 '17 at 16:40