aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/ui/dialogs/UserDecisionDialog.java
blob: 25349b2d85693016e6d920749348b6b19fcfb440 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package de.thedevstack.conversationsplus.ui.dialogs;

import android.app.Activity;
import android.content.DialogInterface;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import de.thedevstack.conversationsplus.enums.UserDecision;
import de.thedevstack.conversationsplus.ui.listeners.UserDecisionListener;
import de.thedevstack.conversationsplus.R;

/**
 * A dialog to give the user the choice to decide whether to do something or not.
 * The user also has the choice to save his answer for the future.
 * A UserDecisionListener is used to provide the functionality to be performed by clicking on yes, or no.
 */
public class UserDecisionDialog extends AbstractAlertDialog {
    protected final UserDecisionListener listener;
    protected final CheckBox rememberCheckBox;

    public UserDecisionDialog(Activity context, int questionResourceId, UserDecisionListener userDecisionListener) {
        super(context, questionResourceId);
        this.listener = userDecisionListener;

        int viewId = R.layout.dialog_userdecision;
        View view = context.getLayoutInflater().inflate(viewId, null);

        this.rememberCheckBox = (CheckBox) view.findViewById(R.id.dlgUserDecRemember);

        this.setPositiveButton(R.string.cplus_yes, new PositiveOnClickListener());
        this.setNegativeButton(R.string.cplus_no, new NegativeOnClickListener());
        this.setView(view);
    }

    public void decide(UserDecision baseDecision) {
        switch (baseDecision) {
            case ALWAYS:
                this.listener.onYes();
                break;
            case NEVER:
                this.listener.onNo();
                break;
            case ASK:
                this.show();
                break;
        }
    }

    class PositiveOnClickListener implements DialogInterface.OnClickListener {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            listener.onYes();
            if (rememberCheckBox.isChecked()) {
                listener.onRemember(UserDecision.ALWAYS);
            }
        }
    }

    class NegativeOnClickListener implements DialogInterface.OnClickListener {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            listener.onNo();
            if (rememberCheckBox.isChecked()) {
                listener.onRemember(UserDecision.NEVER);
            }
        }
    }
}