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 / CoroutinesTest.kt
Last active Aug 14, 2019
JUnit Test - Coroutines
View CoroutinesTest.kt
@ExperimentalCoroutinesApi
@get:Rule
// MainCoroutineRule Gist - https://github.com/googlecodelabs/android-testing/blob/codelab2019/app/src/sharedTest/java/com/example/android/architecture/blueprints/todoapp/MainCoroutineRule.kt#L17
var mainCoroutineRule = MainCoroutineRule()
// runBlocking strategy
@Test
fun someTest() = mainCoroutineRule.runBlockingTest {
// Method calling Coroutine here
}
@AdamSHurwitz
AdamSHurwitz / ParameterizedTest.kt
Created Aug 14, 2019
JUnit Test - Parameterized
View ParameterizedTest.kt
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class ParseTest {
@ParameterizedTest
@MethodSource("validTokenProvider")
fun `parse valid tokens`(testData: ValidTokenTestData) {
assertThat(parse(testData.value)).isEqualTo(testData.expectedToken)
}
private fun validTokenProvider() = Stream.of(
@AdamSHurwitz
AdamSHurwitz / ContentResult.kt
Last active Aug 1, 2019
Sample App - Android Unidirectional Data Flow: ContentResult
View ContentResult.kt
sealed class ContentResult {
data class ContentPlayer(val uri: Uri, val image: ByteArray, val errorMessage: String): ContentResult(), Parcelable {...}
}
@AdamSHurwitz
AdamSHurwitz / AudioFragment.kt
Last active Aug 1, 2019
Sample App - Android Unidirectional Data Flow: AudioFragment > observe ContentPlayer
View AudioFragment.kt
class AudioFragment : Fragment() {
private lateinit var contentToPlay: ContentResult.ContentToPlay
private lateinit var contentViewModel: ContentViewModel
private fun observeViewState() {
contentViewModel.playerViewState.observe(viewLifecycleOwner, Observer { viewState ->
viewState?.contentPlayer?.observe(viewLifecycleOwner, EventObserver { contentPlayer ->
//Launch audiocast!
})
})
}
@AdamSHurwitz
AdamSHurwitz / ContentViewModel.kt
Created Aug 1, 2019
Sample App - Android Unidirectional Data Flow: ContentViewModel > getContentPlayer
View ContentViewModel.kt
class ContentViewModel(application: Application) : AndroidViewModel(application) {
private fun getContentPlayer(contentId: String, filePath: String, imageUrl: String) =
getContentUri(contentId, filePath).combineLiveData(bitmapToByteArray(imageUrl)) { a, b ->
Event(ContentResult.ContentPlayer(a.peekEvent().uri, b.peekEvent().image,
getLiveDataErrors(a, b)
))
}
/**
* Sets the value to the result of a function that is called when both `LiveData`s have data
* or when they receive updates after that.
@AdamSHurwitz
AdamSHurwitz / ContentViewModel.kt
Last active Aug 1, 2019
Sample App - Android Unidirectional Data Flow: ContentViewModel > PlayerLoad
View ContentViewModel.kt
class ContentViewModel(application: Application) : AndroidViewModel(application) {
val playerViewState: LiveData<PlayerViewState> get() = _playerViewState
private val _playerViewState = MutableLiveData<PlayerViewState>()
fun processEvent(event: ContentViewEvent) {
when (event) {
is ContentViewEvent.PlayerLoad ->
_playerViewState.value = PlayerViewState(getContentPlayer(
event.contentId, event.filePath, event.previewImageUrl))
}
}
@AdamSHurwitz
AdamSHurwitz / AudioFragment.kt
Last active Aug 1, 2019
Sample App - Android Unidirectional Data Flow: AudioFragment > PlayerLoad
View AudioFragment.kt
class AudioFragment : Fragment() {
private val viewEvent: LiveData<Event<ContentViewEvent>> get() = _viewEvent
private val _viewEvent = MutableLiveData<Event<ContentViewEvent>>()
override fun onCreate(savedInstanceState: Bundle?) {
...
contentToPlay = arguments!!.getParcelable(CONTENT_TO_PLAY_KEY)!!
if (savedInstanceState == null)
_viewEvent.value = Event(ContentViewEvent.PlayerLoad(
contentToPlay.content.id, contentToPlay.filePath!!,
contentToPlay.content.previewImage))
@AdamSHurwitz
AdamSHurwitz / ContentViewState.kt
Created Aug 1, 2019
Sample App - Android Unidirectional Data Flow: ContentViewState > PlayerViewState
View ContentViewState.kt
data class PlayerViewState(val contentPlayer: LiveData<Event<ContentResult.ContentPlayer>>)
@AdamSHurwitz
AdamSHurwitz / ContentFragment.kt
Created Aug 1, 2019
Sample App - Android Unidirectional Data Flow: ContentFragment observe NotifyItemChanged
View ContentFragment.kt
class ContentFragment : Fragment() {
private fun observeViewEffects() {
contentViewModel.viewEffect.observe(viewLifecycleOwner, EventObserver { effect ->
when (effect) {
is ContentViewEffect.NotifyItemChanged -> adapter.notifyItemChanged(effect.position)
}
})
}
}
@AdamSHurwitz
AdamSHurwitz / ContentFragment.kt
Last active Aug 1, 2019
Sample App - Android Unidirectional Data Flow: ContentFragment observe ContentToPlay
View ContentFragment.kt
class ContentFragment : Fragment() {
private fun observeViewState() {
contentViewModel.feedViewState.observe(viewLifecycleOwner, Observer { viewState ->
viewState.contentToPlay.observe(viewLifecycleOwner, EventObserver { contentToPlay ->
when (feedType) {
MAIN, DISMISSED ->
if (childFragmentManager.findFragmentByTag(CONTENT_DIALOG_FRAGMENT_TAG) == null)
ContentDialogFragment().newInstance(Bundle().apply {
putParcelable(CONTENT_TO_PLAY_KEY, contentToPlay)
}).show(childFragmentManager, CONTENT_DIALOG_FRAGMENT_TAG)
You can’t perform that action at this time.