Skip to content
This repository has been archived by the owner on Nov 20, 2019. It is now read-only.

tperraut/CoffeeScene-Android

 
 

Repository files navigation

CoffeeScene-Android

A simple scene manager for Android.
CoffeeScene allows to switch the current scene of an Activity, ViewGroup and Fragment (v4 supported).
This library can be usefull if you want to switch from a spinner loader to your main content or a placeholder for example.

Sample app

The sample app is available in this repository under sample/.
You can also try it on Google Play : https://play.google.com/store/apps/details?id=com.geronimostudios.sample.coffeescene

Gradle

Include the dependency Download (.aar) :

dependencies {
    implementation 'com.geronimostudios.coffeescene:coffeescene:1.0.2'
}

Example

How to use with a SceneCreator

You can use the SceneCreator with an activity, viewgroup or fragment.
You have to inflate your viewgroup, fragment or call setContentView(...) in your activity before using the SceneCreator.
The SceneCreator is useful if the anchors of your scenes aren't at the root of your layout.

public class SampleNoAnnotationsActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_no_annotations_sample);
        
        SceneManager.create(
                SceneCreator.with(this)
                        .add(Scene.MAIN, R.id.activity_no_annotations_sample_main_content)
                        .add(Scene.MAIN, R.id.activity_no_annotations_sample_main_content_another_view) // you can add many layouts to the same scene id.
                        .add(Scene.SPINNER, R.id.activity_no_annotations_sample_loader)
                        .add(Scene.PLACEHOLDER, R.id.activity_no_annotations_sample_placeholder)
                        .add(EMPTY_RECYCLER_PLACEHOLDER, R.id.activity_no_annotations_sample_empty_placeholder)
                        .listener(this)
                        .animation(this)
                        .first(Scene.SPINNER)
        );
    }
...
}

With a SceneCreator the scenes are registered by calling .add with an unique identifier and the id of the view/viewgroup that holds the scene.

How to use with annotations only

You have to declare your scenes in your activity, viewgroup or fragment. Each scene requires an unique identifier (sceneId) and a valid layout resource.

@CoffeeScene({
        @Scene(scene = Scene.MAIN, layout = R.layout.sample_activity_main),
        @Scene(scene = Scene.MAIN, layout = R.layout.sample_activity_main_second_anchor), // you can add many layouts to the same scene id.
        @Scene(scene = Scene.SPINNER, layout = R.layout.loader),
        @Scene(scene = Scene.PLACEHOLDER, layout = R.layout.placeholder)
})
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
        SceneManager.create(this);
        
        ...
    }
    ...
}

The class Scene provides a few scene's identifiers : Scene.MAIN, Scene.SPINNER and Scene.PLACEHOLDER.
You are free to use it or not, but be sure that each identifier is unique.

Activities: Just call SceneManager.create(this);. You don't need to call setContentView();
ViewGroup: Just call SceneManager.create(this);. All scenes will be automatically created and added to your viewgroup.
Fragments: With fragments SceneManager.create(this) will returns the view that must be returned by onCreateView

Change the current scene

You can easily change the current scene with SceneManager.scene(this, int sceneId);.
-The first parameter must be the same object that was passed to SceneManager.create(this);.
-The second parameter is the unique identifier of the scene.

...
public class SampleActivity extends AppCompatActivity implements View.OnClickListener {
    ...
    
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sample_switch_to_progress:
                SceneManager.scene(this, Scene.SPINNER); // Change the current scene
                break;
            case R.id.sample_switch_to_placeholder:
                SceneManager.scene(this, Scene.PLACEHOLDER); // Change the current scene
                break;
             ...
        }
    }
}

Release your scenes

Don't forget to release your scenes.

...
public class SampleActivity extends AppCompatActivity implements View.OnClickListener {
    ...
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        SceneManager.release(this);
    }
    ...
}

Full example

@CoffeeScene(
        value = {
                @Scene(scene = Scene.MAIN, layout = R.layout.sample_activity_main),
                @Scene(scene = Scene.SPINNER, layout = R.layout.loader),
                @Scene(scene = Scene.PLACEHOLDER, layout = R.layout.placeholder)
        },
        first = Scene.MAIN
)
public class SampleActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SceneManager.create(this); // Setup scenes

        findViewById(R.id.sample_switch_to_main).setOnClickListener(this);
        findViewById(R.id.sample_switch_to_progress).setOnClickListener(this);
        findViewById(R.id.sample_switch_to_placeholder).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sample_switch_to_main:
                SceneManager.scene(this, Scene.MAIN); // Change the current scene
                break;
            case R.id.sample_switch_to_progress:
                SceneManager.scene(this, Scene.SPINNER); // Change the current scene
                break;
            case R.id.sample_switch_to_placeholder:
                SceneManager.scene(this, Scene.PLACEHOLDER); // Change the current scene
                break;
            ...
        }
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        SceneManager.release(this);
    }
}

License

Copyright (C) 2018 Geronimo

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.

About

A simple scene manager for Android

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%