This file contains hidden or 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
# Built application files | |
*.apk | |
*.ap_ | |
*.aab | |
# Files for the ART/Dalvik VM | |
*.dex | |
# Java class files | |
*.class |
This file contains hidden or 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
allprojects { | |
ext { | |
// NEVER name this block 'android'. Never name *ANY* block android | |
androidVersions = [ | |
// updated 17/3/2019 | |
androidXVersion : "1.0.0", | |
livedata : '2.0.0-rc01', | |
minSdkVersion : 21, | |
targetSdkVersion : 28, | |
compileSdkVersion: 28, |
This file contains hidden or 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
import kotlinx.coroutines.CoroutineScope | |
import kotlinx.coroutines.Dispatchers | |
import kotlinx.coroutines.Job | |
import kotlinx.coroutines.launch | |
import kotlinx.coroutines.newFixedThreadPoolContext | |
import kotlinx.coroutines.runBlocking | |
import kotlinx.coroutines.withContext | |
import java.util.UUID | |
import java.util.concurrent.ConcurrentHashMap |
This file contains hidden or 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
import kotlinx.coroutines.CoroutineScope | |
import kotlinx.coroutines.Dispatchers | |
import kotlinx.coroutines.Job | |
import kotlinx.coroutines.launch | |
import kotlinx.coroutines.newFixedThreadPoolContext | |
import kotlinx.coroutines.runBlocking | |
import kotlinx.coroutines.withContext | |
import java.util.UUID | |
import java.util.concurrent.ConcurrentHashMap |
This file contains hidden or 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
class PostViewModel(private val redditRepository: RedditRepository) : ViewModel() { | |
val stateData = MutableLiveData<State>() | |
fun getRedditPost() { | |
viewModelScope.launch { | |
stateData.value = State.Loading | |
val post: State.Post = redditRepository.getPost() | |
stateData.value = post | |
} |
This file contains hidden or 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
class RedditRepository(private val dispatcherProvider: DispatcherProvider) { | |
suspend fun getPost(): State.Post { | |
return withContext(dispatcherProvider.io) { | |
delay(5000) | |
State.Post("to be fair") | |
} | |
} | |
} |
This file contains hidden or 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
class RedditRepository(private val dispatcherProvider: DispatcherProvider) { | |
suspend fun getPost(): State.Post { | |
return withContext(dispatcherProvider.io) { | |
delay(2000) | |
State.Post("🐢 SLOW AND STEADY 🐢 WINS THE RACE 🐢 MODS CAN'T BAN ME 🐢 AT THIS PACE 🐢") | |
} | |
} | |
} |
This file contains hidden or 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
interface DispatcherProvider { | |
val io: CoroutineDispatcher | |
val ui: CoroutineDispatcher | |
val default: CoroutineDispatcher | |
val unconfined: CoroutineDispatcher | |
} | |
class DefaultDispatcherProvider( | |
override val ui: CoroutineDispatcher = Dispatchers.Main, | |
override val default: CoroutineDispatcher = Dispatchers.Default, |
This file contains hidden or 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
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
super.onViewCreated(view, savedInstanceState) | |
viewModelpost.stateData.observe(viewLifecycleOwner, Observer { state -> | |
when (state) { | |
State.Loading -> button.text = "Loading" | |
is State.Post -> button.text = state.text | |
} | |
}) |
This file contains hidden or 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
fun getRedditPost() { | |
viewModelScope.launch(Dispatchers.Main) { | |
stateData.value = State.Loading | |
val post: State.Post = redditRepository.getPost() | |
stateData.value = post | |
} | |
} |