Skip to content

Commit 2b68915

Browse files
committed
Merge branch 'dev'
2 parents 52bb9a6 + a5d16d8 commit 2b68915

32 files changed

+424
-439
lines changed

app/src/main/kotlin/com/aykuttasil/modernapp/di/modules/AppModule.kt

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,3 @@
1-
/**
2-
* Designed and developed by Aykut Asil (@aykuttasil)
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
161
package com.aykuttasil.modernapp.di.modules
172

183
import android.app.Application
@@ -23,7 +8,7 @@ import dagger.hilt.InstallIn
238
import dagger.hilt.android.components.ApplicationComponent
249
import javax.inject.Singleton
2510

26-
@Module
11+
@Module(includes = [ImplModule::class])
2712
@InstallIn(ApplicationComponent::class)
2813
object AppModule {
2914

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.aykuttasil.modernapp.di.modules
2+
3+
import com.aykuttasil.domain.util.AppExecutors
4+
import com.aykuttasil.domain.util.DispatcherProvider
5+
import com.aykuttasil.modernapp.util.AppExecutorsImpl
6+
import com.aykuttasil.modernapp.util.DispatcherProviderImp
7+
import dagger.Binds
8+
import dagger.Module
9+
import dagger.hilt.InstallIn
10+
import dagger.hilt.android.components.ApplicationComponent
11+
import javax.inject.Singleton
12+
13+
@Module
14+
@InstallIn(ApplicationComponent::class)
15+
abstract class ImplModule {
16+
17+
@Singleton
18+
@Binds
19+
abstract fun provideDispatcherProvider(
20+
dispatcherProvider: DispatcherProviderImp
21+
): DispatcherProvider
22+
23+
@Singleton
24+
@Binds
25+
abstract fun provideAppExecutors(
26+
executors: AppExecutorsImpl
27+
): AppExecutors
28+
29+
}

app/src/main/kotlin/com/aykuttasil/modernapp/ui/common/DataBoundListAdapter.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import androidx.databinding.ViewDataBinding
2020
import androidx.recyclerview.widget.AsyncDifferConfig
2121
import androidx.recyclerview.widget.DiffUtil
2222
import androidx.recyclerview.widget.ListAdapter
23-
import com.aykuttasil.modernapp.util.AppExecutors
23+
import com.aykuttasil.domain.util.AppExecutors
2424

2525
/**
2626
* A generic RecyclerView adapter that uses Data Binding & DiffUtil.
@@ -33,7 +33,7 @@ abstract class DataBoundListAdapter<T, V : ViewDataBinding>(
3333
diffCallback: DiffUtil.ItemCallback<T>
3434
) : ListAdapter<T, DataBoundViewHolder<V>>(
3535
AsyncDifferConfig.Builder<T>(diffCallback)
36-
.setBackgroundThreadExecutor(appExecutors.diskIO())
36+
.setBackgroundThreadExecutor(appExecutors.diskIO)
3737
.build()
3838
) {
3939
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DataBoundViewHolder<V> {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.aykuttasil.modernapp.ui.login
2+
3+
/**
4+
* Data validation state of the login form.
5+
*/
6+
data class LoginFormState(
7+
val usernameError: String? = null,
8+
val passwordError: String? = null,
9+
val isDataValid: Boolean = false
10+
)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.aykuttasil.modernapp.ui.login
2+
3+
import android.os.Bundle
4+
import android.text.Editable
5+
import android.text.TextWatcher
6+
import androidx.fragment.app.viewModels
7+
import androidx.lifecycle.Observer
8+
import com.aykuttasil.modernapp.ui.common.BaseFragment
9+
import dagger.hilt.android.AndroidEntryPoint
10+
11+
@AndroidEntryPoint
12+
class LoginFragment : BaseFragment() {
13+
14+
private val viewModel by viewModels<LoginViewModel>()
15+
16+
override fun onActivityCreated(savedInstanceState: Bundle?) {
17+
super.onActivityCreated(savedInstanceState)
18+
19+
viewModel.liveLoginFormState.observe(viewLifecycleOwner, Observer {
20+
21+
})
22+
}
23+
24+
val afterTextChangedListener = object : TextWatcher {
25+
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
26+
// ignore
27+
}
28+
29+
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
30+
// ignore
31+
}
32+
33+
override fun afterTextChanged(s: Editable) {
34+
viewModel.loginDataChanged(
35+
"", ""
36+
// usernameEditText.text.toString(),
37+
// passwordEditText.text.toString()
38+
)
39+
}
40+
}
41+
42+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.aykuttasil.modernapp.ui.login
2+
3+
import com.aykuttasil.domain.entities.UserEntity
4+
5+
/**
6+
* Authentication result : success (user details) or error message.
7+
*/
8+
data class LoginResult(
9+
val success: UserEntity? = null,
10+
val errorMessage: String? = null
11+
)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.aykuttasil.modernapp.ui.login
2+
3+
import android.util.Patterns
4+
import androidx.hilt.lifecycle.ViewModelInject
5+
import androidx.lifecycle.LiveData
6+
import androidx.lifecycle.MutableLiveData
7+
import androidx.lifecycle.viewModelScope
8+
import com.aykuttasil.domain.usecases.user.LoginParams
9+
import com.aykuttasil.domain.usecases.user.LoginUseCase
10+
import com.aykuttasil.domain.util.Resource
11+
import com.aykuttasil.modernapp.App
12+
import com.aykuttasil.modernapp.ui.common.BaseViewModel
13+
import kotlinx.coroutines.ExperimentalCoroutinesApi
14+
import kotlinx.coroutines.flow.onEach
15+
import kotlinx.coroutines.launch
16+
17+
@ExperimentalCoroutinesApi
18+
class LoginViewModel @ViewModelInject constructor(
19+
val app: App,
20+
val loginUseCase: LoginUseCase
21+
) : BaseViewModel(app) {
22+
23+
private val _loginForm = MutableLiveData<LoginFormState>()
24+
val liveLoginFormState: LiveData<LoginFormState> = _loginForm
25+
26+
private val _loginResult = MutableLiveData<LoginResult>()
27+
val liveLoginResult: LiveData<LoginResult> = _loginResult
28+
29+
fun login(username: String, password: String) {
30+
loginDataChanged(username, password)
31+
val isValid = liveLoginFormState.value?.isDataValid ?: false
32+
33+
viewModelScope.launch {
34+
loginUseCase(LoginParams(username, password)) { state ->
35+
state.onEach {
36+
when (it) {
37+
is Resource.Success -> {
38+
_loginResult.value =
39+
LoginResult(success = it.data)
40+
}
41+
is Resource.Error -> {
42+
_loginResult.value = LoginResult(errorMessage = it.error?.message ?: "HATA")
43+
}
44+
is Resource.Loading -> {
45+
46+
}
47+
}
48+
}
49+
}
50+
}
51+
}
52+
53+
fun loginDataChanged(username: String, password: String) {
54+
if (!isUserNameValid(username)) {
55+
_loginForm.value = LoginFormState(usernameError = "Invalid username")
56+
} else if (!isPasswordValid(password)) {
57+
_loginForm.value = LoginFormState(passwordError = "Invalid password")
58+
} else {
59+
_loginForm.value = LoginFormState(isDataValid = true)
60+
}
61+
}
62+
63+
// A placeholder username validation check
64+
private fun isUserNameValid(username: String): Boolean {
65+
return if (username.contains("@")) {
66+
Patterns.EMAIL_ADDRESS.matcher(username).matches()
67+
} else {
68+
username.isNotBlank()
69+
}
70+
}
71+
72+
// A placeholder password validation check
73+
private fun isPasswordValid(password: String): Boolean {
74+
return password.length > 5
75+
}
76+
77+
78+
}

