Skip to content

Commit

Permalink
Updated Preferences.kt with new methods and details. Added support fo…
Browse files Browse the repository at this point in the history
…r emitting state using operator functions.

*moved emitting of flow to observe method.
* Added a sealed key called Key
* Moved basic key implementation to value class.
  • Loading branch information
iZakirSheikh committed Nov 20, 2022
1 parent 65ae27e commit 3648b43
Show file tree
Hide file tree
Showing 4 changed files with 383 additions and 317 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ dependencies {
implementation 'androidx.activity:activity-compose:1.6.1'
implementation project(path: ':core-ktx')
implementation project(path: ':compose-ui')
implementation project(path: ':preferences')
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
Expand Down
46 changes: 23 additions & 23 deletions app/src/main/java/com/primex/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,52 @@ import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Button
import androidx.compose.material.DropdownMenu
import androidx.compose.material.ExposedDropdownMenuBox
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AddCircle
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.runtime.Composable
import androidx.compose.runtime.ExperimentalComposeApi
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.window.Dialog
import com.primex.core.rememberState
import com.primex.preferences.IntSaver
import com.primex.preferences.Preferences
import com.primex.preferences.get
import com.primex.preferences.intPreferenceKey
import com.primex.sample.ui.theme.SampleTheme
import com.primex.ui.Label
import com.primex.ui.Button
import com.primex.ui.Preference
import com.primex.ui.SliderPreference
import com.primex.ui.dialog.BottomSheetDialog


private val KEY_COUNTER =
intPreferenceKey("counter3", defaultValue = true, saver = object : IntSaver<Boolean> {
override fun save(value: Boolean): Int = if (value) 1 else 0

override fun restore(value: Int): Boolean = value != 0
})

class MainActivity : ComponentActivity() {

private lateinit var preferences: Preferences

@OptIn(ExperimentalComposeApi::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
preferences = Preferences(this)
setContent {
SampleTheme {
var state by rememberState(initial = false)
Box(modifier = Modifier.fillMaxSize()) {

Button(onClick = { state = !state }) {
Label(text = "Click Me")
}

BottomSheetDialog(
expanded = state,
onDismissRequest = { state = false }
) {
Column {
PreviewPerf()
PreviewPerf()
PreviewPerf()
}
}
val counter by preferences[KEY_COUNTER]

Button(label = "$counter", onClick = {
preferences[KEY_COUNTER] = !counter
})

}
}
}
Expand Down
Loading

0 comments on commit 3648b43

Please sign in to comment.