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
package hakerrank | |
import kotlin.collections.* | |
import kotlin.io.* | |
import kotlin.text.* | |
/** | |
* Hackerrank's Sherlock and Array | |
* https://www.hackerrank.com/challenges/sherlock-and-array | |
* |
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
package hakerrank | |
import java.io.* | |
import java.math.* | |
import java.security.* | |
import java.text.* | |
import java.util.* | |
import java.util.concurrent.* | |
import java.util.function.* | |
import java.util.regex.* |
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 findMedian(arr: Array<Int>): Int { | |
val size = arr.size | |
arr.sort() | |
return arr[(size / 2)] | |
} |
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
package hakerrank | |
import java.io.* | |
import java.math.* | |
import java.security.* | |
import java.text.* | |
import java.util.* | |
import java.util.concurrent.* | |
import java.util.function.* | |
import java.util.regex.* |
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
package hakerrank | |
import java.io.* | |
import java.math.* | |
import java.security.* | |
import java.text.* | |
import java.util.* | |
import java.util.concurrent.* | |
import java.util.function.* | |
import java.util.regex.* |
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
@Test | |
fun saveAuction() { | |
auction.apply { | |
id = 100L | |
title = "auction to save 1" | |
status = Auction.Status.COMPLETE | |
} | |
runBlocking { | |
auctionsCreationViewModel.saveAuction(auction) | |
} |
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
@RunWith(JUnit4::class) | |
class AuctionCreationViewModelTest { | |
@get:Rule | |
val instantTaskExecutorRule = InstantTaskExecutorRule() | |
lateinit var auctionsCreationViewModel: AuctionsCreationViewModel | |
lateinit var auctionCreationRepository: AuctionCreationRepository | |
val auction = Auction() |
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 <T> LiveData<T>.observeOnce(onChangeHandler: (T) -> Unit) { | |
val observer = OneTimeObserver(handler = onChangeHandler) | |
observe(observer, observer) | |
} |
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 OneTimeObserver<T>(private val handler: (T) -> Unit) : Observer<T>, LifecycleOwner { | |
private val lifecycle = LifecycleRegistry(this) | |
init { | |
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME) | |
} | |
override fun getLifecycle(): Lifecycle = lifecycle | |
override fun onChanged(t: T) { |
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 AuctionsCreationViewModel : ViewModel(), CoroutineScope { | |
private var auction: MutableLiveData<Auction> = MutableLiveData() | |
private var auctionCreationRepository = AuctionCreationRepository() | |
private var onClickSaveTrigger = SingleLiveEvent<Unit>() | |
private val job = Job() | |
override val coroutineContext: CoroutineContext | |
get() = job + Dispatchers.Main |