Skip to content

Commit

Permalink
Hilt and Shared Preference Setup
Browse files Browse the repository at this point in the history
  • Loading branch information
roshanrai06 committed Jan 28, 2022
1 parent d14ab59 commit 27e9726
Show file tree
Hide file tree
Showing 17 changed files with 366 additions and 21 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package="com.roshan.calorietracker">

<application
android:name=".CalorieTrackerApplication"
android:allowBackup="true"
android:fullBackupContent="true"
android:icon="@mipmap/ic_launcher"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.roshan.calorietracker

import android.app.Application
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class CalorieTrackerApplication : Application() {
}
58 changes: 38 additions & 20 deletions app/src/main/java/com/roshan/calorietracker/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,54 @@ package com.roshan.calorietracker
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.roshan.calorietracker.navigation.navigate
import com.roshan.calorietracker.ui.theme.CalorieTrackerTheme
import com.roshan.core.navigation.Route
import com.roshan.onboarding_presentation.welcome.WelcomeScreen

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
CalorieTrackerTheme {
// A surface container using the 'background' color from the theme
Surface(color = MaterialTheme.colors.background) {
Greeting("Android")
val navController = rememberNavController()
NavHost(navController = navController, startDestination = Route.WELCOME) {
composable(Route.WELCOME) {
WelcomeScreen(onNavigate = navController::navigate)
}
composable(Route.AGE) {

}
composable(Route.GENDER) {

}
composable(Route.HEIGHT) {

}
composable(Route.WEIGHT) {

}
composable(Route.NUTRIENT_GOAL) {

}
composable(Route.ACTIVITY) {

}
composable(Route.GOAL) {

}
composable(Route.TRACKER_OVERVIEW) {

}
composable(Route.SEARCH) {

}
}
}
}
}
}

@Composable
fun Greeting(name: String) {
Text(text = "Hello $name!")
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
CalorieTrackerTheme {
Greeting("Android")
}
}
28 changes: 28 additions & 0 deletions app/src/main/java/com/roshan/calorietracker/di/AppModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.roshan.calorietracker.di

