Fork me on GitHub

Friday, August 14, 2015

Scaloid 4.0 is released

Today I release Scaloid 4.0, which includes lots of improvements.

Incompatible changes

Scaloid 4.0 is best with Android API Level 16, while still supports Level 10

Scaloid 4.0 distribution is compiled with Android API Level 16, still retaining compatibility with Level 10. Accessor methods and deprecation warning is based on API Level 16, so you have to carefully check the availability of API if you target lower version of Android. In our experience on building an App for Gingerbread, currently we found no obstruction that prevents building a Scaloid app for older devices.

To compile with Scaloid 4.0, you have to specify build time Android API in project.properties file as android-16 or higher.

LocalServiceConnection

Usual use case of LocalServiceConnection is:
val service = new LocalServiceConnection[MyService]
//...
service {
  myService => // do something with myService
}
The implementation of apply() is changed to:
def apply[T](f: S => Unit): Unit = service.fold(onConnected(f))(f)
The block will be executed later if the service is not connected yet. Formally it was just did nothing when the service is not connected yet. This behavior now becomes ifAvailable. Then all the code using apply(...) should be changed to ifAvailable(...).

SArrayAdapter

Constructor parameter of SArrayAdapter is now java.util.List.

spinnerDialog

spinnerDialog returns Future[ProgressDialog].

End of incompatible changes


More convenient press-and-hold action

We provided the press-and-hold action callback from the last release. The sample code below increases the number on the TextView in every 100 milliseconds:
val num = STextView("0")
SButton("Increase").onPressAndHold(100, num.text = (num.text.toString.toInt + 1).toString)
The press-and-holds has separate callback from that of clicks. Often the behavior for a click and press-and-hold are the same. In this case, we have to assign it individually:
val num = STextView("0")
SButton("Increase", num.text = (num.text.toString.toInt + 1).toString)
.onPressAndHold(100, num.text = (num.text.toString.toInt + 1).toString)
This kind of repetition is not looks good. From Scaloid 4.0, we introduce a shorter representation:
val num = STextView("0")
SButton("Increase", num.text = (num.text.toString.toInt + 1).toString, 100)

Cleaner and type-safe way to access SharedPreference

SharedPreference can be accessed in this way:
val executionCount = preferenceVar(0) // default value 0
val ec = executionCount() // read
executionCount() = ec + 1 // write
executionCount.remove() // remove
Refer to this post for more details:
http://blog.scaloid.org/2015/07/type-safe-sharedpreference.html

`here` method

To attach a view widget to a layout, we have two major way to do it. One is using apply method of companion object of views:
new SLinearLayout {
  SButton("Hello") textColor Color.RED
}
The other is creating object with new keyword, and assign it to the layout using += method:
lazy val button = new SButton("Hello")

onCreate {
  contentView = new SLinearLayout {
    this += button
    button textColor Color.RED
  }
}
In Scaloid 4.0, we have another choice:
lazy val button = new SButton("Hello")

onCreate {
  contentView = new SLinearLayout {
    button.here textColor Color.RED
  }
}
As you see, this += is abbreviated with .here. Because it returns SButton, method chaining is available.

Oftentimes, style is applied to widgets, and sometimes we want avoid it for some widget. We provide hereWithoutStyle method:
lazy val button = new SButton("Hello") textColor Color.RED
lazy val redBtn = new SButton("Hello") textColor Color.RED

contentView = new SLinearLayout {
  style {
    case b: Button => b textColor Color.BLUE
  }
  button.here // blue
  redBtn.hereWithoutStyle // red
}
here method can also be applied to layouts:
contentView = new SVerticalLayout {
  new SLinearLayout {
    SButton("1")
    SButton("2")
  }.here
  SButton("3")
}

Thursday, August 6, 2015

StateListDrawable DSL for cleaner button styles.

XML is wordy and not programmable. One of the major goal of Scaloid is to replace XMLs with Scala code, to be more concise, type-safe and programmable. Scaloid 4.0-RC2 provides a new way to build StateListDrawable without XML. For example, given the XML drawable selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item   android:state_checked="true"
            android:drawable="@drawable/drumset_on"/>
    <item   android:state_checked="false"
            android:drawable="@drawable/drumset_off"/>
</selector>

This can be written with SStateListDrawable as shown here:
new SStateListDrawable {
  +=(R.drawable.drumset_off, -SELECTED)
  +=(R.drawable.drumset_on, SELECTED)
}
It is definitely much cleaner and programmable. For example, we can write a program that returns a single-colored drawable for buttons:
def c(color: Int) = new ColorDrawable(color)

def btn(normal:Int, pressed:Int) = new SStateListDrawable {
  +=(c(pressed), PRESSED)
  +=(c(Color.LTGRAY), -ENABLED)
  +=(c(normal))
}
We pass normal color and pressed color in an Int type color code. We can further think that pressed color can be automatically determined by a normal color; It is really easy to implement:
def btn(normal:Int): SStateListDrawable = btn(normal, pressedColor(normal))
SStateListDrawable is included in Scaloid library, and the implementation of btn and pressedColor implemented in org.scaloid.util.Styles, which is included in Scaloid-util package.

The implementation is quite straightforward, take a look at the code, and you can easily have a conception about this idea:

Source of SStateListDrawable (Scaloid package)
Source of Styles (Scaloid-util package)