Beautiful Code

This is what I’m aiming for — powerful declarations, minimal logic. UIButtonGroup, UIWebView and UIBundleHelper carry a lot of the weight here and I hope to explain (and share) all of these soon.

public class TTActivityInfo

extends TTAbstractActivity

{

public Button uiLeftButton;

public Button uiRightButton;

public UIButtonGroup buttonGroup;

public WebView uiWebView;

static String lActivityTitle = "Welcome!";

static String lLeftButtonTitle = "Welcome";

static String lRightButtonTitle = "About the app";

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

this.setContentView(R.layout.tt_activity_info);

UIHelper.bindViews(this);

UIHelper.hookupButtons(this);

_configureWebView();

_configureNavigationBar();

_configureButtons();

UIBundleHelper.restoreActivityStateFromBundle(this, savedInstanceState);

if (savedInstanceState == null) {

_forceWebViewToCurrentState();

}

}

@Override

protected void onSaveInstanceState(Bundle _bundle)

{

UIBundleHelper.saveActivityStateToBundle(this, _bundle);

}

protected void _configureNavigationBar()

{

TTUI.configureNavigationBarTitle(this, lActivityTitle);

}

protected void _configureButtons()

{

TTUI.configureBlueButton(this, uiLeftButton, lLeftButtonTitle);

TTUI.configureBlueButton(this, uiRightButton, lRightButtonTitle);

buttonGroup = new UIButtonGroup(uiLeftButton, uiRightButton);

buttonGroup.select(uiLeftButton);

}

protected void _configureWebView()

{

if (uiWebView == null) {

return;

}

uiWebView.getSettings().setJavaScriptEnabled(true);

uiWebView.setWebViewClient(new UIWebViewClient(this, "uiWebView"));

}

protected void _forceWebViewToCurrentState()

{

if (uiWebView == null) {

return;

}

if (buttonGroup.getSelectedButton() == uiRightButton) {

uiWebView.loadUrl("http://www.google.com");

} else {

uiWebView.loadUrl("http://www.davidjanes.com");

}

}

public void _handleButtonSelected(Button _button)

{

buttonGroup.select(_button);

_forceWebViewToCurrentState();

}

public void uiLeftButton_onClick(Button _button)

{

_handleButtonSelected(_button);

}

public void uiRightButton_onClick(Button _button)

{

_handleButtonSelected(_button);

}

}

2 comments on this post.
  1. Arnaud:

    Hello,

    When are you share your code about LogHelper ?
    Because i use fragment of it
    Regards
    Arnaud

  2. admin:

    I don’t have a publicly shared version of this at this time — it’s pretty obvious though; it just outputs the vararg arguments.