Skip to content

Commit

Permalink
Remove GlobalScope Usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Yazan98 committed Feb 4, 2022
1 parent aa00b47 commit 6fca463
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package io.vortex.android.data.executer
import io.reactivex.Flowable
import io.reactivex.disposables.CompositeDisposable
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.lang.ref.WeakReference

Expand Down Expand Up @@ -33,26 +31,20 @@ class VortexFlowableRequestExecutor<Result, Listener : VortexRequestListener.Vor
it?.let { result ->
listener?.let {
it.get()?.let {
GlobalScope.launch {
it.onSuccess(result)
}
it.onSuccess(result)
}
}
}
}, {
listener?.let {
it.get()?.let {
GlobalScope.launch {
it.onError(it as Exception)
}
it.onError(it as Exception)
}
}
}, {
listener?.let {
it.get()?.let {
GlobalScope.launch {
it.onComplete()
}
it.onComplete()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ interface VortexRequestListener {

interface VortexFlowableRequestListener<Result> : VortexObservableRequestListener<Result>
interface VortexObservableRequestListener<Result> : VortexRequestListener {
suspend fun onSuccess(data: Result)
suspend fun onError(error: Throwable)
suspend fun onComplete()
fun onSuccess(data: Result)
fun onError(error: Throwable)
fun onComplete()
}

interface VortexSingleRequestListener<Result> : VortexRequestListener {
suspend fun onSuccess(data: Result)
suspend fun onError(error: Throwable)
fun onSuccess(data: Result)
fun onError(error: Throwable)
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import io.vortex.android.firebase.impl.FirebaseAuthImpl
import io.vortex.android.firebase.listener.VortexAuthListener
import io.vortex.android.firebase.listener.VortexGoogleAuthLIstener
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
Expand Down Expand Up @@ -40,15 +38,11 @@ class VortexFirebaseAuth(
.addOnCompleteListener {
if (it.isSuccessful) {
it.result?.let {
GlobalScope.launch {
listener?.onAuthSuccess(it.user)
}
listener?.onAuthSuccess(it.user)
}
} else {
GlobalScope.launch {
it.exception?.let {
listener?.onAuthError(it)
}
it.exception?.let {
listener?.onAuthError(it)
}
}
}
Expand Down Expand Up @@ -79,9 +73,7 @@ class VortexFirebaseAuth(
val account = task.getResult(ApiException::class.java)
googleListener?.onAuthSuccess(account)
} catch (e: ApiException) {
GlobalScope.launch {
googleListener?.onAuthError(e)
}
googleListener?.onAuthError(e)
}
}
}
Expand All @@ -91,16 +83,12 @@ class VortexFirebaseAuth(
withContext(Dispatchers.IO) {
auth.createUserWithEmailAndPassword(email, password).addOnCompleteListener {
if (it.isSuccessful) {
GlobalScope.launch {
it.result?.let {
listener?.onAuthSuccess(it.user)
}
}
} else {
GlobalScope.launch {
it.exception?.let {
listener?.onAuthError(it)
}
it.exception?.let {
listener?.onAuthError(it)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import io.vortex.android.models.ui.VortexAnimationSettings
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext


Expand Down Expand Up @@ -64,10 +62,8 @@ object VortexGifController {
resource?.registerAnimationCallback(object :
Animatable2Compat.AnimationCallback() {
override fun onAnimationEnd(drawable: Drawable?) {
GlobalScope.launch {
listener?.let {
it.onAnimationFinished()
}
listener?.let {
it.onAnimationFinished()
}
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import com.google.android.libraries.places.api.model.Place
import com.google.android.libraries.places.widget.AutocompleteSupportFragment
import com.google.android.libraries.places.widget.listener.PlaceSelectionListener
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.util.*

Expand Down Expand Up @@ -83,25 +81,21 @@ object VortexMapBuilder {
autocompleteFragment.setOnPlaceSelectedListener(object : PlaceSelectionListener {
override fun onPlaceSelected(p0: Place) {
p0.latLng?.let {
GlobalScope.launch {
listener.onPlacePicked(it)
}
listener.onPlacePicked(it)
}
}

override fun onError(p0: Status) {
GlobalScope.launch {
listener.onError(p0)
}
listener.onError(p0)
}

})
}
}

interface AutoPlaceConfiguration {
suspend fun onPlacePicked(latLng: LatLng)
suspend fun onError(p0: Status)
fun onPlacePicked(latLng: LatLng)
fun onError(p0: Status)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,37 @@ import io.vortex.android.VortexViewModelType
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

/**
* VortexViewModel Class (One of Supported ViewModels)
* Used to ViewModels That only Needs to Manage State Without Actions
*/
abstract class VortexIndirectViewModel<State> : VortexPureViewModel(), VortexIndirectViewModelImpl<State>,
VortexViewModelType {

private val state: MutableLiveData<State> by lazy {
MutableLiveData<State>()
}

/**
* Override The Current State With Your New State By Using This Method
* To Accept New State
*/
override suspend fun acceptNewState(newState: State) {
withContext(Dispatchers.IO) {
state.postValue(newState)
}
}

/**
* Get Current State To Subscribe it With Current ViewLifecycleOwner
*/
override fun getStateHandler(): MutableLiveData<State> {
return state
}

/**
* Get Current Value of Current Saved State in this ViewModel
*/
override fun getCurrentState(): State? {
return getStateHandler().value
}
Expand Down

0 comments on commit 6fca463

Please sign in to comment.