Skip to content

Instantly share code, notes, and snippets.

View PierceZ's full-sized avatar

Pierce Zaifman PierceZ

  • Canada
View GitHub Profile
@PierceZ
PierceZ / RxBus.java
Last active December 6, 2017 15:47
An event bus made with RxJava and RxAndroid
/**
* Used for subscribing to and publishing to subjects. Allowing you to send data between activities, fragments, etc.
* <p>
* Created by Pierce Zaifman on 2017-01-02.
*/
public final class RxBus {
private static SparseArray<PublishSubject<Object>> sSubjectMap = new SparseArray<>();
private static Map<Object, CompositeDisposable> sSubscriptionsMap = new HashMap<>();
public final class RxBus {
private static PublishSubject<Object> sSubject = PublishSubject.create();
private RxBus() {
// hidden constructor
}
public static Disposable subscribe(@NonNull Consumer<Object> action) {
public final class RxBus {
private static Map<String, PublishSubject<Object>> sSubjectMap = new HashMap<>();
private static Map<Object, CompositeDisposable> sSubscriptionsMap = new HashMap<>();
private RxBus() {
// hidden constructor
}
public abstract class BaseActivity extends AppCompatActivity {
@Override
protected void onDestroy() {
super.onDestroy();
RxBus.unregister(this);
}
}
public abstract class BaseFragment extends Fragment {
@Override
public void onDestroy() {
super.onDestroy();
RxBus.unregister(this);
}
}
@PierceZ
PierceZ / DatabaseUpgradeHelper.java
Created January 29, 2017 15:15
Example of greenDAO migrations
public class DatabaseUpgradeHelper extends DaoMaster.OpenHelper {
public DatabaseUpgradeHelper(Context context, String name) {
super(context, name);
}
@Override
public void onUpgrade(Database db, int oldVersion, int newVersion) {
List<Migration> migrations = getMigrations();
@PierceZ
PierceZ / BusModule.java
Created February 11, 2017 21:37
An example of using Dagger to make a bus of subjects.
@Module
public class BusModule {
public static final String PROVIDER_TOP_SUBJECT = "PROVIDER_TOP_SUBJECT";
public static final String PROVIDER_BOTTOM_SUBJECT = "PROVIDER_BOTTOM_SUBJECT";
@Provides
@Singleton
@Named(PROVIDER_TOP_SUBJECT)
static PublishSubject<String> provideTopSubject() {
@PierceZ
PierceZ / BusComponent.java
Created February 11, 2017 21:43
Dagger component to make use of my BusModule class.
@Component(modules = BusModule.class)
@Singleton
public interface BusComponent {
@Named(BusModule.PROVIDER_TOP_SUBJECT)
PublishSubject<String> getTopSubject();
@Named(BusModule.PROVIDER_BOTTOM_SUBJECT)
PublishSubject<String> getBottomSubject();
}
@PierceZ
PierceZ / App.java
Created February 11, 2017 22:00
App class to hold onto a reference of the BusComponent.
public class App extends Application {
private static BusComponent sBusComponent;
@Override
public void onCreate() {
super.onCreate();
sBusComponent = DaggerBusComponent.create();
}
@PierceZ
PierceZ / BusComponentExample.java
Created February 11, 2017 22:05
Example using the BusComponent.
//Subscribe to a subject
App.getBusComponent().getTopSubject().subscribe((message) -> {
if (mMessageView != null) {
mMessageView.setText(message);
}
});
//Send an item to subscribers of a subject
App.getBusComponent().getBottomSubject().onNext("Hello!");