app/src/main/kotlin/com/aykuttasil/modernapp/ui/user/UserActivity.kt

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ import com.aykuttasil.modernapp.ui.common.BaseActivity
2525
import com.aykuttasil.modernapp.util.delegates.contentView
2626
import com.aykuttasil.domain.util.logd
2727
import dagger.hilt.android.AndroidEntryPoint
28+
import kotlinx.coroutines.ExperimentalCoroutinesApi
2829

30+
@ExperimentalCoroutinesApi
2931
@AndroidEntryPoint
3032
class UserActivity : BaseActivity() {
3133

@@ -37,27 +39,10 @@ class UserActivity : BaseActivity() {
3739
super.onCreate(savedInstanceState)
3840
logd { "onCreate" }
3941

40-
/*
41-
viewModel.getUser().observe(this, Observer {
42-
when (it) {
43-
is Resource.Loading -> {
44-
Toast.makeText(this, "Loading...", Toast.LENGTH_SHORT).show()
45-
}
46-
is Resource.Success -> {
47-
Toast.makeText(this, it.data?.userEmail, Toast.LENGTH_SHORT).show()
48-
}
49-
is Resource.Error -> {
50-
Toast.makeText(this, it.throwable?.message, Toast.LENGTH_SHORT).show()
51-
}
52-
}
53-
})
54-
55-
*/
56-
5742
viewModel.viewState.observe(this, Observer {
5843
if (it.isLoading) {
5944
Toast.makeText(this, "Lütfen Bekleyiniz", Toast.LENGTH_SHORT).show()
60-
} else {
45+
} else if (it.userEntity != null) {
6146
Toast.makeText(this, "İşlem Tamamlandı", Toast.LENGTH_SHORT).show()
6247
Toast.makeText(this, it.userEntity?.userName, Toast.LENGTH_SHORT).show()
6348
}

app/src/main/kotlin/com/aykuttasil/modernapp/ui/user/UserActivityModule.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import dagger.Provides
2121
import dagger.hilt.InstallIn
2222
import dagger.hilt.android.components.ActivityComponent
2323
import dagger.hilt.android.qualifiers.ActivityContext
24+
import kotlinx.coroutines.ExperimentalCoroutinesApi
2425

26+
@ExperimentalCoroutinesApi
2527
@Module
2628
@InstallIn(ActivityComponent::class)
2729
class UserActivityModule {

app/src/main/kotlin/com/aykuttasil/modernapp/ui/user/UserViewModel.kt

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,25 @@ import androidx.hilt.Assisted
1919
import androidx.hilt.lifecycle.ViewModelInject
2020
import androidx.lifecycle.MutableLiveData
2121
import androidx.lifecycle.SavedStateHandle
22-
import androidx.lifecycle.liveData
2322
import androidx.lifecycle.viewModelScope
2423
import com.aykuttasil.modernapp.util.SingleLiveEvent
2524
import com.aykuttasil.domain.entities.UserEntity
2625
import com.aykuttasil.domain.usecases.user.GetUserUseCase
27-
import com.aykuttasil.domain.util.Result
26+
import com.aykuttasil.domain.util.Resource
2827
import com.aykuttasil.modernapp.App
2928
import com.aykuttasil.modernapp.ui.common.BaseViewModel
30-
import kotlinx.coroutines.delay
29+
import kotlinx.coroutines.ExperimentalCoroutinesApi
30+
import kotlinx.coroutines.flow.launchIn
31+
import kotlinx.coroutines.flow.onEach
3132
import kotlinx.coroutines.launch
32-
import timber.log.Timber
3333

34+
data class UserActivityViewState(
35+
var isLoading: Boolean = true,
36+
var userEntity: UserEntity? = null,
37+
var error: String? = null
38+
)
39+
40+
@ExperimentalCoroutinesApi
3441
class UserViewModel @ViewModelInject constructor(
3542
@Assisted private val savedStateHandle: SavedStateHandle,
3643
private val app: App,
@@ -48,38 +55,23 @@ class UserViewModel @ViewModelInject constructor(
4855

4956
fun getUser() {
5057
viewModelScope.launch {
51-
try {
52-
viewState.value = viewState.value?.copy(isLoading = true)
53-
delay(1000)
54-
55-
getUserUseCase("aykuttasil123") {
58+
getUserUseCase("aykuttasil") { state ->
59+
state.onEach {
5660
when (it) {
57-
is Result.Success -> {
58-
viewState.value = viewState.value?.copy(isLoading = false, userEntity = it.data)
61+
is Resource.Loading -> {
62+
viewState.value = viewState.value?.copy(isLoading = true)
5963
}
60-
is Result.Error -> {
61-
Timber.e(it.msg)
62-
63-
viewState.value =
64-
viewState.value?.copy(
65-
isLoading = true,
66-
userEntity = UserEntity(userName = "HOHOHOHO")
67-
)
64+
is Resource.Success -> {
65+
viewState.value = viewState.value?.copy(isLoading = false, userEntity = it.data)
6866
}
69-
else -> {
67+
is Resource.Error -> {
68+
errorState.value = it.error
7069
}
7170
}
72-
}
73-
} catch (ex: Exception) {
74-
errorState.value = ex
71+
}.launchIn(viewModelScope)
72+
7573
}
7674
}
7775
}
7876

7977
}
80-
81-
data class UserActivityViewState(
82-
var isLoading: Boolean = true,
83-
var userEntity: UserEntity? = null,
84-
var error: String? = null
85-
)

0 commit comments

Comments
 (0)