This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add the Google maven repository | |
repositories { | |
maven { | |
url 'https://maven.google.com' | |
} | |
} | |
// Add to your app's dependencies | |
dependencies { | |
compile 'com.android.support.constraint:constraint-layout:1.0.2' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static Disposable subscribe(@Subject int subject, @NonNull Object lifecycle, @NonNull Consumer<Object> action) { | |
Disposable disposable = getSubject(subject).subscribe(action); | |
getCompositeDisposable(lifecycle).add(disposable); | |
return disposable; | |
} | |
//In your code | |
Disposable disposable = RxBus.subscribe(RxBus.SUBJECT_MY_EVENT, this, action); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Example Activity | |
public class MyActivity extends BaseActivity { | |
@Override | |
protected void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.myactivity); | |
// with lambda | |
LiveDataBus.subscribe(LiveDataBus.SUBJECT_DATA_LOADED, this, (data) -> { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EventLiveData extends LiveData<Object> { | |
private final int mSubject; | |
public EventLiveData(@LiveDataBus.Subject int subject) { | |
mSubject = subject; | |
} | |
public void update(Object object) { | |
postValue(object); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class BaseFragment extends Fragment implements LifecycleRegistryOwner { | |
private final LifecycleRegistry mRegistry = new LifecycleRegistry(this); | |
@Override | |
public LifecycleRegistry getLifecycle() { | |
return mRegistry; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class BaseActivity extends AppCompatActivity implements LifecycleRegistryOwner { | |
private final LifecycleRegistry mRegistry = new LifecycleRegistry(this); | |
@Override | |
public LifecycleRegistry getLifecycle() { | |
return mRegistry; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class LiveDataBus { | |
private static SparseArray<EventLiveData> sSubjectMap = new SparseArray<>(); | |
public static final int SUBJECT_DATA_LOADED = 0, SUBJECT_DOWNLOAD_COMPLETE = 1; | |
@Retention(SOURCE) | |
@IntDef({SUBJECT_DATA_LOADED, SUBJECT_DOWNLOAD_COMPLETE}) | |
@interface Subject { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UserDeserializer implements JsonDeserializer<User> { | |
@Override | |
public User deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { | |
User user = new User(); | |
// custom parsing logic goes here | |
return user; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class MyAPIBuilder { | |
private static final String BASE_URL = "https://MySuperGreatAPI.com/"; | |
private static MyAPI sMyAPI; | |
private MyAPIBuilder() { | |
} | |
public static MyAPI getMyAPI() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface MyAPI { | |
@GET("some_json") | |
Observable<JsonObject> getJson(@Header("header1") String header1); | |
@GET("my_user") | |
Observable<User> getUserInfo(); | |
} |