Skip to content

Commit f426cf5

Browse files
committed
update README and version number
1 parent 372f74c commit f426cf5

2 files changed

Lines changed: 64 additions & 5 deletions

File tree

README.md

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
AndroidUtils is a collection of util classes that I use in basically every project - I might as well give them their own module. This is more for personal use (I don't expect most people to use this as a dependency in their own projects, other than stealing a snippet or two), but feel free to add your own contributions as you wish.
1+
AndroidUtils is a collection of util classes that I use in almost every project - I might as well give them their own module. This is more for personal use (I don't expect most people to use this as a dependency in their own projects, other than stealing a snippet or two), but feel free to add your own contributions as you wish.
22

33
[![](https://jitpack.io/v/me.jfenn/AndroidUtils.svg)](https://jitpack.io/#me.jfenn/AndroidUtils)
44

@@ -20,9 +20,68 @@ allprojects {
2020
To add the dependency, copy this line into your app module's build.gradle file.
2121

2222
```gradle
23-
implementation 'me.jfenn:AndroidUtils:0.0.2'
23+
implementation 'me.jfenn:AndroidUtils:0.0.5'
2424
```
2525

2626
### Methods
2727

28-
Most of these methods are fairly self-explanatory in terms of what they actually do. As I'm lazy, you can find auto-generated docs for all of them [here](https://jfenn.me/projects/androidutils/docs), or just browse the source code to see what they do.
28+
#### ViewUtils
29+
30+
Various extension functions/delegates to safely reference view ids in activities, fragments, and other Android components.
31+
32+
```kotlin
33+
val button: MaterialButton? = findView(R.id.button)
34+
val recyclerView: RecyclerView? by bind(R.id.recycler)
35+
```
36+
37+
Also: `View.setBackgroundTint(color)` will wrap & tint a view's background drawable.
38+
39+
#### Prefs
40+
41+
A bunch of generic-typed preference utility functions.
42+
43+
```kotlin
44+
// editing a SharedPreferences object (automatically runs .apply())
45+
sharedPrefs.edit {
46+
putInt("themeKey", THEME_DARK)
47+
}
48+
49+
// setter/getter operator functions
50+
val theme: Int = sharedPrefs["themeKey"]
51+
sharedPrefs["themeKey"] = THEME_LIGHT
52+
```
53+
54+
Property delegates can be used to greatly simplify SharedPreferences operations.
55+
56+
```kotlin
57+
// binding a single property
58+
val theme by sharedPrefs.get<Int>("themeKey", defaultValue = THEME_LIGHT)
59+
60+
// uses the delegated property name ("themeKey") if a key is not provided
61+
val themeKey by sharedPrefs.get<Int>(defaultValue = THEME_LIGHT)
62+
```
63+
64+
##### TypedPreference
65+
66+
TypedPreference objects can be used to create a set of typed "pointers" to reference throughout your project, instead of relying on a hardcoded preference key/string.
67+
68+
```kotlin
69+
// constructs a TypedPreference<Int> with the key "PREF_THEME"
70+
val PREF_THEME by pref<Int>(defaultValue = THEME_LIGHT)
71+
72+
// in another file...
73+
var theme by sharedPrefs.get(PREF_THEME)
74+
75+
// alternatively, in an activity/fragment/etc (if using PreferenceManager.getDefaultSharedPreferences)
76+
var theme by prefs(PREF_THEME)
77+
```
78+
79+
#### DimenUtils
80+
81+
Contains two `Context` extension functions: `getStatusBarHeight()` and `getNavigationBarHeight()`, that both work accordingly.
82+
83+
Also includes some unit conversion functions that use `Resources.getSystem()` to find the display density:
84+
- `dpToPx(Float) : Int`
85+
- `spToPx(Float) : Int`
86+
- `pxToDp(Int) : Float`
87+
- `pxToSp(Int) : Float`

androidutils/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ android {
66
defaultConfig {
77
minSdkVersion 14
88
targetSdkVersion 29
9-
versionCode 4
10-
versionName "0.0.4"
9+
versionCode 5
10+
versionName "0.0.5"
1111
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1212
}
1313
buildTypes {

0 commit comments

Comments
 (0)