#Mirror Sandbox Mirror Sandbox is a companion library for the Java code hot-swapping feature in jimu Mirror. Mirror Sandbox classes are "design mode" code that allow you to build and preview animations and interactions piecewise -- think of it as a REPL for Android UI development.
Typical use cases for Mirror Sandbox include:
- Live-coding animations and interactions. Once satisfied, directly use the animation code in production.
- Experimenting and learning UI related Android APIs by executing code piecewise - just as what you can do with Ruby REPL.
- Populating views with mock data where Mirror's sample data XML doesn't have support yet:
myCustomView.setData(someDataModel)
See this blog post for more information.
#Download via Gradle:
compile 'com.jimulabs.mirrorsandbox:mirror-sandbox:0.2.1'
#Usage
Overview
Mirror Sandbox consists of two parts:
MirrorSandbox
interface that Mirror calls when previewing a screen- A set of simple utilities for generating mock data. See MockData class.
Note, the wrappers of Android's property animators have been moved to its own repo, motion-kit.
##Steps
Step 1: Extend MirrorSandboxBase
(or implement the interface MirrorSandbox
).
public class MySandbox extends MirrorSandboxBase {
public MySandbox(View view) { super(view); }
@Override
public void $onLayoutDone(View rootView) {
List<Album> albums = new ArrayList<>();
MockData md = new MockData();
for (int i = 0; i < 100; i++) {
Album album = new Album(md.imageUrl(ImageSize.small), md.personName(),
md.phrase(), md.paragraph());
albums.add(album);
}
albumList.setAdapter(new AlbumsAdapter(albums));
}
}
Optionally, you can add a static method $init(Context)
in the sandbox class. In this method, you can put initialization code that needs to run before views are created. For example, the code below enables the use of Stetho:
public class MySandbox extends MirrorSandboxBase {
@SuppressWarnings("unused")
// This method doesn't have to public
public static void $init(Context context) {
Stetho.initialize(
Stetho.newInitializerBuilder(context)
.enableDumpapp(Stetho.defaultDumperPluginsProvider(context))
.enableWebKitInspector(Stetho.defaultInspectorModulesProvider(context))
.build());
}
...
}
For Kotlin code, the keyword platformStatic
needs to be used:
public class SomeBox(root: View) : MirrorSandboxBase(root) {
companion object {
platformStatic fun `$init`(context: Context) {
...
}
}
...
}
Step 2: In a mirror screen file, set the sandbox
attribute of the screen
element to the fully qualified name of your sandbox class:
<screen sandbox="com.mypackage.MySandbox">
...
</screen>
See examples for details.
#License
Copyright 2015 jimu Labs Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Written with StackEdit. <== I don't mind leaving this here as it's an awesome editor!