-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from hiranthaR/development
login interface
- Loading branch information
Showing
27 changed files
with
405 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
4 changes: 4 additions & 0 deletions
4
mobile/app/src/main/java/lk/eclk/locationservice/data/Repository.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
package lk.eclk.locationservice.data | ||
|
||
import androidx.lifecycle.LiveData | ||
import lk.eclk.locationservice.internal.AuthState | ||
|
||
interface Repository { | ||
|
||
//repository for handle all data flows inside the android app | ||
fun getAuthState(): LiveData<AuthState> | ||
} |
27 changes: 26 additions & 1 deletion
27
mobile/app/src/main/java/lk/eclk/locationservice/data/RepositoryImpl.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,28 @@ | ||
package lk.eclk.locationservice.data | ||
|
||
class RepositoryImpl() : Repository {} | ||
import androidx.lifecycle.LiveData | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.GlobalScope | ||
import kotlinx.coroutines.launch | ||
import lk.eclk.locationservice.data.proviers.JWTProvider | ||
import lk.eclk.locationservice.internal.AuthState | ||
|
||
class RepositoryImpl( | ||
private val jwtProvider: JWTProvider | ||
) : Repository { | ||
|
||
init { | ||
jwtProvider.apply { | ||
authState.observeForever { | ||
GlobalScope.launch(Dispatchers.IO) { | ||
// there should handle auth state's data | ||
// eg: fetching user data | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun getAuthState(): LiveData<AuthState> = jwtProvider.authState | ||
|
||
|
||
} |
11 changes: 11 additions & 0 deletions
11
mobile/app/src/main/java/lk/eclk/locationservice/data/proviers/JWTProvider.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package lk.eclk.locationservice.data.proviers | ||
|
||
import androidx.lifecycle.LiveData | ||
import lk.eclk.locationservice.internal.AuthState | ||
|
||
interface JWTProvider { | ||
val authState: LiveData<AuthState> | ||
fun getJWT(): String? | ||
fun setJWT(token: String): Boolean | ||
fun unsetJWT(): Boolean | ||
} |
38 changes: 38 additions & 0 deletions
38
mobile/app/src/main/java/lk/eclk/locationservice/data/proviers/JWTProviderImpl.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package lk.eclk.locationservice.data.proviers | ||
|
||
import android.content.Context | ||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import lk.eclk.locationservice.internal.AuthState | ||
|
||
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()) | ||
} | ||
|
||
override fun getJWT(): String? { | ||
return preference.getString(JWT_TOKEN, null) | ||
} | ||
|
||
override fun setJWT(token: String): Boolean { | ||
setAuthState(token) | ||
return preference.edit().putString(JWT_TOKEN, token).commit() | ||
} | ||
|
||
override fun unsetJWT(): Boolean { | ||
setAuthState(null) | ||
return preference.edit().remove(JWT_TOKEN).commit() | ||
} | ||
|
||
private fun setAuthState(jwt: String?) { | ||
if (jwt.isNullOrEmpty()) _authState.postValue(AuthState.NEED_LOGIN) | ||
if (!jwt.isNullOrEmpty()) _authState.postValue(AuthState.LOGGED_IN) | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
mobile/app/src/main/java/lk/eclk/locationservice/data/proviers/PreferenceProvider.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package lk.eclk.locationservice.data.proviers | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import androidx.preference.PreferenceManager | ||
|
||
abstract class PreferenceProvider(context: Context) { | ||
// shared preference for cache data | ||
private val appContext = context.applicationContext | ||
protected val preference: SharedPreferences | ||
get() = PreferenceManager.getDefaultSharedPreferences( | ||
appContext | ||
) | ||
} |
6 changes: 6 additions & 0 deletions
6
mobile/app/src/main/java/lk/eclk/locationservice/internal/enums.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package lk.eclk.locationservice.internal | ||
|
||
enum class AuthState { | ||
LOGGED_IN, | ||
NEED_LOGIN | ||
} |
35 changes: 35 additions & 0 deletions
35
mobile/app/src/main/java/lk/eclk/locationservice/ui/signin/SignInFragment.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package lk.eclk.locationservice.ui.signin | ||
|
||
import android.os.Bundle | ||
import androidx.fragment.app.Fragment | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.lifecycle.ViewModelProvider | ||
|
||
import lk.eclk.locationservice.R | ||
import org.kodein.di.Kodein | ||
import org.kodein.di.KodeinAware | ||
import org.kodein.di.android.x.closestKodein | ||
import org.kodein.di.generic.instance | ||
|
||
class SignInFragment : Fragment(), KodeinAware { | ||
|
||
override val kodein: Kodein by closestKodein() | ||
private lateinit var viewModel: SignInViewModel | ||
private val viewModelFactory: SignInViewModelFactory by instance() | ||
|
||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View? { | ||
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) | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
mobile/app/src/main/java/lk/eclk/locationservice/ui/signin/SignInViewModel.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package lk.eclk.locationservice.ui.signin | ||
|
||
import androidx.lifecycle.ViewModel | ||
import lk.eclk.locationservice.data.Repository | ||
|
||
class SignInViewModel(private val repository: Repository) : ViewModel() { | ||
// TODO: Implement the ViewModel | ||
} |
14 changes: 14 additions & 0 deletions
14
mobile/app/src/main/java/lk/eclk/locationservice/ui/signin/SignInViewModelFactory.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package lk.eclk.locationservice.ui.signin | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import lk.eclk.locationservice.data.Repository | ||
|
||
class SignInViewModelFactory(private val repository: Repository) : | ||
ViewModelProvider.NewInstanceFactory() { | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
override fun <T : ViewModel?> create(modelClass: Class<T>): T { | ||
return SignInViewModel(repository) as T | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
mobile/app/src/main/java/lk/eclk/locationservice/ui/splash/SplashScreenFragment.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package lk.eclk.locationservice.ui.splash | ||
|
||
import android.os.Bundle | ||
import androidx.fragment.app.Fragment | ||
import android.view.LayoutInflater | ||
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 | ||
import org.kodein.di.generic.instance | ||
|
||
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?, | ||
savedInstanceState: Bundle? | ||
): View? { | ||
return inflater.inflate(R.layout.splash_screen_fragment, container, false) | ||
} | ||
|
||
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) | ||
} | ||
}) | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
mobile/app/src/main/java/lk/eclk/locationservice/ui/splash/SplashScreenViewModel.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package lk.eclk.locationservice.ui.splash | ||
|
||
import androidx.lifecycle.ViewModel | ||
import lk.eclk.locationservice.data.Repository | ||
|
||
class SplashScreenViewModel(private val repository: Repository) : ViewModel() { | ||
val authState by lazy { repository.getAuthState() } | ||
} |
14 changes: 14 additions & 0 deletions
14
mobile/app/src/main/java/lk/eclk/locationservice/ui/splash/SplashScreenViewModelFactory.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package lk.eclk.locationservice.ui.splash | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import lk.eclk.locationservice.data.Repository | ||
|
||
class SplashScreenViewModelFactory(private val repository: Repository) : | ||
ViewModelProvider.NewInstanceFactory() { | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
override fun <T : ViewModel?> create(modelClass: Class<T>): T { | ||
return SplashScreenViewModel(repository) as T | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<set xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<alpha android:fromAlpha="0" | ||
android:toAlpha="1" | ||
android:duration="300"/> | ||
</set> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<set xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<alpha android:fromAlpha="1" | ||
android:toAlpha="0" | ||
android:duration="300"/> | ||
</set> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<set xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<translate android:fromXDelta="0%" android:toXDelta="0%" | ||
android:fromYDelta="100%" android:toYDelta="0%" | ||
android:duration="300"/> | ||
</set> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<set xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<translate android:fromXDelta="-100%" android:toXDelta="0%" | ||
android:fromYDelta="0%" android:toYDelta="0%" | ||
android:duration="300"/> | ||
</set> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<set xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<translate android:fromXDelta="100%" android:toXDelta="0%" | ||
android:fromYDelta="0%" android:toYDelta="0%" | ||
android:duration="300"/> | ||
</set> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<set xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<translate android:fromXDelta="0%" android:toXDelta="0%" | ||
android:fromYDelta="0%" android:toYDelta="100%" | ||
android:duration="300"/> | ||
</set> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<set xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<translate android:fromXDelta="0%" android:toXDelta="-100%" | ||
android:fromYDelta="0%" android:toYDelta="0%" | ||
android:duration="300"/> | ||
</set> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<set xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<translate android:fromXDelta="0%" android:toXDelta="100%" | ||
android:fromYDelta="0%" android:toYDelta="0%" | ||
android:duration="300"/> | ||
</set> |
This file contains 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
Oops, something went wrong.