Coder Social home page Coder Social logo

alexandereggers / hitogo Goto Github PK

View Code? Open in Web Editor NEW
8.0 2.0 1.0 894 KB

Android library to simplify hints, dialogs, popups, toasts and snackbars inside your app.

License: Apache License 2.0

Java 100.00%
android android-library android-ui hint error-handling dialogue-systems dialogs alert dialog lifecycle

hitogo's Introduction

Hitogo

Download

Hitogo is a fluent-api for Android which helps you to simplify alerts for your app! This api can be initialised by only a few lines of codes.

Motivation

This api is not only usable to simplify the creation of alerts, but also helps you to keep control of all alerts, their trigger sources and the actual alerts which are displayed to the user. Trigger sources are for example server requests, actions triggered by the user or something similar.

Handling alerts can be especially difficult if the trigger sources are not just one but many different per activity/fragment/... This api helps you to prioritise, sort and filter these sources to show only the most relevant alerts to your user base.

Here one example:

You have 20 different trigger sources in your acitivty. In this example these sources are server requests. Each source can trigger an alert to the user to inform him in the case that the request has failed. Makes sense, right? So, let's say 10 of these requests failed. Normally 10 alerts would be triggered now. That probably would look wired to the user or fill up the whole screen with error messages.

At this point Hitogo can help you to structure your alerts to only show one error message instead of 10 (in the case all 10 alerts have the same text).

And another example:

You have again 20 different trigger sources which are based on server requests and one addtional which will trigger when user clicks a certain button. All alerts will be displayed as Snackbars. Because we all know that only one Snackbar can be displayed at the time, it would make sense to focus on the user interaction alert instead of the error messages of these 20 sources, right? So when the user clicks the button, the current Snackbar message should always be dismissed to show the user alert. And it should also surpress attemps of the other trigger sources to dismiss this user alert.

To solve this problem, the library allows you to define a priority for your alert. This priority could be 1 for the user interaction and 2 for the error messages. That means that the user interaction alert will always be displayed.

Download

You can use Gradle to download this libray:

repositories {
  jcenter()
}

dependencies {
  implementation 'org.hitogo:hitogo-core:1.0.3'
  testImplementation 'org.hitogo:hitogo-testing:1.0.3' //mocks and testing tools
}

How do I use Hitogo? (Step-by-step introduction for 1.0.3)

  1. Extend the HitogoController

The HitogoController is the base for the alert system. It decides if the new requested alert should be shown and which should be closed. It also holds several provider-methods which are pointing to your default configuration. The methods are useful if your layouts are quite similar to each other, but only differ for example in the usage of color. For example: getDefaultTitleViewId() holds the view id for the default title textview. Because of defining this default title view id, you can use setTitle(String) instead of setTitle(String, Integer) which should simplify your code base.

public class AlertController extends HitogoController {

    public static final int HINT = 0;
    public static final int SUCCESS = 1;
    public static final int WARNING = 2;
    public static final int DANGER = 3;

    public AlertController(Lifecycle lifecycle) {
        super(lifecycle);
    }

    @Override
    public Integer provideViewLayout(int state) {
        switch (state) {
            case SUCCESS:
                return R.layout.hitogo_success;
            case WARNING:
                return R.layout.hitogo_warning;
            case DANGER:
                return R.layout.hitogo_danger;
            case HINT:
            default:
                return R.layout.hitogo_hint;
        }
    }

    @Nullable
    @Override
    public Integer provideDefaultAlertTextViewId(AlertType type) {
        return R.id.text;
    }
    
    ...
}
  1. Extend HitogoActivity/HitogoFragment or implement HitogoContainer

HitogoActivity and HitogoFragment are using the interface HitogoContainer. This interface is used to give you a certain structure in how to connect alerts to your app. It includes some getter-methods which will be used by the alert builder system.

public class MainActivity extends HitogoActivity<AlertController> {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ...
    }

    @NonNull
    @Override
    public AlertController initialiseController(@NonNull Lifecycle lifecycle) {
        return new AlertController(lifecycle);
    }
}
  1. Start using Hitogo!

If you have finished step 1 and 2, you are ready to go! Using Hitogo you can create views, popups, dialogs, toasts and snackbars. Each builder system will be covered in full length inside the wiki (coming very soon!).

