Macroid-Viewable

65 views
Skip to first unread message

Nick Stanchenko

unread,
Jul 31, 2014, 6:41:35 PM7/31/14
to mac...@googlegroups.com, scala-on...@googlegroups.com
Hi,

Here’s a new library I wrote under the Macroid umbrella project: Macroid-Viewable: http://macroid.github.io/related/Viewable.html
It provides three main advantages:
  • A clear way to declare how your data should be displayed
  • Boilerplate-free ListAdapters and PagerAdapters
  • Macroid’s trademark emphasis on composability
A quick example of displaying Alice and Bob in a list (with font size proportional to their age):

import macroid._
import macroid.FullDsl._
import macroid.viewable._

// our data type
case class User(name: String, age: Int)

// defines how to view a User in a list
implicit def userListable(implicit ctx: ActivityContext, appCtx: AppContext) =
 
Listable[User].tw {
 
  // the layout is a TextView
    w[TextView]
 
} { user
   
// to display a user, we tweak the layout
    text
(user.name) + TextTweaks.size(user.age + 10)
 
}

// now we simply tweak the ListView
myListView
<~ List(User("Alice", 12), User("Bob", 23)).listAdapterTweak

Please follow the link above for more documentation and cool features.
Also note that I’ve just submitted the library to the jCenter repo, but until it arrives there, you might need to use the following resolver:

resolvers += "stanch@Bintray" at "http://dl.bintray.com/stanch/maven/"

Looking forward to hearing your feedback,
Nick

Nick Stanchenko

unread,
Nov 30, 2014, 6:25:07 PM11/30/14
to scala-on...@googlegroups.com, mac...@googlegroups.com
Hi,

The way I understand tweaks is that they are mutable effects to layouts/views.

That’s correct, although I would rather say “mutating effects”, as tweaks themselves are immutable.
 
In this case, is a ListAdapterTweak also a method to modify the display of the ListAdapeter? If so, where can I see what it actually does and how can I compose them?

listAdapterTweak returns a Tweak[AbsListView], which you can apply to any AbsListView:

// create a tweak
val tweak
= userListable.listAdapterTweak(User("Terry"), User("Graham"))
// myListView is a ListView slot
var myListView = slot[ListView]

// wire myListView to something...

runUi
{
 
// apply the tweak
  myListView
<~ tweak
}

If you just want the adapter, there is also a listAdapter method, which returns it:

val adapter = userListable.listAdapter(User("Terry"), User("Graham"))
runUi
{
  myListView
<~ macroid.contrib.ListTweaks.adapter(adapter)
}

The second example is actually how listAdapterTweak is implemented internally :) https://github.com/macroid/macroid-viewable/blob/master/src/main/scala/macroid/viewable/Listable.scala#L168

Hope this helps,
Nick

Nick Stanchenko

unread,
Dec 2, 2014, 3:51:58 AM12/2/14
to scala-on...@googlegroups.com, mac...@googlegroups.com
Yes, that tweak sets the list adapter.

Nick

On Tuesday, December 2, 2014 3:35:53 AM UTC, Stanislav Palatnik wrote:
Ah, so applying a Tweak[AbsListView] would be the same as wiring a ListableListAdapter?
Reply all
Reply to author
Forward
0 new messages