Skip to content

Commit

Permalink
DI and bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
hiranthaR committed Feb 21, 2020
1 parent 009b9e4 commit 3219e6b
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import lk.eclk.locationservice.data.proviers.JWTProviderImpl
import lk.eclk.locationservice.data.remote.ConnectivityInterceptor
import lk.eclk.locationservice.data.remote.ConnectivityInterceptorImpl
import lk.eclk.locationservice.data.remote.api.LocationServiceApiService
import lk.eclk.locationservice.ui.signin.SignInViewModelFactory
import lk.eclk.locationservice.ui.splash.SplashScreenViewModelFactory
import org.kodein.di.Kodein
import org.kodein.di.KodeinAware
Expand Down Expand Up @@ -47,5 +48,6 @@ class LocationServiceApplication : Application(), KodeinAware {

//view model factories
bind() from provider { SplashScreenViewModelFactory(instance()) }
bind() from provider { SignInViewModelFactory(instance()) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ private const val JWT_TOKEN = "jwt_token"

class JWTProviderImpl(context: Context) : PreferenceProvider(context), JWTProvider {

// any view model can observe auth state using this live data
override val authState: LiveData<AuthState> get() = _authState
private val _authState by lazy { MutableLiveData<AuthState>() }

init {
setAuthState(getJWT())
}

// any view model can observe auth state using this live data
override val authState: LiveData<AuthState> get() = _authState
private val _authState = MutableLiveData<AuthState>()

override fun getJWT(): String? {
return preference.getString(JWT_TOKEN, null)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import org.kodein.di.KodeinAware
import org.kodein.di.android.x.closestKodein
import org.kodein.di.generic.instance

class SignInFragment : Fragment(),KodeinAware {
class SignInFragment : Fragment(), KodeinAware {

override val kodein: Kodein by closestKodein()
private lateinit var viewModel: SignInViewModel
Expand All @@ -24,13 +24,12 @@ class SignInFragment : Fragment(),KodeinAware {
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.signin_fragment, container, false)
return inflater.inflate(R.layout.sign_in_fragment, container, false)
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProvider(this,viewModelFactory).get(SignInViewModel::class.java)
// TODO: Use the ViewModel
viewModel = ViewModelProvider(this, viewModelFactory).get(SignInViewModel::class.java)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ class SignInViewModelFactory(private val repository: Repository) :

@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return SignInViewModelFactory(repository) as T
return SignInViewModel(repository) as T
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.NavController
import androidx.navigation.Navigation

import lk.eclk.locationservice.R
import lk.eclk.locationservice.internal.AuthState
import org.kodein.di.Kodein
import org.kodein.di.KodeinAware
import org.kodein.di.android.x.closestKodein
Expand All @@ -18,6 +21,7 @@ class SplashScreenFragment : Fragment(), KodeinAware {
override val kodein: Kodein by closestKodein()
private lateinit var viewModel: SplashScreenViewModel
private val viewModelFactory: SplashScreenViewModelFactory by instance()
private lateinit var navController: NavController

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
Expand All @@ -26,16 +30,19 @@ class SplashScreenFragment : Fragment(), KodeinAware {
return inflater.inflate(R.layout.splash_screen_fragment, container, false)
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel = ViewModelProvider(this, viewModelFactory).get(SplashScreenViewModel::class.java)
navController = Navigation.findNavController(view)
bindUI()
}

private fun bindUI() {
viewModel.authState.observe(this, Observer {
if (it == null) return@Observer

when (it) {
AuthState.NEED_LOGIN -> navController.navigate(R.id.action_splashScreenFragment_to_signInFragment)
}
})
}
}
14 changes: 10 additions & 4 deletions mobile/app/src/main/res/navigation/main_navigation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@
android:id="@+id/splashScreenFragment"
android:name="lk.eclk.locationservice.ui.splash.SplashScreenFragment"
android:label="splash_screen_fragment"
tools:layout="@layout/splash_screen_fragment" >
tools:layout="@layout/splash_screen_fragment">

<action
android:id="@+id/action_splashScreenFragment_to_signInFragment"
app:destination="@id/signInFragment" />
app:destination="@id/signInFragment"
app:enterAnim="@anim/slide_in_down"
app:exitAnim="@anim/fade_out"
app:popEnterAnim="@anim/fade_in"
app:popExitAnim="@anim/slide_out_down" />
</fragment>
<fragment
android:id="@+id/signInFragment"
android:name="lk.eclk.locationservice.ui.signin.SignInFragment"
android:label="signin_fragment"
tools:layout="@layout/signin_fragment" />
android:label="sign_in_fragment"
tools:layout="@layout/sign_in_fragment" />

</navigation>

0 comments on commit 3219e6b

Please sign in to comment.