reddit is fun · themes


Lua API

Theme scripting reference

Exported functions

Lua scripts for ListViews will generally implement a newView and bindView function.

newView(Builder)
Create a View hierarchy for the list item.
See Builder.
bindView(Holder, Thing, ListItem)
Bind info to the View hierarchy for the list item.
See Holder.
See Thing.
See ListItem.

manifest.json

You can change the script filename, and newView and bindView function names in the manifest.json file.

manifest_json["scripts"][SCRIPT_NAME] is the JSON path to the script JSON object in the manifest.

script["file"]
The filename. Default filename varies and will be indicated elsewhere in this guide.
script["new_view"]
The newView function name. Defaults to "newView".
script["bind_view"]
The bindView function name. Defaults to "bindView".

Hint Change these properties to combine scripts into fewer files, or to reuse a script for different screens.


Script files

posts_thread
Default manifest_json["scripts"]["posts_thread"]["file"] = "thread_item.lua"
Type ListView
Represents a reddit link.
posts_comment
Default manifest_json["scripts"]["posts_comment"]["file"] = "profile_comment_item.lua"
Type ListView
Represents a saved reddit comment.

onClick callbacks

TODO


Script files

comments_thread
Default manifest_json["scripts"]["comments_thread"]["file"] = "thread_op.lua"
Type ListView
Represents the OP (original post), a reddit link.
comments_comment
Default manifest_json["scripts"]["comments_comment"]["file"] = "comment_item.lua"
Type ListView
Represents a comment.

onClick callbacks

TODO


Script files

profile_thread
Default manifest_json["scripts"]["profile_thread"]["file"] = "profile_thread_item.lua"
Type ListView
Represents a reddit link.
profile_comment
Default manifest_json["scripts"]["profile_comment"]["file"] = "profile_comment_item.lua"
Type ListView
Represents a reddit comment.
profile_header
Default manifest_json["scripts"]["profile_header"]["file"] = "profile_header.lua"
Type ListView header
Represents a reddit account.

onClick callbacks

TODO


Script files

inbox_message
Default manifest_json["scripts"]["inbox_message"]["file"] = "inbox_message_item.lua"
Type ListView
Represents a reddit message.
inbox_comment
Default manifest_json["scripts"]["inbox_comment"]["file"] = "inbox_comment_item.lua"
Type ListView
Represents an inbox comment.

onClick callbacks

TODO


The Builder object builds Views. It's generally provided as an argument to the newView function.

Builder:beginFrameLayout()
Builder:beginFrameLayout(idStringOrInteger)
Begins a FrameLayout with optional View id. Views created between here and Builder:endViewGroup() will be children of the FrameLayout.
idStringOrInteger can be used to refer to the FrameLayout later.
Note idStringOrInteger of 0 or nil is same as no id.
Returns FrameLayout
Builder:beginLinearLayout()
Builder:beginLinearLayout(idStringOrInteger)
Begins a LinearLayout with optional View id. Views created between here and Builder:endViewGroup() will be children of the LinearLayout.
View id can be used to refer to the LinearLayout later.
Returns LinearLayout
Builder:endViewGroup()
Ends the current ViewGroup (e.g., FrameLayout).
Builder:endFrameLayout()
Builder:endLinearLayout()
Synonyms for Builder:endViewGroup().
Builder:addView()
Builder:addView(idStringOrInteger)
Creates a new View and adds it to the current ViewGroup.
View can be useful for spacing. It's uncommonly used.
Returns View
Builder:addTextView()
Builder:addTextView(idStringOrInteger)
Creates a new TextView and adds it to the current ViewGroup.
Returns TextView
Builder:addImageView()
Builder:addImageView(idStringOrInteger)
Creates a new ImageView and adds it to the current ViewGroup.
Returns ImageView
Builder:addProgressBar()
Builder:addProgressBar(idStringOrInteger)
Creates a new ProgressBar and adds it to the current ViewGroup.
Returns ProgressBar
Builder:addProgressBarSmall()
Builder:addProgressBarSmall(idStringOrInteger)
Creates a new ProgressBar with style ?android:attr/progressBarStyleSmall and adds it to the current ViewGroup.
Returns ProgressBar
Builder:addButton()
Builder:addButton(idStringOrInteger)
Creates a new Button and adds it to the current ViewGroup.
Returns Button
Builder:addButtonSmall()
Builder:addButtonSmall(idStringOrInteger)
Creates a new Button with style ?android:attr/buttonStyleSmall and adds it to the current ViewGroup.
Returns Button
Builder:addImageButton()
Builder:addImageButton(idStringOrInteger)
Creates a new ImageButton and adds it to the current ViewGroup.
Returns ImageButton
Builder:addImageButtonSmall()
Builder:addImageButtonSmall(idStringOrInteger)
Creates a new ImageButton with style ?android:attr/buttonStyleSmall and adds it to the current ViewGroup.
Returns ImageButton

