Skip to content

Instantly share code, notes, and snippets.

🚀
Building Coinverse's Android app

AdamHurwitz AdamSHurwitz

🚀
Building Coinverse's Android app
Block or report user

Report or block AdamSHurwitz

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@AdamSHurwitz
AdamSHurwitz / Node.kt
Last active Feb 18, 2020
ODG - Kotlin Classes and Interfaces: Primary and secondary constructors
View Node.kt
// Primary constructor declared in class creation line with '(...)'
// Default value defined with '='.
class Node(var id: Int = 0, var someAttr: String = "someDefault") : Comparable<Node> {
var newAttribute: Int = 0
// Secondary constructor
constructor(id: Int, newAttribute: Int) : this(id) {
this.newAttribute = newAttribute
}
@AdamSHurwitz
AdamSHurwitz / build.gradle
Created Feb 15, 2020
Optimizing ViewModel with Lifecycle 2.2.0: Setup
View build.gradle
// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:X.X.X"
// LiveData
implementation "androidx.lifecycle:lifecycle-livedata-ktx:X.X.X"
@AdamSHurwitz
AdamSHurwitz / CustomViewModelFactory.kt
Last active Feb 8, 2020
Optimizing ViewModel with Lifecycle 2.2.0: Passing Arguments/Parameters with SavedState
View CustomViewModelFactory.kt
class SomeViewModelFactory(
private val owner: SavedStateRegistryOwner,
private val someString: String) : AbstractSavedStateViewModelFactory(owner, null) {
override fun <T : ViewModel?> create(key: String, modelClass: Class<T>, state: SavedStateHandle) =
SomeViewModel(state, someString) as T
}
class SomeViewModel(private val state: SavedStateHandle, private val someString: String) : ViewModel() {
val feedPosition = state.get<Int>(FEED_POSITION_KEY).let { position ->
if (position == null) 0 else position
@AdamSHurwitz
AdamSHurwitz / Fragment.kt
Last active Feb 8, 2020
Optimizing ViewModel with Lifecycle 2.2.0: Declaring VM val
View Fragment.kt
class Fragment : Fragment() {
private val viewModel: SomeViewModel by viewModels()
private fun observeViewState() {
viewModel.viewState.observe(viewLifecycleOwner) { viewState ->
//viewState used here.
}
}
}
@AdamSHurwitz
AdamSHurwitz / Fragment.kt
Last active Feb 8, 2020
Optimizing ViewModel with Lifecycle 2.2.0: Declaring Instance VM
View Fragment.kt
class Fragment : Fragment() {
private lateinit var viewModel: SomeViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val viewModel: SomeViewModel by viewModels()
this.viewModel = viewModel
}
private fun observeViewState() {
@AdamSHurwitz
AdamSHurwitz / CustomViewModelFactory.kt
Last active Feb 8, 2020
Optimizing ViewModel with Lifecycle 2.2.0: Passing Arguments/Parameters
View CustomViewModelFactory.kt
// Override ViewModelProvider.NewInstanceFactory to create the ViewModel (VM).
class SomeViewModelFactory(private val someString: String): ViewModelProvider.NewInstanceFactory() {
override fun <T : ViewModel?> create(modelClass: Class<T>): T = SomeViewModel(someString) as T
}
class SomeViewModel(private val someString: String) : ViewModel() {
init {
//TODO: Use 'someString' to init process when VM is created. i.e. Get data request.
}
}
@AdamSHurwitz
AdamSHurwitz / build.gradle
Last active Feb 4, 2020
Jcenter gradle configuration
View build.gradle
ext {
bintrayRepo = 'customrippleview'
bintrayName = 'customrippleview'
publishedGroupId = 'com.ebay.customrippleview'
libraryName = 'CustomRippleView'
artifact = 'customrippleview'
libraryDescription = 'Custom Ripple View for Android'
siteUrl = 'https://github.com/eBay/CustomRippleView'
gitUrl = 'https://github.com/eBay/CustomRippleView.git'
libraryVersion = '1.0'
@AdamSHurwitz
AdamSHurwitz / Zip.java
Last active Feb 2, 2020
Rx1: Observable.zip() function
View Zip.java
Observable
.compose(RxHelpers.IOAndMainThreadSchedulers())
.doOnCompleted { ... }
.subscribe({
// Do something with 'it'.
}, {
throwable -> // Do something.
})
Observable.zip(
@AdamSHurwitz
AdamSHurwitz / Observable.java
Created Feb 2, 2020
RxJava2: Listener to Observable
View Observable.java
fun someObservable(): Observable<String> {
return Observable.create<String> { emitter ->
try {
// Listener emits items.
val todos = listOf("1", "2", "3")
for (todo in todos) {
emitter.onNext(todo)
}
emitter.onComplete()
} catch (e: Exception) {
@AdamSHurwitz
AdamSHurwitz / MultipleClasses.java
Created Feb 2, 2020
RxJava: Replacing callbacks between multiple classes
View MultipleClasses.java
// Observable class
private ReplaySubject<Boolean> onBackPressedSubscriber = ReplaySubject.create();
// Call this method in subscribing class.
public Observable<Boolean> onBackPressedEvent() {
return onBackPressedSubscriber.asObservable();
}
private void someMethod() {
You can’t perform that action at this time.