aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/PocketQueryList.java
blob: 27edffd74ad537aecace76f7c7df346601563adb (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
72
73
74
75
76
77
78
package cgeo.geocaching;

import cgeo.geocaching.activity.ActivityMixin;
import cgeo.geocaching.connector.gc.GCParser;

import org.apache.commons.collections4.CollectionUtils;

import rx.android.app.AppObservable;
import rx.functions.Action1;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;

import java.util.List;

public final class PocketQueryList {

    private final String guid;
    private final int maxCaches;
    private final String name;

    public PocketQueryList(final String guid, final String name, final int maxCaches) {
        this.guid = guid;
        this.name = name;
        this.maxCaches = maxCaches;
    }

    public String getGuid() {
        return guid;
    }

    public int getMaxCaches() {
        return maxCaches;
    }

    public String getName() {
        return name;
    }

    public static void promptForListSelection(final Activity activity, final Action1<PocketQueryList> runAfterwards) {
        final Dialog waitDialog = ProgressDialog.show(activity, activity.getString(R.string.search_pocket_title), activity.getString(R.string.search_pocket_loading), true, true);

        AppObservable.bindActivity(activity, GCParser.searchPocketQueryListObservable).subscribe(new Action1<List<PocketQueryList>>() {
            @Override
            public void call(final List<PocketQueryList> pocketQueryLists) {
                waitDialog.dismiss();
                selectFromPocketQueries(activity, pocketQueryLists, runAfterwards);
            }
        });
    }

    private static void selectFromPocketQueries(final Activity activity, final List<PocketQueryList> pocketQueryList, final Action1<PocketQueryList> runAfterwards) {
        if (CollectionUtils.isEmpty(pocketQueryList)) {
            ActivityMixin.showToast(activity, activity.getString(R.string.warn_no_pocket_query_found));
            return;
        }

        final CharSequence[] items = new CharSequence[pocketQueryList.size()];

        for (int i = 0; i < pocketQueryList.size(); i++) {
            items[i] = pocketQueryList.get(i).name;
        }

        new AlertDialog.Builder(activity)
                .setTitle(activity.getString(R.string.search_pocket_select))
                .setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(final DialogInterface dialogInterface, final int itemId) {
                        dialogInterface.dismiss();
                        runAfterwards.call(pocketQueryList.get(itemId));
                    }
                }).create().show();
    }

}