Holder is generally provided as an argument to the bindView function. It's used to gain access to the Views that the Builder created in newView.

Holder:getView(idStringOrInteger)
Returns View with this id.

Generally passed as an argument to bindView. See https://github.com/reddit/reddit/wiki/JSON

Thing, when accessed via the Lua API, is read-only.

Look You should look at the javadocs for Things for the supported properties.


ListItem is generally provided as an argument to the bindView function. It provides functionality surrounding the list item, in relation to the ListView and ListAdapter that contain it.

ListItem:toggleChecked()
Sets the (single) checked position to the current position, or unsets it if current position is already the checked position.
ListItem:isChecked()
Returns true or false depending if it's the currently-checked position.
ListItem:getTop()
Returns Integer, pixels from the top of the ListView on screen.
ListItem:setTop(pixels)
Moves the item pixels pixels from the top of the ListView.

These are wrappers for the Android View classes.

You create Views with Builder and access them later with Holder.

Look You should look at the javadocs for Views.


Note Global constants are all referenced by redditisfun.CONSTANTNAME, e.g., redditisfun.TEXT_SIZE_LARGE

TEXT_SIZE_LARGE
TEXT_SIZE_MEDIUM
TEXT_SIZE_SMALL
Integer, pixels.
Text sizes based on user's app settings.
TEXT_SIZE_LARGE is generally 4sp larger than TEXT_SIZE_MEDIUM.
TEXT_SIZE_SMALL is generally 4sp smaller than TEXT_SIZE_MEDIUM.
TEXT_COLOR_PRIMARY
TEXT_COLOR_SECONDARY
Integer, color.
?android:attr/textColorPrimary
?android:attr/textColorSecondary
ACTIONBAR_ITEM_BACKGROUND
Integer, resource id.
?android:attr/actionBarItemBackground

Note Global helper classes are referenced by redditisfun.ClassName, e.g., redditisfun.Fonts.

Fonts

Fonts:registerNormal(typefaceName, fontPath)
Fonts:registerBold(typefaceName, fontPath)
Fonts:registerItalic(typefaceName, fontPath)
Fonts:registerBoldItalic(typefaceName, fontPath)
Register font typefaces, which then become usable in TextView:setTypeface(typefaceName).
typefaceName is any identifier you want.
fontPath is the relative path to the font file in your theme folder.
Example Fonts:registerNormal("Roboto", "fonts/Roboto-Regular.ttf")
Example Fonts:registerBold("Roboto", "fonts/Roboto-Bold.ttf")
Example Fonts:registerItalic("Roboto", "fonts/Roboto-Italic.ttf")
Example Fonts:registerBoldItalic("Roboto", "fonts/Roboto-BoldItalic.ttf")

Spans

Spans:builder()
Returns An empty SpannableStringBuilder.
Spans:addColor(originalCharSequence, colorString)
Add a color span to the originalCharSequence which may be a regular String.
colorString is "#RRGGBB" or "#AARRGGBB" or color names.
All supported colorString formats from android.graphics.Color.parseColor().
Returns The CharSequence with new Span attached.
Spans:addBackgroundColor(originalCharSequence, colorString)
colorString is "#RRGGBB" or "#AARRGGBB" or color names.
Returns The CharSequence with new Span attached.
Spans:addStyle(originalCharSequence, styleName)
styleName is one of: "normal", "bold", "italic", "bold|italic"
Returns The CharSequence with new Span attached.
Spans:addSize(originalCharSequence, textSize)
textSize is either a String like "12sp", or a pixel integer value (not recommended) like 20.
Returns The CharSequence with new Span attached.

Toasts

Toasts:showToastShort(text)
Toasts:showToastLong(text)
Display a Toast with short or long duration.
Toasts:showHintShort(text, view)
Toasts:showHintLong(text, view)
Display a Toast located in proximity to the provided view on screen.
Hint This is useful in conjunction with View:setOnLongClick().
Example upvote_button:setOnLongClick(function(v) Toasts:showHintShort("Upvote the comment", v) end)