// To create a simple alert that displays a short message, you could do that:
    protected void someMethod() {
        Hitogo.with(this)
                .asViewAlert()
                .withAnimations()
                .addText("Test")
                .asLayoutChild()
                .show();
    }

    //Here is a more complex alert, that has one button:
    public void someMethod() {
        ViewButton button = Hitogo.with(this)
                .asViewButton()
                .setButtonListener(new ButtonListener() {
                    @Override
                    public void onClick(Alert alert, Object parameter) {
                        testOnClick();
                    }
                }, false)
                .setView(R.id.button)
                .addText("Click me!")
                .build();

        Hitogo.with(this)
                .asViewAlert()
                .addText("Test")
                .asLayoutChild()
                .addButton(button)
                .show();
    }

    //Here an example to create a dialog:
    public void someMethod() {
        Hitogo.with(this)
                .asDialogAlert()
                .setTitle("Test Dialog")
                .addText("Long message...")
                .addButton("Ok")
                .show();
    }

    //Last example for creating a popup:
    public void someMethod() {
        Hitogo.with(this)
                .asPopupAlert()
                .addText("Test Popup")
                .setAnchor(R.id.button_test)
                .show();
    }

Status

Version 1.1.0 is currently under development in the develop branch. The latest stable version is 1.0.3 which can be found in the 1.0.x branch.

Each new version version will be supported until the next version is released. For example: 1.0.3 is supported until 1.1.0 or 1.0.4 is out. Each minor release won't include any public API changes.

Comments/bugs/questions/pull requests are always welcome!

Compatibility

  • Minimum Android SDK: Hitogo requires a minimum API level of 16.

Author

Alexander Eggers - @mordag on GitHub

License

Apache 2.0. See the LICENSE file for details.

hitogo's People

Contributors

alexandereggers avatar mordag avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

henkoglobin

hitogo's Issues

Showing AlertDialog without Drawable set causing a IndexOutOfBoundsException

Hello Alex,

this is Flemming from TEF HH Project.
I just found a bug in the DialogAlertImpl when showing an AlertDialog without setting a Drawable.It results in an IndexOutOfBoundsException. It´s happening in Line 137.

SparseArray<Drawable> drawableMap = getParams().getDrawableMap();
        if (view != null) {
            for (int i = 0; i < drawableMap.size(); i++) {
                Integer viewId = drawableMap.keyAt(i);
                Drawable drawable = drawableMap.valueAt(i);
                setDrawable(view, viewId, drawable);
            }
        } else if (textMap.size() > 0) {
            builder.setIcon(drawableMap.valueAt(0));
        }

I think this should look like this:

        if (view != null) {
            for (int i = 0; i < drawableMap.size(); i++) {
                Integer viewId = drawableMap.keyAt(i);
                Drawable drawable = drawableMap.valueAt(i);
                setDrawable(view, viewId, drawable);
            }
        } else if (drawableMap.size() > 0) {
            builder.setIcon(drawableMap.valueAt(0));
        }

Would be great if you could fix it and release a new version of the lib.

Greetings,
Flemming

Min SDK 16

Hi Alexander,
this is Harri from TEF HH.
I hope you enjoy your new Ways.
But there is a Problem with your Great Lib when using Popups with older Devices (API 16):
PopupAlertParams uses Transition, which was introduced with API 19 and crashes on API < 19.
There is a Support Variant, but this is not compatible with PopupWindow,
so the easy Way is to make a SDK Check within onCreateParams:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
enterTransition = holder.getCustomObject(PopupAlertParamsKeys.ENTER_TRANSITION_KEY);
exitTransition = holder.getCustomObject(PopupAlertParamsKeys.EXIT_TRANSITION_KEY);
}
I took Build.VERSION_CODES.M here since using methods for Transition has the same case.

Greets
Harri

DialogAlert only allows two buttons (but claims to support three)

When adding exactly three buttons to a DialogAlertBuilder, an InvalidParameterException is thrown when building the dialog, because DialogAlertImpl (line 295) uses >= rather than > to check for maximum amount of buttons:

        if (getController().provideIsDebugState() && dialogButtonCount >= MAX_BUILDER_BUTTON_AMOUNT) {
            throw new InvalidParameterException("Dialog only supports up to three different " +
                    "builder buttons (primary, secondary and neutral)!");
        }

Version 1.1 TODO

  • Add support for AndroidX.
  • Improve POM creation.
  • Switch to Azure Pipeline CI.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.