import android.app.Application
import android.content.Context.MODE_PRIVATE
import android.content.SharedPreferences
import com.roshan.core.domain.DefaultPreferences
import com.roshan.core.domain.preferences.Preferences
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object AppModule {
@Provides
@Singleton
fun providesSharedPreferences(app: Application): SharedPreferences {
return app.getSharedPreferences("shared_pref", MODE_PRIVATE)
}

@Provides
@Singleton
fun providesPreferences(sharedPreferences: SharedPreferences): Preferences {
return DefaultPreferences(sharedPreferences)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.roshan.calorietracker.navigation

import androidx.navigation.NavController
import com.roshan.core.util.UiEvent

fun NavController.navigate(event: UiEvent.Navigate) {
this.navigate(event.route)
}
89 changes: 89 additions & 0 deletions core/src/main/java/com/roshan/core/domain/DefaultPreferences.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.roshan.core.domain

import android.content.SharedPreferences
import com.roshan.core.domain.model.ActivityLevel
import com.roshan.core.domain.model.Gender
import com.roshan.core.domain.model.GoalType
import com.roshan.core.domain.model.UserInfo
import com.roshan.core.domain.preferences.Preferences

class DefaultPreferences(private val sharedPref: SharedPreferences) : Preferences {
override fun saveGender(gender: Gender) {
sharedPref.edit()
.putString(Preferences.KEY_GENDER, gender.name)
.apply()
}

override fun saveAge(age: Int) {
sharedPref.edit()
.putInt(Preferences.KEY_AGE, age)
.apply()
}

override fun saveWeight(weight: Float) {
sharedPref.edit()
.putFloat(Preferences.KEY_WEIGHT, weight)
.apply()
}

override fun saveHeight(height: Int) {
sharedPref.edit()
.putInt(Preferences.KEY_HEIGHT, height)
.apply()
}

override fun saveActivityLevel(level: ActivityLevel) {
sharedPref.edit()
.putString(Preferences.KEY_ACTIVITY_LEVEL, level.name)
.apply()
}

override fun saveGoalType(type: GoalType) {
sharedPref.edit()
.putString(Preferences.KEY_GOAL_TYPE, type.name)
.apply()
}

override fun saveCarbRatio(ratio: Float) {
sharedPref.edit()
.putFloat(Preferences.KEY_CARB_RATIO, ratio)
.apply()
}

override fun saveProteinRatio(ratio: Float) {
sharedPref.edit()
.putFloat(Preferences.KEY_PROTEIN_RATIO, ratio)
.apply()
}

override fun saveFatRatio(ratio: Float) {
sharedPref.edit()
.putFloat(Preferences.KEY_FAT_RATIO, ratio)
.apply()
}

override fun loadUserInfo(): UserInfo {
val age = sharedPref.getInt(Preferences.KEY_AGE, -1)
val height = sharedPref.getInt(Preferences.KEY_HEIGHT, -1)
val weight = sharedPref.getFloat(Preferences.KEY_WEIGHT, -1f)
val genderString = sharedPref.getString(Preferences.KEY_GENDER, null)
val activityLevelString = sharedPref
.getString(Preferences.KEY_ACTIVITY_LEVEL, null)
val goalType = sharedPref.getString(Preferences.KEY_GOAL_TYPE, null)
val carbRatio = sharedPref.getFloat(Preferences.KEY_CARB_RATIO, -1f)
val proteinRatio = sharedPref.getFloat(Preferences.KEY_PROTEIN_RATIO, -1f)
val fatRatio = sharedPref.getFloat(Preferences.KEY_FAT_RATIO, -1f)

return UserInfo(
gender = Gender.fromString(genderString ?: "male"),
age = age,
weight = weight,
height = height,
activityLevel = ActivityLevel.fromString(activityLevelString ?: "medium"),
goalType = GoalType.fromString(goalType ?: "keep_weight"),
carbRatio = carbRatio,
proteinRatio = proteinRatio,
fatRatio = fatRatio
)
}
}
18 changes: 18 additions & 0 deletions core/src/main/java/com/roshan/core/domain/model/ActivityLevel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.roshan.core.domain.model

sealed class ActivityLevel(val name: String) {
object Low : ActivityLevel("low")
object High : ActivityLevel("high")
object Medium : ActivityLevel("medium")

companion object {
fun fromString(name: String): ActivityLevel {
return when (name) {
"low" -> Low
"high" -> High
"medium" -> Medium
else -> Medium
}
}
}
}
15 changes: 15 additions & 0 deletions core/src/main/java/com/roshan/core/domain/model/Gender.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.roshan.core.domain.model

sealed class Gender(val name: String) {
object Male : Gender("male")
object Female : Gender("female")
companion object {
fun fromString(name: String): Gender {
return when (name) {
"male" -> Male
"female" -> Female
else -> Female
}
}
}
}
19 changes: 19 additions & 0 deletions core/src/main/java/com/roshan/core/domain/model/GoalType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.roshan.core.domain.model

sealed class GoalType(val name: String) {
object LoseWeight : GoalType("lose_weight")
object KeepWeight : GoalType("keep_weight")
object GainWeight : GoalType("gain_weight")

companion object {
fun fromString(name: String): GoalType {
return when (name) {
"lose_weight" -> LoseWeight
"gain_weight" -> GainWeight
"keep_weight" -> KeepWeight
else -> KeepWeight
}
}
}

}
13 changes: 13 additions & 0 deletions core/src/main/java/com/roshan/core/domain/model/UserInfo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.roshan.core.domain.model

data class UserInfo(
val gender: Gender,
val age: Int,
val weight: Float,
val height:Int,
val activityLevel: ActivityLevel,
val goalType: GoalType,
val fatRatio: Float,
val carbRatio: Float,
val proteinRatio: Float
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.roshan.core.domain.preferences

import com.roshan.core.domain.model.ActivityLevel
import com.roshan.core.domain.model.Gender
import com.roshan.core.domain.model.GoalType
import com.roshan.core.domain.model.UserInfo

interface Preferences {
fun saveGender(gender: Gender)
fun saveAge(age: Int)
fun saveWeight(weight: Float)
fun saveHeight(height: Int)
fun saveActivityLevel(level: ActivityLevel)
fun saveGoalType(type: GoalType)
fun saveCarbRatio(ratio: Float)
fun saveProteinRatio(ratio: Float)
fun saveFatRatio(ratio: Float)

fun loadUserInfo(): UserInfo

companion object {
const val KEY_GENDER = "gender"
const val KEY_AGE = "age"
const val KEY_WEIGHT = "weight"
const val KEY_HEIGHT = "height"
const val KEY_ACTIVITY_LEVEL = "activity_level"
const val KEY_GOAL_TYPE = "goal_type"
const val KEY_CARB_RATIO = "carb_ratio"
const val KEY_PROTEIN_RATIO = "protein_ratio"
const val KEY_FAT_RATIO = "fat_ratio"
}
}
15 changes: 15 additions & 0 deletions core/src/main/java/com/roshan/core/navigation/Route.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.roshan.core.navigation

object Route {
const val WELCOME = "welcome"
const val AGE = "age"
const val GENDER = "gender"
const val HEIGHT = "height"
const val WEIGHT = "weight"
const val NUTRIENT_GOAL = "nutrient_goal"
const val ACTIVITY = "activity"
const val GOAL = "goal"

const val TRACKER_OVERVIEW = "tracker_overview"
const val SEARCH = "search"
}
6 changes: 6 additions & 0 deletions core/src/main/java/com/roshan/core/util/UiEvent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.roshan.core.util

sealed class UiEvent {
data class Navigate(val route: String) : UiEvent()
object NavigateUp : UiEvent()
}
File renamed without changes.
2 changes: 1 addition & 1 deletion onboarding/onboarding_presentation/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apply {
from("$rootDir/base-module.gradle")
from("$rootDir/compose-module.gradle")
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.roshan.onboarding_presentation.components

import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import com.roshan.core_ui.LocalSpacing

@Composable
fun ActionButton(
text: String,
onClick: () -> Unit,
modifier: Modifier,
isEnabled: Boolean = true,
textStyle: TextStyle = MaterialTheme.typography.button
) {
Button(
onClick = onClick,
modifier = modifier,
enabled = isEnabled,
shape = RoundedCornerShape(100.dp)
) {
Text(
text = text,
style = textStyle,
color = MaterialTheme.colors.onPrimary,
modifier = Modifier.padding(LocalSpacing.current.spaceSmall)
)

}
}
Loading

0 comments on commit 27e9726

Please sign in to comment.