Navigation Menu

Skip to content

Commit

Permalink
Readme updated, StateController moved to StatefulLayout
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubkinst committed Jan 24, 2017
1 parent f8ce4a9 commit aeea43f
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 87 deletions.
33 changes: 33 additions & 0 deletions .travis.yml
@@ -0,0 +1,33 @@
language: android
sudo: false
jdk: oraclejdk8
env:
matrix:
- ANDROID_TARGET=android-25 ANDROID_ABI=armeabi-v7a

android:
components:
- platform-tools
- tools
- build-tools-25.0.2
- android-25

# Additional components
- extra-google-m2repository
- extra-android-m2repository
- extra-android-support

cache:
directories:
- $HOME/.gradle/caches/2.9
- $HOME/.gradle/caches/jars-1
- $HOME/.gradle/daemon
- $HOME/.gradle/native
- $HOME/.gradle/wrapper


script:
# Unit tests
- ./gradlew testReleaseUnitTest
# If successful and Git TAG exists - publish to Bintray
- '[[ -z $TRAVIS_TAG ]] || ./gradlew bintrayUpload -q'
72 changes: 64 additions & 8 deletions README.md
@@ -1,10 +1,62 @@
# StatefulLayout
# Android StatefulLayout 2
![alt text](screen.gif)

## Installation
### Gradle/Maven
compile 'cz.kinst.jakub:stateful-layout:1.1.5'
## Usage
## StatefulLayout
compile 'cz.kinst.jakub:android-stateful-layout-base:2.0.1'

This is a base class you should **use if you want to have completely custom set of states/views**. You can either **inherit** and add custom states in constructor
(see `SimpleStateLayout` as an example) or you can use directly `StatefulLayout` and **add states dynamically in your code** via `setStateView()`. Raw `StatefulLayout`
contains only one state - `StatefulLayout.State.CONTENT` with whatever child is inside the XML content of the tag.

### Example
```xml
<cz.kinst.jakub.view.StatefulLayout
android:id="@+id/stateful_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!--Your Content Here-->

</cz.kinst.jakub.view.StatefulLayout>
```
```java
// in onCreate()
statefulLayout.setStateView(STATE_NO_PERSMISSION, LayoutInflater.from(this).inflate(R.layout.state_no_permission, null));
statefulLayout.setStateView(STATE_PROGRESS, LayoutInflater.from(this).inflate(R.layout.state_progress, null));
```

### API
- `setStateView(String state, View view)` Adds a new state and with a corresponding View
- `setState(String state)` Changes current state
- `getState()` Returns current view state (String ID)
- `setOnStateChangeListener(OnStateChangeListener listener)` Sets a listener on state change event
- `setStateController(StateController stateController)` Sets a state controller object. See below.

### StateController
If you don't want to directly manipulate with the view (MVVM/MVP scenario) you can create an instance of `StateController`
and bind it to `StatefulLayout` (e.g. using Data Binding). `StateController` allows you to setup different states as well as control current state itself.
See example below or `DataBindingControllerActivity` in sample project.

```xml
<cz.kinst.jakub.view.StatefulLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:stateController="@{viewModel.stateController}">
</cz.kinst.jakub.view.StatefulLayout>
```
```java
stateController = StatefulLayout.StateController.create()
.withState(STATE_NO_PERSMISSION, LayoutInflater.from(this).inflate(R.layout.state_no_permission, null))
.withState(STATE_PROGRESS, LayoutInflater.from(this).inflate(R.layout.state_progress, null))
.build();

//...

stateController.setState(STATE_PROGRESS);
```

## SimpleStatefulLayout
compile 'cz.kinst.jakub:android-stateful-layout-simple:2.0.1'

### Example
```xml
Expand All @@ -17,10 +69,13 @@

</cz.kinst.jakub.view.SimpleStatefulLayout>
```

### Optional Attributes
- `app:offlineText` Custom text to show when in OFFLINE state
- `app:offlineRetryText` Text for a retry button in OFFLINE state
- `app:emptyText` Custom text to show when in OFFLINE state
- `app:offlineImageDrawable` Custom image to show above the offline state text (if not using custom layout)
- `app:offlineImageDrawable` Custom image to show above the offline state text (if not using custom layout)
- `app:emptyImageDrawable` Custom image to show above the empty state text (if not using custom layout)
- `app:offlineLayout` Custom layout to show when in OFFLINE state
- `app:emptyLayout` Custom layout to show when in EMPTY state
Expand All @@ -33,17 +88,18 @@
- `showProgress()`
- `showEmpty()`
- `showOffline()`
- `getState()` Returns current view state (CONTENT/OFFLINE/EMPTY/PROGRESS)
- `setEmptyText(String text)` If using default layouts, this will set the text displayed in the EMPTY state
- `setOfflineText(String text)` If using default layouts, this will set the text displayed in the OFFLINE state
- `setOfflineRetryText(String text)` If using default layouts, this will set the text of a retry button displayed in the OFFLINE state
- `setOfflineRetryOnClickListener(OnClickListener listener)` If using default layouts, this will set the click listener to a retry button displayed in the OFFLINE state
- `setEmptyImageDrawable(Drawable drawable)` Sets custom image shown above empty text when not using custom layout
- `setEmptyImageResource(int resourceId)` Sets custom image shown above empty text when not using custom layout
- `setOfflineImageDrawable(Drawable drawable)` Sets custom image shown above offline text when not using custom layout
- `setOfflineImageResource(int resourceId)` Sets custom image shown above offline text when not using custom layout
- `setOnStateChangeListener(OnStateChangeListener listener)` Sets a listener on state change event
- `setTransitionsEnabled(boolean enabled)` Enables/disables transitions between states

