Skip to content

feat: detekt implemented #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ android {
}
}



dependencies {

implementation ("org.jetbrains.kotlin:kotlin-stdlib:${versions.kotlin}")
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/monstarlab/arch/data/Repository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package com.monstarlab.arch.data
/**
* Empty for now, but could allow us to add cache helpers etc
*/
abstract class Repository constructor()
abstract class Repository
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package com.monstarlab.arch.data

import android.content.SharedPreferences
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerializationException
import kotlinx.serialization.json.Json
import timber.log.Timber
import java.io.IOException

abstract class SingleSharedPreferenceDataStore<T> constructor(
private val sharedPreferences: SharedPreferences,
Expand All @@ -16,7 +19,7 @@ abstract class SingleSharedPreferenceDataStore<T> constructor(
val json = sharedPreferences.getString(key, "") ?: ""
val entries = Json.decodeFromString(serializer, json)
entries
} catch (e: Exception) {
} catch (e: SerializationException) {
null
}
}
Expand All @@ -25,7 +28,8 @@ abstract class SingleSharedPreferenceDataStore<T> constructor(
try {
val json = Json.encodeToString(serializer, item)
sharedPreferences.edit().putString(key, json).apply()
} catch (e: Exception) {
} catch (e: SerializationException) {
Timber.e(e)
}
}

Expand Down
24 changes: 20 additions & 4 deletions app/src/main/java/com/monstarlab/arch/extensions/FlowExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ import kotlinx.coroutines.launch
* @return [Flow] that only emits items from `this` upstream flow when the [lifecycle] is at
* least in the [minActiveState].
*/

@OptIn(ExperimentalCoroutinesApi::class)
public fun <T> Flow<T>.flowWithLifecycle(
fun <T> Flow<T>.flowWithLifecycle(
lifecycle: Lifecycle,
minActiveState: Lifecycle.State = Lifecycle.State.STARTED
): Flow<T> = callbackFlow {
Expand All @@ -71,7 +72,11 @@ public fun <T> Flow<T>.flowWithLifecycle(
close()
}

fun <T1, T2> CoroutineScope.combineFlows(flow1: Flow<T1>, flow2: Flow<T2>, collectBlock: (suspend (T1, T2) -> Unit)) {
fun <T1, T2> CoroutineScope.combineFlows(
flow1: Flow<T1>,
flow2: Flow<T2>,
collectBlock: (suspend (T1, T2) -> Unit)
) {
launch {
flow1.combine(flow2) { v1, v2 ->
collectBlock.invoke(v1, v2)
Expand All @@ -81,7 +86,12 @@ fun <T1, T2> CoroutineScope.combineFlows(flow1: Flow<T1>, flow2: Flow<T2>, colle
}
}

fun <T1, T2, T3> CoroutineScope.combineFlows(flow1: Flow<T1>, flow2: Flow<T2>, flow3: Flow<T3>, collectBlock: (suspend (T1, T2, T3) -> Unit)) {
fun <T1, T2, T3> CoroutineScope.combineFlows(
flow1: Flow<T1>,
flow2: Flow<T2>,
flow3: Flow<T3>,
collectBlock: (suspend (T1, T2, T3) -> Unit)
) {
launch {
combine(flow1, flow2, flow3) { v1, v2, v3 ->
collectBlock.invoke(v1, v2, v3)
Expand All @@ -91,7 +101,13 @@ fun <T1, T2, T3> CoroutineScope.combineFlows(flow1: Flow<T1>, flow2: Flow<T2>, f
}
}

fun <T1, T2, T3, T4> CoroutineScope.combineFlows(flow1: Flow<T1>, flow2: Flow<T2>, flow3: Flow<T3>, flow4: Flow<T4>, collectBlock: (suspend (T1, T2, T3, T4) -> Unit)) {
fun <T1, T2, T3, T4> CoroutineScope.combineFlows(
flow1: Flow<T1>,
flow2: Flow<T2>,
flow3: Flow<T3>,
flow4: Flow<T4>,
collectBlock: (suspend (T1, T2, T3, T4) -> Unit)
) {
launch {
combine(flow1, flow2, flow3, flow4) { v1, v2, v3, v4 ->
collectBlock.invoke(v1, v2, v3, v4)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ class FragmentViewBindingDelegate<T : ViewBinding>(
private val fragment: Fragment,
private val viewBinder: (View) -> T,
private val disposeEvents: T.() -> Unit = {}
) : ReadOnlyProperty<Fragment, T>, LifecycleObserver {
) : ReadOnlyProperty<Fragment, T>, DefaultLifecycleObserver {

private inline fun Fragment.observeLifecycleOwnerThroughLifecycleCreation(crossinline viewOwner: LifecycleOwner.() -> Unit) {
private inline fun Fragment.observeLifecycleOwnerThroughLifecycleCreation(
crossinline viewOwner: LifecycleOwner.() -> Unit
) {
lifecycle.addObserver(object : DefaultLifecycleObserver {
override fun onCreate(owner: LifecycleOwner) {
viewLifecycleOwnerLiveData.observe(
Expand All @@ -28,8 +30,12 @@ class FragmentViewBindingDelegate<T : ViewBinding>(

private var fragmentBinding: T? = null

@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun disposeBinding() {
override fun onDestroy(owner: LifecycleOwner) {
disposeBinding()
super.onDestroy(owner)
}

private fun disposeBinding() {
fragmentBinding?.disposeEvents()
fragmentBinding = null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public fun LifecycleOwner.addRepeatingJob(
* again.
* @param block The block to run when the lifecycle is at least in [state] state.
*/
@Suppress("LongMethod")
public suspend fun Lifecycle.repeatOnLifecycle(
state: Lifecycle.State,
block: suspend CoroutineScope.() -> Unit
Expand Down Expand Up @@ -138,4 +139,4 @@ public suspend fun Lifecycle.repeatOnLifecycle(
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ suspend inline fun <T> safeUseCase(
UseCaseResult.Error(e.toError())
}

@Suppress("TooGenericExceptionCaught")
inline fun <T> useCaseFlow(
crossinline block: suspend () -> T,
): Flow<UseCaseResult<T>> = flow {
Expand All @@ -36,25 +37,28 @@ inline fun <T> useCaseFlow(
}
}

fun <T> observableFlow(block: suspend FlowCollector<T>.() -> Unit): Flow<UseCaseResult<T>> = flow(block)
.catch { exception ->
Timber.e(exception)
UseCaseResult.Error(exception.toError())
}
.map {
UseCaseResult.Success(it)
}
fun <T> observableFlow(block: suspend FlowCollector<T>.() -> Unit): Flow<UseCaseResult<T>> =
flow(block)
.catch { exception ->
Timber.e(exception)
UseCaseResult.Error(exception.toError())
}
.map {
UseCaseResult.Success(it)
}

fun <T> Flow<UseCaseResult<T>>.onSuccess(action: suspend (T) -> Unit): Flow<UseCaseResult<T>> = transform { result ->
if (result is UseCaseResult.Success<T>) {
action(result.value)
fun <T> Flow<UseCaseResult<T>>.onSuccess(action: suspend (T) -> Unit): Flow<UseCaseResult<T>> =
transform { result ->
if (result is UseCaseResult.Success<T>) {
action(result.value)
}
return@transform emit(result)
}
return@transform emit(result)
}

fun <T> Flow<UseCaseResult<T>>.onError(action: suspend (ErrorModel) -> Unit): Flow<UseCaseResult<T>> = transform { result ->
if (result is UseCaseResult.Error) {
action(result.error)
fun <T> Flow<UseCaseResult<T>>.onError(action: suspend (ErrorModel) -> Unit): Flow<UseCaseResult<T>> =
transform { result ->
if (result is UseCaseResult.Error) {
action(result.error)
}
return@transform emit(result)
}
return@transform emit(result)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.monstarlab.core.data.network.dtos

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
Expand All @@ -8,5 +9,6 @@ data class ResourceDto(
val name: String,
val year: Int,
val color: String,
val pantone_value: String
@SerialName("pantone_value")
val pantoneValue: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ fun ResourceDto.toEntity(): Resource {
name = name,
year = year,
color = color,
pantoneValue = pantone_value
pantoneValue = pantoneValue
)
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.monstarlab.core.data.network.dtos

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class UserDto(
val id: Int,
val email: String,
val first_name: String,
val last_name: String,
@SerialName("first_name")
val firstName: String,
@SerialName("last_name")
val lastName: String,
val avatar: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import com.monstarlab.core.domain.model.User
fun UserDto.toUser(): User {
return User(
email = email,
firstName = first_name,
lastName = last_name,
firstName = firstName,
lastName = lastName,
avatar = avatar
)
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.monstarlab.core.data.network.responses

import com.monstarlab.core.data.network.dtos.ResourceDto
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class ResourcesResponse(
val page: Int,
val per_page: Int,
@SerialName("per_page")
val perPage: Int,
val total: Int,
val total_pages: Int,
@SerialName("total_pages")
val totalPages: Int,
val data: List<ResourceDto>
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import java.io.IOException
import java.net.SocketTimeoutException
import java.net.UnknownHostException

@Suppress("MagicNumber")
fun <T> Response<T>.toError(): ErrorModel.Http {
return when {
code() == 400 -> ErrorModel.Http.BadRequest
Expand Down
21 changes: 0 additions & 21 deletions app/src/main/java/com/monstarlab/core/domain/mock/MockFlows.kt

This file was deleted.

11 changes: 0 additions & 11 deletions app/src/main/java/com/monstarlab/core/domain/mock/MockSuspends.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import com.google.android.material.snackbar.Snackbar
import com.monstarlab.core.domain.error.ErrorModel
import javax.inject.Inject

fun Fragment.showErrorDialog(error: ViewError, cancelable: Boolean = true, dismissAction: (() -> Unit)? = null) {
fun Fragment.showErrorDialog(
error: ViewError,
cancelable: Boolean = true,
dismissAction: (() -> Unit)? = null
) {
val builder = AlertDialog.Builder(requireContext())
builder.setTitle(error.title)
builder.setMessage(error.message)
Expand All @@ -26,7 +30,12 @@ fun Fragment.showErrorDialog(error: ViewError, cancelable: Boolean = true, dismi
}
}

fun Fragment.showErrorSnackbar(view: View, error: ViewError, showAction: Boolean = false, dismissAction: (() -> Unit)? = null) {
fun Fragment.showErrorSnackbar(
view: View,
error: ViewError,
showAction: Boolean = false,
dismissAction: (() -> Unit)? = null,
) {
val showLength = if (showAction) Snackbar.LENGTH_INDEFINITE else Snackbar.LENGTH_LONG
val snackbar = Snackbar.make(view, error.message, showLength)
if (showAction) {
Expand All @@ -41,6 +50,7 @@ fun Fragment.showErrorSnackbar(view: View, error: ViewError, showAction: Boolean
}
}

@Suppress("LongMethod")
fun ErrorModel.mapToViewError(): ViewError {
return when (this) {
is ErrorModel.Http.Forbidden,
Expand Down
18 changes: 10 additions & 8 deletions app/src/main/java/com/monstarlab/features/login/LoginFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ import androidx.lifecycle.viewErrorFlow
import androidx.navigation.fragment.findNavController
import androidx.transition.TransitionManager
import com.monstarlab.R
import com.monstarlab.arch.extensions.collectFlow
import com.monstarlab.arch.extensions.onClick
import com.monstarlab.arch.extensions.snackErrorFlow
import com.monstarlab.arch.extensions.viewBinding
import com.monstarlab.arch.extensions.visibilityFlow
import com.monstarlab.arch.extensions.*
import com.monstarlab.databinding.FragmentLoginBinding
import dagger.hilt.android.AndroidEntryPoint

Expand All @@ -26,26 +22,32 @@ class LoginFragment : Fragment(R.layout.fragment_login) {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
bindEvent()
collectFlows()
}

private fun bindEvent() {
binding.loginButton.onClick {
viewModel.login(
binding.loginEmailEditText.text.toString(),
binding.loginPasswordEditText.text.toString()
)
}
}

private fun collectFlows() {
collectFlow(viewModel.loginResultFlow) {
findNavController().navigate(R.id.resourceFragment)
}

snackErrorFlow(viewModel.viewErrorFlow, view)
visibilityFlow(viewModel.loadingFlow, binding.loginProgressBar)

collectFlow(viewModel.loadingFlow) { loading ->
TransitionManager.beginDelayedTransition(binding.root)
binding.loginEmailEditText.isEnabled = !loading
binding.loginPasswordEditText.isEnabled = !loading
binding.loginButton.isVisible = !loading
}

snackErrorFlow(viewModel.viewErrorFlow, binding.root)
visibilityFlow(viewModel.loadingFlow, binding.loginProgressBar)
}
}
Loading