1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 *      http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15package com.android.egg.neko;
16
17import android.support.annotation.NonNull;
18import android.app.Dialog;
19import android.content.Context;
20import android.support.v7.widget.GridLayoutManager;
21import android.support.v7.widget.RecyclerView;
22import android.view.LayoutInflater;
23import android.view.View;
24import android.view.ViewGroup;
25import android.widget.ImageView;
26import android.widget.TextView;
27
28import com.android.egg.R;
29
30import java.util.ArrayList;
31
32public class NekoDialog extends Dialog {
33
34    private final Adapter mAdapter;
35
36    public NekoDialog(@NonNull Context context) {
37        super(context, android.R.style.Theme_Material_Dialog_NoActionBar);
38        RecyclerView view = new RecyclerView(getContext());
39        mAdapter = new Adapter(getContext());
40        view.setLayoutManager(new GridLayoutManager(getContext(), 2));
41        view.setAdapter(mAdapter);
42        final float dp = context.getResources().getDisplayMetrics().density;
43        final int pad = (int)(16*dp);
44        view.setPadding(pad, pad, pad, pad);
45        setContentView(view);
46    }
47
48    private void onFoodSelected(Food food) {
49        PrefState prefs = new PrefState(getContext());
50        int currentState = prefs.getFoodState();
51        if (currentState == 0 && food.getType() != 0) {
52            NekoService.registerJob(getContext(), food.getInterval(getContext()));
53        }
54        prefs.setFoodState(food.getType());
55        dismiss();
56    }
57
58    private class Adapter extends RecyclerView.Adapter<Holder> {
59
60        private final Context mContext;
61        private final ArrayList<Food> mFoods = new ArrayList<>();
62
63        public Adapter(Context context) {
64            mContext = context;
65            int[] foods = context.getResources().getIntArray(R.array.food_names);
66            // skip food 0, you can't choose it
67            for (int i=1; i<foods.length; i++) {
68                mFoods.add(new Food(i));
69            }
70        }
71
72        @Override
73        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
74            return new Holder(LayoutInflater.from(parent.getContext())
75                    .inflate(R.layout.food_layout, parent, false));
76        }
77
78        @Override
79        public void onBindViewHolder(final Holder holder, int position) {
80            final Food food = mFoods.get(position);
81            ((ImageView) holder.itemView.findViewById(R.id.icon))
82                    .setImageIcon(food.getIcon(mContext));
83            ((TextView) holder.itemView.findViewById(R.id.text))
84                    .setText(food.getName(mContext));
85            holder.itemView.setOnClickListener(new View.OnClickListener() {
86                @Override
87                public void onClick(View v) {
88                    onFoodSelected(mFoods.get(holder.getAdapterPosition()));
89                }
90            });
91        }
92
93        @Override
94        public int getItemCount() {
95            return mFoods.size();
96        }
97    }
98
99    public static class Holder extends RecyclerView.ViewHolder {
100
101        public Holder(View itemView) {
102            super(itemView);
103        }
104    }
105}
106