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
/** | |
* 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<>(); |
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 RxBus { | |
private static PublishSubject<Object> sSubject = PublishSubject.create(); | |
private RxBus() { | |
// hidden constructor | |
} | |
public static Disposable subscribe(@NonNull Consumer<Object> 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
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 | |
} |
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 { | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
RxBus.unregister(this); | |
} | |
} |
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 { | |
@Override | |
public void onDestroy() { | |
super.onDestroy(); | |
RxBus.unregister(this); | |
} | |
} |
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 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(); |
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
@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() { |
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
@Component(modules = BusModule.class) | |
@Singleton | |
public interface BusComponent { | |
@Named(BusModule.PROVIDER_TOP_SUBJECT) | |
PublishSubject<String> getTopSubject(); | |
@Named(BusModule.PROVIDER_BOTTOM_SUBJECT) | |
PublishSubject<String> getBottomSubject(); | |
} |
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 App extends Application { | |
private static BusComponent sBusComponent; | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
sBusComponent = DaggerBusComponent.create(); | |
} |
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
//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!"); |
OlderNewer