Skip to content

Instantly share code, notes, and snippets.

View ekoo's full-sized avatar
🏠
Working from home

Eko Yulianto ekoo

🏠
Working from home
View GitHub Profile
@ekoo
ekoo / CommonExtensions.kt
Created February 18, 2020 06:41 — forked from adavis/CommonExtensions.kt
Common Android Extensions in Kotlin
fun View.visible() {
visibility = View.VISIBLE
}
fun View.invisible() {
visibility = View.INVISIBLE
}
fun View.gone() {
visibility = View.GONE
implementation 'com.tinder.scarlet:scarlet:0.1.10'
implementation 'com.tinder.scarlet:stream-adapter-rxjava2:0.1.10'
implementation 'com.tinder.scarlet:websocket-okhttp:0.1.10'
implementation 'com.tinder.scarlet:message-adapter-gson:0.1.10'
implementation 'com.tinder.scarlet:lifecycle-android:0.1.10'
implementation "io.reactivex.rxjava2:rxandroid:2.1.1"
implementation "io.reactivex.rxjava2:rxkotlin:2.4.0"
implementation "com.squareup.okhttp3:okhttp:4.5.0"
interface BitcoinService {
@Receive
fun observeWebSocketEvent(): Flowable<WebSocket.Event>
@Send
fun sendSubscribe(subscribe: Subscribe)
@Receive
fun observeResponse(): Flowable<Response>
private val client = OkHttpClient.Builder()
.addInterceptor(HttpLoggingInterceptor()
.setLevel(HttpLoggingInterceptor.Level.BASIC))
.build()
private fun scarletLifecycle() = AndroidLifecycle.ofApplicationForeground(getApplication())
private val service = Scarlet.Builder()
.webSocketFactory(client.newWebSocketFactory(BASE_URL))
.addStreamAdapterFactory(RxJava2StreamAdapterFactory())
service.observeWebSocketEvent()
.filter{ it is WebSocket.Event.OnConnectionOpened<*>}
.subscribe ({ service.sendSubscribe(Subscribe()) },
{error -> error.printStackTrace()})
service.observeResponse()
.subscribeOn(Schedulers.io())
.subscribe({
response.postValue(it)
fun jagaKonter() = viewModelScope.launch {
val listrik = async { beliListrik() }
val pulsa = async { beliPulsa() }
Log.d("example", "Semua Transaksi Selesai, Total Pendapatan : Rp. ${listrik.await()+pulsa.await()}")
}
suspend fun beliListrik(): Int{
Log.d("example", "Melayani Pelanggan Pertama")
delay(7000L)
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
//-------------dependencies yang di butuhkan------------------
//untuk viewmodel
........
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
viewModel = ViewModelProvider(this)[MainViewModel::class.java]
tapMeButton = findViewById(R.id.tapMeButton)
gameScoreTextView = findViewById(R.id.gameScoreTextView)
timeLeftTextView = findViewById(R.id.timeLeftTextView)
.................
val timeLeft: LiveData<Int> get() = _timeLeft
private val _timeLeft = MutableLiveData(60)
private val countDownTimer = flow {
for (timeLeftOnTimer in 60 downTo 0){
emit(timeLeftOnTimer)
delay(1000)
}
}
class NetworkInfo {
@Suppress("DEPRECATION")
@ExperimentalCoroutinesApi
fun checkConnection(context: Context) = callbackFlow{
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val networkRequest = NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)