|
| 1 | +# CachePot - Android Simple Data Cache |
| 2 | + |
| 3 | +CachePot is a simple open source for data cache management and passing data object between Fragments without Pacelable and Serializable. |
| 4 | + |
| 5 | +> WARNING: It would be inappropriate to pass data both ways between different android applications, it's a better way to use `Intent` properly. |
| 6 | +
|
| 7 | +# Download |
| 8 | + |
| 9 | +Gradle: |
| 10 | + |
| 11 | +```bash |
| 12 | +repositories { |
| 13 | + jcenter() |
| 14 | +} |
| 15 | + |
| 16 | +dependencies { |
| 17 | + compile 'com.github.kimkevin:cachepot:1.0.0' |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +Maven: |
| 22 | + |
| 23 | +```bash |
| 24 | +<dependency> |
| 25 | + <groupId>com.github.kimkevin</groupId> |
| 26 | + <artifactId>cachepot</artifactId> |
| 27 | + <version>1.0.0</version> |
| 28 | +</dependency> |
| 29 | +``` |
| 30 | + |
| 31 | +# Usage |
| 32 | + |
| 33 | +It works anywhere. there're examples as below |
| 34 | +* Between `Activizty` and `Activity` |
| 35 | +* Between `Activity` and `Fragment` |
| 36 | +* Between `Fragment` and `Fragment` |
| 37 | +* From `PagerAdapter` to individual `Fragment` |
| 38 | + |
| 39 | +#### 1. Pass Object(Model) Synchronously |
| 40 | + |
| 41 | +First push your data object to `CachePot` instance in your `Activity` or `Fragment`. |
| 42 | + |
| 43 | +```java |
| 44 | +// Sample data model |
| 45 | +KoreanFood foodItem = new KoreanFood(1, "Kimchi", "Traditional fermented Korean side dish made of vegetables") |
| 46 | + |
| 47 | +CachePot.getInstance().push(foodItem); |
| 48 | +// open your activity or fragment |
| 49 | +``` |
| 50 | + |
| 51 | +Get your data object in your `Activity` or `Fragment` |
| 52 | + |
| 53 | +```java |
| 54 | +public class MainFragment extends Fragment{ |
| 55 | + private KoreanFood foodItem; |
| 56 | + |
| 57 | + @Override |
| 58 | + public void onCreate(Bundle savedInstanceState) { |
| 59 | + super.onCreate(savedInstanceState); |
| 60 | + |
| 61 | + foodItem = CachePot.getInstance().pop(KoreanFood.class); |
| 62 | + } |
| 63 | +``` |
| 64 | + |
| 65 | +#### 2. Pass Collection or Map Synchronously |
| 66 | + |
| 67 | +First push your collection to `CachePot` instance in your `Activity` or `Fragment`. |
| 68 | + |
| 69 | +```java |
| 70 | +List<KoreanFood> foodItems = new ArrayList<>(); |
| 71 | +foodItems.add(new KoreanFood(1, "Kimchi", "Traditional fermented Korean side dish made of vegetables")); |
| 72 | +foodItems.add(new KoreanFood(2, "Kkakdugi", "A variety of kimchi in Korean cuisine")); |
| 73 | + |
| 74 | +CachePot.getInstance().push(foodItems); |
| 75 | +// open your activity or fragment |
| 76 | +``` |
| 77 | + |
| 78 | +Get your collection in your `Activity` or `Fragment` |
| 79 | + |
| 80 | +```java |
| 81 | +List<KoreanFood> foodItems = CachePot.getInstance().pop(ArrayList.class); |
| 82 | +``` |
| 83 | + |
| 84 | +#### 3. Pass Object(Model) Asynchronously when using ViewPager |
| 85 | + |
| 86 | +First push your data object with position to `CachePot` instance in `FragmentStatePagerAdapter`(or `FragmentPagerAdapter`) |
| 87 | + |
| 88 | +```java |
| 89 | +private class PagerAdapter extends FragmentStatePagerAdapter { |
| 90 | + ... |
| 91 | + public Fragment getItem(int position) { |
| 92 | + CachePot.getInstance().push(position, foodItems.get(position)); |
| 93 | + |
| 94 | + return FoodFragment.newInstance(position); |
| 95 | + } |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +Get your data object in your `Fragment` of `ViewPager` |
| 100 | + |
| 101 | +```java |
| 102 | +public static FoodFragment newInstance(int position) { |
| 103 | + FoodFragment fragment = new FoodFragment(); |
| 104 | + Bundle args = new Bundle(); |
| 105 | + args.putInt(ARG_POSITION, position); |
| 106 | + fragment.setArguments(args); |
| 107 | + return fragment; |
| 108 | +} |
| 109 | + |
| 110 | +@Override |
| 111 | +public void onCreate(Bundle savedInstanceState) { |
| 112 | + super.onCreate(savedInstanceState); |
| 113 | + |
| 114 | + if (getArguments() != null) { |
| 115 | + final int position = getArguments().getInt(ARG_POSITION); |
| 116 | + koreanFoodItem = CachePot.getInstance().pop(position); |
| 117 | + } |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +# Developed By |
| 122 | +* Kevin Yongjun Kim - imkimkevin@gmail.com |
| 123 | + |
| 124 | +# Contributing |
| 125 | +All contributions are welcome. Open a [Pull Requests](https://github.com/kimkevin/CachePot/pulls) or refer to |
| 126 | +the [Issues](https://github.com/kimkevin/CachePot/issues) section. |
| 127 | + |
| 128 | +# License |
| 129 | +MIT |
0 commit comments