Kimkevin CachePot Save

Android Data Object Cache Management

Project README

CachePot - Android Simple Data Cache

Build Status Download API

CachePot is a simple open source for data cache management and passing data object between Fragments(Activities) without Pacelable and Serializable.

WARNING: It would be inappropriate to pass data both ways between different android applications, it's a better way to use Intent properly.

Download

Gradle:

repositories {
  jcenter()
}

dependencies {
  compile 'com.github.kimkevin:cachepot:1.2.0’
}

Maven:

<dependency>
  <groupId>com.github.kimkevin</groupId>
  <artifactId>cachepot</artifactId>
  <version>1.2.0</version>
</dependency>

Usage

It works anywhere. there're examples as below

  • Between Activity and Activity
  • Between Activity and Fragment
  • Between Fragment and Fragment
  • From PagerAdapter to individual Fragment

1. Single Type, Single Data

Push your data object to CachePot instance in your Activity or Fragment.

public void push(Object data)

KoreanFood foodItem = new KoreanFood(1, "Kimchi", "Traditional fermented Korean side dish made of vegetables")  
CachePot.getInstance().push(foodItem);
// open your activity or fragment

Pop your data object from CachePot after view changes

public Object pop(Class classType)

public class MainFragment extends Fragment{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        KoreanFood foodItem = CachePot.getInstance().pop(KoreanFood.class);
    }

In case of Collection or Map

Push

List<KoreanFood> foodItems = new ArrayList<>();
foodItems.add(new KoreanFood(1, "Kimchi", "Traditional fermented Korean side dish made of vegetables"));
foodItems.add(new KoreanFood(2, "Kkakdugi", "A variety of kimchi in Korean cuisine"));

CachePot.getInstance().push(foodItems);
// open your activity or fragment

Pop

List<KoreanFood> foodItems = CachePot.getInstance().pop(ArrayList.class);

2. Single Type, Multiple Data

How to pass Object(Model) Asynchronously when using ViewPager

First push your data object with position or id to CachePot instance in FragmentStatePagerAdapter(or FragmentPagerAdapter)

public void push(long id, Object data)

private class PagerAdapter extends FragmentStatePagerAdapter {
    ...
    public Fragment getItem(int position) {
        KoreanFood foodItem = foodItems.get(position);
        CachePot.getInstance().push(position, foodItem);
        // or
        CachePot.getInstance().push(foodItem.getId(), foodItem);
        ...
    }
}

public Object pop(long id)

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    if (getArguments() != null) {
        final int position = getArguments().getInt(ARG_POSITION);
        foodItem = CachePot.getInstance().pop(position);
        // or
        foodItem = CachePot.getInstance().pop(id);
    }
}

If more complecated views, use TAG

public void push(String tag, Object data)
public void push(String tag, long id, Object data)

private class PagerAdapter extends FragmentStatePagerAdapter {
    ...
    public Fragment getItem(int position) {
        KoreanFood foodItem = foodItems.get(position);
        CachePot.getInstance().push(TAG, position, foodItem);
        // or
        CachePot.getInstance().push(TAG, foodItem.getId(), foodItem);
        ...
    }
}

public Object pop(String tag)
public Object pop(String tag, long id)

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    if (getArguments() != null) {
        final int position = getArguments().getInt(ARG_POSITION);
        foodItem = CachePot.getInstance().pop(TAG, position);
        // or
        foodItem = CachePot.getInstance().pop(TAG, id);
    }
}

Contributing

All contributions are welcome. Open a Pull Requests or refer to the Issues section.

License

MIT
Copyright (c) 2016-present, “KimKevin” Yongjun Kim

Open Source Agenda is not affiliated with "Kimkevin CachePot" Project. README Source: kimkevin/CachePot
Stars
40
Open Issues
1
Last Commit
5 years ago
Repository

Open Source Agenda Badge

Open Source Agenda Rating