Skip to content

Instantly share code, notes, and snippets.

View PierceZ's full-sized avatar

Pierce Zaifman PierceZ

  • Canada
View GitHub Profile
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!");
@PierceZ
PierceZ / NoParallel.java
Created February 26, 2017 16:28
Processing data without parallelization.
List<String> dataList;
//dataList gets populated here
List<DataModel> result = new ArrayList<>();
for (String data : dataList) {
result.add(DataParser.createData(data));
}
@PierceZ
PierceZ / EmptyActivityManifest.xml
Created March 31, 2017 14:26
The AndroidManifest.xml with an empty activity.
<activity
android:name=".activity.MainEmptyActivity"
android:theme="@android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
@PierceZ
PierceZ / CustomView.java
Created April 9, 2017 15:20
Example custom view.
public class CustomView extends LinearLayout {
public CustomView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
setOrientation(LinearLayout.VERTICAL);
LayoutInflater.from(context).inflate(R.layout.customview, this, true);
String title;
String subtitle;
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomView, 0, 0);