## License
Copyright 2015 Jakub Kinst (jakub@kinst.cz)
Copyright 2017 Jakub Kinst (jakub@kinst.cz)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down

This file was deleted.

Expand Up @@ -179,4 +179,58 @@ public static class State {
public static final String CONTENT = "content";
}


public static class StateController {
private Map<String, View> mStateMap = new HashMap<>();
private String mState = State.CONTENT;
private OnStateChangeListener mListener;


private StateController() {
}


public static Builder create() {
return new Builder();
}


public Map<String, View> getStates() {
return mStateMap;
}


public String getState() {
return mState;
}


public void setState(String newState) {
mState = newState;
if(mListener != null)
mListener.onStateChange(newState);
}


void setOnStateChangeListener(final OnStateChangeListener listener) {
mListener = listener;
}


public static class Builder {
StateController mStateController = new StateController();


public Builder withState(String state, View stateView) {
mStateController.mStateMap.put(state, stateView);
return this;
}


public StateController build() {
return mStateController;
}

}
}
}
Expand Up @@ -22,7 +22,7 @@
public class SimpleStatefulLayout extends StatefulLayout {

private String mInitialState = State.CONTENT;
private boolean mTransitionEnabled = true;
private boolean mTransitionsEnabled = true;


public class State extends StatefulLayout.State {
Expand Down Expand Up @@ -182,7 +182,7 @@ public void setOfflineImageDrawable(Drawable drawable) {

@Override
public void setState(String state) {
if(isTransitionEnabled())
if(isTransitionsEnabled())
TransitionManager.beginDelayedTransition(this);
super.setState(state);
}
Expand Down Expand Up @@ -231,13 +231,13 @@ public void setEmptyView(View emptyView) {
}


public boolean isTransitionEnabled() {
return mTransitionEnabled;
public boolean isTransitionsEnabled() {
return mTransitionsEnabled;
}


public void setTransitionEnabled(boolean transitionEnabled) {
mTransitionEnabled = transitionEnabled;
public void setTransitionsEnabled(boolean transitionsEnabled) {
mTransitionsEnabled = transitionsEnabled;
}


Expand Down
Expand Up @@ -9,7 +9,6 @@

import cz.kinst.jakub.sample.statefullayout.databinding.ActivityDataBindingControllerBinding;
import cz.kinst.jakub.sample.statefullayout.databinding.CustomGenericBinding;
import cz.kinst.jakub.view.StateController;
import cz.kinst.jakub.view.StatefulLayout;


Expand All @@ -18,7 +17,7 @@ public class DataBindingControllerActivity extends AppCompatActivity {
public static final String STATE_1 = "state_1";
public static final String STATE_2 = "state_2";
public static final String STATE_3 = "state_3";
private StateController mStateController;
private StatefulLayout.StateController mStateController;


public static Intent newIntent(Context context) {
Expand All @@ -38,7 +37,7 @@ protected void onCreate(Bundle savedInstanceState) {
CustomGenericBinding state3 = CustomGenericBinding.inflate(getLayoutInflater());
state3.setContent("State 3");

mStateController = StateController.create()
mStateController = StatefulLayout.StateController.create()
.withState(STATE_1, state1.getRoot())
.withState(STATE_2, state2.getRoot())
.withState(STATE_3, state3.getRoot())
Expand Down
Expand Up @@ -7,7 +7,7 @@

<variable
name="stateController"
type="cz.kinst.jakub.view.StateController" />
type="cz.kinst.jakub.view.StatefulLayout.StateController" />
</data>

<LinearLayout
Expand Down Expand Up @@ -44,7 +44,6 @@
android:text="3" />
</LinearLayout>
<cz.kinst.jakub.view.StatefulLayout
android:id="@+id/stateful"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:stateController="@{stateController}">
Expand Down
Binary file modified screen.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit aeea43f

Please sign in to comment.