Dan Sandler | f4e83e0 | 2020-05-12 21:25:31 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.egg.neko; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.SharedPreferences; |
| 21 | import android.content.SharedPreferences.OnSharedPreferenceChangeListener; |
| 22 | |
| 23 | import java.util.ArrayList; |
| 24 | import java.util.List; |
| 25 | import java.util.Map; |
| 26 | |
| 27 | public class PrefState implements OnSharedPreferenceChangeListener { |
| 28 | |
| 29 | private static final String FILE_NAME = "mPrefs"; |
| 30 | |
| 31 | private static final String FOOD_STATE = "food"; |
| 32 | |
| 33 | private static final String WATER_STATE = "water"; |
| 34 | |
| 35 | private static final String CAT_KEY_PREFIX = "cat:"; |
| 36 | |
| 37 | private final Context mContext; |
| 38 | private final SharedPreferences mPrefs; |
| 39 | private PrefsListener mListener; |
| 40 | |
| 41 | public PrefState(Context context) { |
| 42 | mContext = context; |
| 43 | mPrefs = mContext.getSharedPreferences(FILE_NAME, 0); |
| 44 | } |
| 45 | |
| 46 | // Can also be used for renaming. |
| 47 | public void addCat(Cat cat) { |
| 48 | mPrefs.edit() |
| 49 | .putString(CAT_KEY_PREFIX + String.valueOf(cat.getSeed()), cat.getName()) |
| 50 | .apply(); |
| 51 | } |
| 52 | |
| 53 | public void removeCat(Cat cat) { |
| 54 | mPrefs.edit().remove(CAT_KEY_PREFIX + String.valueOf(cat.getSeed())).apply(); |
| 55 | } |
| 56 | |
| 57 | public List<Cat> getCats() { |
| 58 | ArrayList<Cat> cats = new ArrayList<>(); |
| 59 | Map<String, ?> map = mPrefs.getAll(); |
| 60 | for (String key : map.keySet()) { |
| 61 | if (key.startsWith(CAT_KEY_PREFIX)) { |
| 62 | long seed = Long.parseLong(key.substring(CAT_KEY_PREFIX.length())); |
| 63 | Cat cat = new Cat(mContext, seed); |
| 64 | cat.setName(String.valueOf(map.get(key))); |
| 65 | cats.add(cat); |
| 66 | } |
| 67 | } |
| 68 | return cats; |
| 69 | } |
| 70 | |
| 71 | public int getFoodState() { |
| 72 | return mPrefs.getInt(FOOD_STATE, 0); |
| 73 | } |
| 74 | |
| 75 | public void setFoodState(int foodState) { |
| 76 | mPrefs.edit().putInt(FOOD_STATE, foodState).apply(); |
| 77 | } |
| 78 | |
| 79 | public float getWaterState() { |
| 80 | return mPrefs.getFloat(WATER_STATE, 0f); |
| 81 | } |
| 82 | |
| 83 | public void setWaterState(float waterState) { |
| 84 | mPrefs.edit().putFloat(WATER_STATE, waterState).apply(); |
| 85 | } |
| 86 | |
| 87 | public void setListener(PrefsListener listener) { |
| 88 | mListener = listener; |
| 89 | if (mListener != null) { |
| 90 | mPrefs.registerOnSharedPreferenceChangeListener(this); |
| 91 | } else { |
| 92 | mPrefs.unregisterOnSharedPreferenceChangeListener(this); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { |
| 98 | mListener.onPrefsChanged(); |
| 99 | } |
| 100 | |
| 101 | public interface PrefsListener { |
| 102 | void onPrefsChanged(); |
| 103 | } |
| 104 | } |