Skip to content

Commit

Permalink
Refactored GroupHeader and added option for text colour. Additionally…
Browse files Browse the repository at this point in the history
…, added option for content colour in ListPref (#2)
  • Loading branch information
JamalMulla authored Jan 4, 2022
1 parent 0f1b38e commit 39755e9
Show file tree
Hide file tree
Showing 15 changed files with 116 additions and 211 deletions.
7 changes: 1 addition & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
/.idea
.DS_Store
/build
/captures
Expand Down
3 changes: 0 additions & 3 deletions .idea/.gitignore

This file was deleted.

1 change: 0 additions & 1 deletion .idea/.name

This file was deleted.

123 changes: 0 additions & 123 deletions .idea/codeStyles/Project.xml

This file was deleted.

5 changes: 0 additions & 5 deletions .idea/codeStyles/codeStyleConfig.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/compiler.xml

This file was deleted.

20 changes: 0 additions & 20 deletions .idea/gradle.xml

This file was deleted.

9 changes: 0 additions & 9 deletions .idea/misc.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

4 changes: 2 additions & 2 deletions ComposePrefs/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ android {
minSdk 21
targetSdk 31
versionCode 1
versionName "1.0"
versionName "1.0.1"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
Expand Down Expand Up @@ -54,7 +54,7 @@ afterEvaluate {

groupId = 'com.github.jamalmulla'
artifactId = 'ComposePrefs'
version = '1.0'
version = '1.0.1'
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp

@Composable
fun PrefsCategoryHeader(
title: String
fun GroupHeader(
title: String,
color: Color = MaterialTheme.colors.primary
) {
Box(
Modifier
Expand All @@ -26,7 +28,7 @@ fun PrefsCategoryHeader(
) {
Text(
title,
color = MaterialTheme.colors.primary,
color = color,
fontSize = LocalTextStyle.current.fontSize.times(FontSizeMultiplier),
fontWeight = FontWeight.SemiBold
)
Expand Down
32 changes: 30 additions & 2 deletions ComposePrefs/src/main/java/com/jamal/composeprefs/ui/PrefDsl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@ interface PrefsScope {
fun prefsItem(content: @Composable PrefsScope.() -> Unit)

/**
* Adds a group of Prefs
* Adds a group of Prefs with a title.
*
* @param title Group header text. Will be shown above the list of Prefs
* @param items All the prefs in this group
*/
fun prefsGroup(title: String, items: PrefsScope.() -> Unit)

/**
* Adds a group of Prefs. This overload is intended for passing in a [GroupHeader] if you want more control over the header.
*
* @param header Group header. Will be shown above the list of Prefs
* @param items All the prefs in this group
*/
fun prefsGroup(header: @Composable PrefsScope.() -> Unit, items: PrefsScope.() -> Unit)
}

internal class PrefsScopeImpl : PrefsScope {
Expand Down Expand Up @@ -52,12 +60,32 @@ internal class PrefsScopeImpl : PrefsScope {
// - if current current item is header of new group
// - if current item is end of group

// add header index so we know where each group starts
_headerIndexes.add(this.prefsItems.size)

this.prefsItem {
GroupHeader(title)
}

// add all children to hierarchy
this.apply(items)

this.prefsItem { Spacer(modifier = Modifier.height(16.dp)) }

// add totalSize -2/-1 to footerIndexes as that is the index of the last item added and the spacer respectively
_footerIndexes.add(this.prefsItems.size - 2)
_footerIndexes.add(this.prefsItems.size - 1)
}

override fun prefsGroup(
header: @Composable PrefsScope.() -> Unit,
items: PrefsScope.() -> Unit
) {
// add header index so we know where each group starts
_headerIndexes.add(this.prefsItems.size)

this.prefsItem {
PrefsCategoryHeader(title)
header()
}

// add all children to hierarchy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import com.jamal.composeprefs.ui.LocalPrefsDataStore
import kotlinx.coroutines.launch
import java.lang.Exception

//TODO useSelectedAsSummary

/**
* Preference that shows a list of entries in a Dialog where a single entry can be selected at one time.
*
Expand Down Expand Up @@ -48,6 +46,7 @@ fun ListPref(
onValueChange: ((String) -> Unit)? = null,
useSelectedAsSummary: Boolean = false,
dialogBackgroundColor: Color = MaterialTheme.colors.surface,
contentColor: Color = contentColorFor(dialogBackgroundColor),
textColor: Color = MaterialTheme.colors.onBackground,
enabled: Boolean = true,
entries: Map<String, String> = mapOf()
Expand Down Expand Up @@ -133,6 +132,7 @@ fun ListPref(

},
backgroundColor = dialogBackgroundColor,
contentColor = contentColor,
properties = DialogProperties(
usePlatformDefaultWidth = true
),
Expand Down
Loading

0 comments on commit 39755e9

Please sign in to comment.