aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/gcvote/GCVoteRatingBarUtil.java
blob: 2d485bd0c251db3715ff2d7d500db12cfea77dd2 (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
package cgeo.geocaching.gcvote;

import butterknife.ButterKnife;

import cgeo.geocaching.Geocache;
import cgeo.geocaching.R;

import org.eclipse.jdt.annotation.Nullable;

import android.view.View;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;

/**
 * TODO: convert to fragment
 *
 */
public final class GCVoteRatingBarUtil {
    public interface OnRatingChangeListener {
        public void onRatingChanged(final float stars);
    }

    private GCVoteRatingBarUtil() {
        // utility class
    }

    public static void initializeRatingBar(final Geocache cache, final View parentView, @Nullable final OnRatingChangeListener changeListener) {
        if (GCVote.isVotingPossible(cache)) {
            final RatingBar ratingBar = ButterKnife.findById(parentView, R.id.gcvoteRating);
            final TextView label = ButterKnife.findById(parentView, R.id.gcvoteLabel);
            ratingBar.setVisibility(View.VISIBLE);
            label.setVisibility(View.VISIBLE);
            ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

                @Override
                public void onRatingChanged(final RatingBar ratingBar, final float stars, final boolean fromUser) {
                    // 0.5 is not a valid rating, therefore we must limit
                    final float rating = GCVote.isValidRating(stars) ? stars : 0;
                    if (rating < stars) {
                        ratingBar.setRating(rating);
                    }
                    label.setText(GCVote.getDescription(rating));
                    if (changeListener != null) {
                        changeListener.onRatingChanged(rating);
                    }
                }
            });
            ratingBar.setRating(cache.getMyVote());
        }
    }

    public static float getRating(final View parentView) {
        final RatingBar ratingBar = ButterKnife.findById(parentView, R.id.gcvoteRating);
        return ratingBar.getRating();
    }

}