Search in sources :

Example 1 with TogglesHandler

use of fr.neamar.kiss.toggles.TogglesHandler in project KISS by Neamar.

the class TogglesResult method doLaunch.

@Override
public void doLaunch(Context context, View v) {
    CompoundButton toggleButton = null;
    if (v != null) {
        toggleButton = (CompoundButton) v.findViewById(R.id.item_toggle_action_toggle);
    }
    if (toggleButton != null) {
        // Use the handler to check or un-check button
        if (toggleButton.isEnabled()) {
            toggleButton.performClick();
        }
    } else {
        //in case it is pinned on kissbar
        if (togglesHandler == null) {
            togglesHandler = new TogglesHandler(context);
        }
        //get message based on current state of toggle
        String msg = context.getResources().getString(togglesHandler.getState(togglePojo) ? R.string.toggles_off : R.string.toggles_on);
        //toggle state
        togglesHandler.setState(togglePojo, !togglesHandler.getState(togglePojo));
        //show toast to inform user what the state is
        Toast.makeText(context, String.format(msg, " " + this.pojo.displayName), Toast.LENGTH_SHORT).show();
    }
}
Also used : TogglesHandler(fr.neamar.kiss.toggles.TogglesHandler) CompoundButton(android.widget.CompoundButton)

Example 2 with TogglesHandler

use of fr.neamar.kiss.toggles.TogglesHandler in project KISS by Neamar.

the class TogglesResult method display.

@SuppressWarnings({ "ResourceType", "deprecation" })
@Override
public View display(Context context, int position, View v) {
    // On first run, initialize handler
    if (togglesHandler == null)
        togglesHandler = new TogglesHandler(context);
    if (v == null)
        v = inflateFromId(context, R.layout.item_toggle);
    String togglePrefix = "<small><small>" + context.getString(R.string.toggles_prefix) + "</small></small>";
    TextView toggleName = (TextView) v.findViewById(R.id.item_toggle_name);
    toggleName.setText(TextUtils.concat(Html.fromHtml(togglePrefix), enrichText(togglePojo.displayName, context)));
    ImageView toggleIcon = (ImageView) v.findViewById(R.id.item_toggle_icon);
    toggleIcon.setImageDrawable(context.getResources().getDrawable(togglePojo.icon));
    toggleIcon.setColorFilter(getThemeFillColor(context), Mode.SRC_IN);
    // Use the handler to check or un-check button
    final CompoundButton toggleButton = (CompoundButton) v.findViewById(R.id.item_toggle_action_toggle);
    //set listener to null to avoid calling the listener of the older toggle item
    //(due to recycling)
    toggleButton.setOnCheckedChangeListener(null);
    Boolean state = togglesHandler.getState(togglePojo);
    if (state != null)
        toggleButton.setChecked(togglesHandler.getState(togglePojo));
    else
        toggleButton.setEnabled(false);
    toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (!togglesHandler.getState(togglePojo).equals(toggleButton.isChecked())) {
                // record launch manually
                recordLaunch(buttonView.getContext());
                togglesHandler.setState(togglePojo, toggleButton.isChecked());
                toggleButton.setEnabled(false);
                new AsyncTask<Void, Void, Void>() {

                    @Override
                    protected Void doInBackground(Void... params) {
                        try {
                            Thread.sleep(1500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void result) {
                        super.onPostExecute(result);
                        toggleButton.setEnabled(true);
                    }
                }.execute();
            }
        }
    });
    return v;
}
Also used : TogglesHandler(fr.neamar.kiss.toggles.TogglesHandler) OnCheckedChangeListener(android.widget.CompoundButton.OnCheckedChangeListener) AsyncTask(android.os.AsyncTask) TextView(android.widget.TextView) ImageView(android.widget.ImageView) CompoundButton(android.widget.CompoundButton)

Aggregations

CompoundButton (android.widget.CompoundButton)2 TogglesHandler (fr.neamar.kiss.toggles.TogglesHandler)2 AsyncTask (android.os.AsyncTask)1 OnCheckedChangeListener (android.widget.CompoundButton.OnCheckedChangeListener)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1