Kickback

Additional

Language
Java
Version
N/A
Created
Dec 10, 2017
Updated
May 29, 2018 (Retired)
Owner
Legendaries (battleent)
Contributor
Jaewoong Eum (skydoves)
1
Activity
Badge
Generate
Download
Source code

Kickback


This Android library helps to manage static data more structurally by Kickback processor.
It let you persist data statically, get and free easily.

Including in your project

build.gradle

dependencies {
    implementation 'com.github.skydoves:kickback:1.0.3'
    annotationProcessor 'com.github.skydoves:kickback-processor:1.0.3'
}

Usage

Create an abstract class with @KickbackBox annotation.
@Kickback annotation does the role of an entity of static objects.

@KickbackBox(name = "Profile")
public abstract class UserProfile {
    final int visits = 1;

    @KickbackElement(name = "name")
    final String userName = "skydoves";

    @Weak
    @KickbackElement(name = "password")
    final int userPassword = 1234;

    @Weak
    @KickbackElement(name = "secondPassword")
    final int userSecondPassword = 12345;

    @Soft
    @KickbackElement(name = "dog")
    Dog userDog;

    @Soft
    UserPrivates userPrivates;
}

@KickbackElement annotation renames the object's name on your taste.
If not annotated, the default name of the element variable will be field name.
@Weak and @Soft annotation make an instance of a field to reference object.
And you can boxing not only primitive type but also POJO objects.

After builds your project, Kickback_(box name) class will be auto-generated by Kickback processor.
Then you can get instance of new generated class like below.

Kickback_Profile box_profile = Kickback_Profile.getInstance();

Then we can put, take out or free at memory into our new generated box class.

Kickback_Profile box_profile = Kickback_Profile.getInstance();
box_profile.setPassword(4321); // persist "Password" element data
box_profile.setDog(new Dog("Akita", 4, "White")); // persist "Dog" object element data
box_profile.getName(); // get value of "Name" element
box_profile.freeVisits() // free "Visit" element
Kickback_Profile.freeAll(); // free all elements
Kickback_Profile.getBoxName(); // get box name

Function

@KickbackFunction annotation processes getter and setter functions.
@KickbackFunction's "keyname" value determines a target. The target should be a keyName.
Function's name should start with "set" or "get" prefix.

@KickbackFunction(keyname = "name")
public String setNameFunction(String userName) {
    return userName + "!!!";
}

@KickbackFunction(keyname = "name")
public String getNameFunction(String userName) {
    return "Hello" + userName;
}

Use in Android

Sometimes we must pass extra data for using the data next Activity.

@KickbackBox(name = "SecondActivityExtra")
public class SecondActivityExtrasBox {
    @KickbackElement(name = "name")
    final String userName = "skydoves";

    @Weak
    @KickbackElement(name = "password")
    final int userPassword = 0;

    @KickbackFunction(keyname = "name")
    public String setNameFunction(String userName) {
        return userName + "!!!";
    }

    @KickbackFunction(keyname = "name")
    public String getNameFunction(String userName) {
        return "Hello" + userName;
    }
}

Do not need to put Extra data in bundle.
In this case, we do not have to implement Parcerable object and putExtra_ and getExtras.
Instead, Kickback persists data at the memory and you can free on the memory when you want.

public class MainActivity extends AppCompatActivity {

    Kickback_SecondActivityExtra box = Kickback_SecondActivityExtra.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        box.setName("skydoves");
        box.setPassword(1234);

        Button button = findViewById(R.id.button0);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(this, SecondActivity.class));
            }
        });
    }

After using data at next Activity, you can remove data from memory onDestroy

public class SecondActivity extends AppCompatActivity {

    Kickback_SecondActivityExtra box = Kickback_SecondActivityExtra.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        Toast.makeText(this, box.getName() + " :" + box.getPassword(), Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Kickback_SecondActivityExtra.freeAll(); // free all elements from memory
    }
}

In Android project, we can set LifecycleObserver and get LifecycleObserver from box class.
Using by these methods, we can more prevent our project from OOM.

kickback_profile.setLifecycleObserver(LifecycleOwner lifecycleOwner)
kickback_profile.getLifecycleObserver()

License

Copyright 2017 skydoves

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.