Skip to content

Commit

Permalink
refactored AboutUsViewmodal class to create repo
Browse files Browse the repository at this point in the history
  • Loading branch information
NiranjanNlc committed Jan 9, 2024
1 parent 212a23c commit bcfcf59
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 47 deletions.
7 changes: 6 additions & 1 deletion app/src/main/java/org/mifos/mobile/di/RepositoryModule.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.mifos.mobile.di

import android.content.Context
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import org.mifos.mobile.api.DataManager
import org.mifos.mobile.repositories.TransferRepository
Expand Down Expand Up @@ -94,5 +96,8 @@ object RepositoryModule {
fun providesReviewLoanApplicationRepository(dataManager: DataManager): ReviewLoanApplicationRepository {
return ReviewLoanApplicationRepositoryImpl(dataManager)
}

@Provides
fun providesAboutUsRepository(@ApplicationContext context: Context): AboutUsRepository {
return AboutUsRepositoryImpl(context)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.mifos.mobile.repositories

import kotlinx.coroutines.flow.Flow
import org.mifos.mobile.models.AboutUsItem


interface AboutUsRepository {
fun getAboutUsItems(): Flow<List<AboutUsItem>>
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.mifos.mobile.repositories

import android.content.Context
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import org.mifos.mobile.R
import org.mifos.mobile.models.AboutUsItem
import org.mifos.mobile.ui.enums.AboutUsListItemId
import java.util.Calendar
import javax.inject.Inject

class AboutUsRepositoryImpl @Inject constructor(
private val context: Context
) : AboutUsRepository {

override fun getAboutUsItems(): Flow<List<AboutUsItem>> = flow {
val currentYear = Calendar.getInstance().get(Calendar.YEAR)
val copyrightText = context.getString(R.string.copy_right_mifos)
.replace("%1\$s", currentYear.toString())

val aboutUsItems = listOf(
AboutUsItem(
title = context.getString(R.string.app_version_text),
itemId = AboutUsListItemId.APP_VERSION_TEXT
),
AboutUsItem(
title = context.getString(R.string.official_website),
iconUrl = R.drawable.ic_website,
itemId = AboutUsListItemId.OFFICE_WEBSITE
),
AboutUsItem(
title = context.getString(R.string.licenses),
iconUrl = R.drawable.ic_law_icon,
itemId = AboutUsListItemId.LICENSES
),
AboutUsItem(
title = context.getString(R.string.privacy_policy),
iconUrl = R.drawable.ic_privacy_policy,
itemId = AboutUsListItemId.PRIVACY_POLICY
),
AboutUsItem(
title = context.getString(R.string.sources),
iconUrl = R.drawable.ic_source_code,
itemId = AboutUsListItemId.SOURCE_CODE
),
AboutUsItem(
title = copyrightText,
subtitle = R.string.license_string_with_value,
itemId = AboutUsListItemId.LICENSES_STRING_WITH_VALUE
)
)
emit(aboutUsItems)
}
}
14 changes: 8 additions & 6 deletions app/src/main/java/org/mifos/mobile/ui/about/AboutUsScreen.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.mifos.mobile.ui.about

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
Expand All @@ -12,7 +14,7 @@ import androidx.lifecycle.viewmodel.compose.viewModel
import org.mifos.mobile.core.ui.component.AboutUsItemCard
import org.mifos.mobile.core.ui.component.MifosItemCard
import org.mifos.mobile.ui.enums.AboutUsListItemId
import java.util.*


@Composable
fun AboutUsScreen(viewModel: AboutUsViewModel) {
Expand All @@ -25,7 +27,7 @@ fun AboutUsScreen(viewModel: AboutUsViewModel) {
Spacer(modifier = Modifier.height(48.dp))
AboutUsHeader()
}
items(viewModel.aboutUsItems) { item ->
items(viewModel.aboutUsItems.value) { item ->
MifosItemCard(
modifier = Modifier.padding(bottom = 8.dp),
onClick = {
Expand Down Expand Up @@ -70,5 +72,5 @@ fun AboutUsScreen(viewModel: AboutUsViewModel) {
@Composable
@Preview
fun AboutScreenPreview() {
AboutUsScreen(AboutUsViewModel())
}
AboutUsScreen(viewModel = viewModel())
}
60 changes: 20 additions & 40 deletions app/src/main/java/org/mifos/mobile/ui/about/AboutUsViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,36 @@ package org.mifos.mobile.ui.about
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import org.mifos.mobile.MifosSelfServiceApp.Companion.context
import org.mifos.mobile.R
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
import org.mifos.mobile.models.AboutUsItem
import org.mifos.mobile.repositories.AboutUsRepository
import org.mifos.mobile.ui.enums.AboutUsListItemId
import java.util.*
import javax.inject.Inject

@HiltViewModel
class AboutUsViewModel @Inject constructor() : ViewModel() {
class AboutUsViewModel @Inject constructor(
private val aboutUsRepositoryImp: AboutUsRepository
) : ViewModel() {

private val _aboutUsItemEvent = MutableLiveData<AboutUsListItemId>()
val aboutUsItemEvent: LiveData<AboutUsListItemId> get() = _aboutUsItemEvent

private val currentYear = Calendar.getInstance().get(Calendar.YEAR)
private val copyrightText =
context?.getString(R.string.copy_right_mifos)
?.replace("%1\$s", currentYear.toString())

val aboutUsItems: List<AboutUsItem> = listOf(
AboutUsItem(
title = context?.getString(R.string.app_version_text),
itemId = AboutUsListItemId.APP_VERSION_TEXT
),
AboutUsItem(
title = context?.getString(R.string.official_website),
iconUrl = R.drawable.ic_website,
itemId = AboutUsListItemId.OFFICE_WEBSITE
),
AboutUsItem(
title = context?.getString(R.string.licenses),
iconUrl = R.drawable.ic_law_icon,
itemId = AboutUsListItemId.LICENSES
),
AboutUsItem(
title = context?.getString(R.string.privacy_policy),
iconUrl = R.drawable.ic_privacy_policy,
itemId = AboutUsListItemId.PRIVACY_POLICY
),
AboutUsItem(
title = context?.getString(R.string.sources),
iconUrl = R.drawable.ic_source_code,
itemId = AboutUsListItemId.SOURCE_CODE
),
AboutUsItem(
title = copyrightText,
subtitle = R.string.license_string_with_value,
itemId = AboutUsListItemId.LICENSES_STRING_WITH_VALUE
)
)
private val _aboutUsItems = MutableStateFlow<List<AboutUsItem>>(emptyList())

val aboutUsItems = _aboutUsItems

init {
// Launch a coroutine in the viewModelScope to collect the Flow from the repository
viewModelScope.launch {
aboutUsRepositoryImp.getAboutUsItems().collect { items ->
// Update the MutableStateFlow with the latest value from the Flow
_aboutUsItems.value = items
}
}
}

fun navigateToItem(itemId: AboutUsListItemId) {
_aboutUsItemEvent.value = itemId
Expand Down

0 comments on commit bcfcf59

Please sign in to comment.