Code Monkey home page Code Monkey logo

quiz-macroid's People

Contributors

phatak-dev avatar

Watchers

 avatar  avatar  avatar

quiz-macroid's Issues

Improve readability

Hi,

I suggest you to further improve the readability of the code for it to be used as a sample project :)
For example, this is almost impossible to parse quickly:

val answerView = l[LinearLayout](
  w[Button] <~ text("Prev")
    <~ wrapContent <~ On.click {
      currentIndex = if (currentIndex > 0) currentIndex - 1
      else
         questions.length - 1
      questionView <~ text(questions(currentIndex).question)
    } <~ wire(prev),
  w[Button] <~ text("Next") <~ wire(next)
    <~ wrapContent <~ On.click {
      currentIndex = (currentIndex + 1) % questions.length
      mIsCheater = false
      questionView <~ text(questions(currentIndex).question)
    }) <~ wrapContent <~ (horizontal) <~ padding(all = 10 dp)

I would write it like this (note that you are not using the wires in any way, so I removed them):

def nav(direction: Int) = {
  currentIndex = (currentIndex + direction) % questions.length
  mIsCheater = false
  questionView <~ text(questions(currentIndex).question)
}

val answerView = l[HorizontalLinearLayout](
  w[Button] <~
    text("Prev") <~
    wrapContent <~
    On.click(nav(-1)),
  w[Button] <~
    text("Next") <~
    wrapContent <~
    On.click(nav(+1))
) <~
  wrapContent <~
  padding(all = 10 dp)

Another one:

val cheatButton = w[Button] <~ text("Cheat!") <~
  wrapContent <~ On.click {
    val intent =
      new android.content.Intent(QuizActivity.this, classOf[CheatActivity])
    val answer = questions(currentIndex).mTrue
    intent.putExtra(CheatActivity.EXTRA_ANSWER_IS_TRUE,
      answer)
    startActivityForResult(intent, 0)
    Ui(true)
      }

I would change to:

def cheat = {
  val intent = Intent(this, classOf[CheatActivity])
  val answer = questions(currentIndex).mTrue
  intent.putExtra(CheatActivity.EXTRA_ANSWER_IS_TRUE, answer)
  startActivityForResult(intent, 0)
}

val cheatButton = w[Button] <~
  text("Cheat!") <~
  wrapContent <~
  On.click(Ui { cheat; true })

A final example:

val landscapeLayout = l[FrameLayout](
  questionTextView <~ lp[FrameLayout](
    WRAP_CONTENT, WRAP_CONTENT, Gravity.
      CENTER_HORIZONTAL),
  answerView <~ lp[FrameLayout](
    WRAP_CONTENT, WRAP_CONTENT, Gravity.CENTER_VERTICAL
      | Gravity.CENTER_HORIZONTAL),
  cheatButton <~ lp[FrameLayout](
    WRAP_CONTENT, WRAP_CONTENT, Gravity.BOTTOM
      | Gravity.CENTER),
  prevNextLayout <~ lp[FrameLayout](
    WRAP_CONTENT, WRAP_CONTENT, Gravity.BOTTOM
      | Gravity.RIGHT)) <~ matchParent

I would tweak slightly:

def wrapContentGravity(gravity: Int) =
  lp[FrameLayout](WRAP_CONTENT, WRAP_CONTENT, gravity)

val landscapeLayout = l[FrameLayout](
  questionTextView <~ wrapContentGravity(Gravity.CENTER_HORIZONTAL),
  answerView <~ wrapContentGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL),
  cheatButton <~ wrapContentGravity(Gravity.BOTTOM | Gravity.CENTER),
  prevNextLayout <~ wrapContentGravity(Gravity.BOTTOM | Gravity.RIGHT)
) <~ matchParent

I hope you agree that readability makes a huge different, especially for those who want to use your code as a starting point.

Nick

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.