Code Monkey home page Code Monkey logo

bulldog's Introduction

Bulldog

Please check https://sserra.gitbook.io/bulldog/ for full documentation.

Medium https://medium.com/@Sserra90/announcing-bulldog-never-use-sharedpreferences-anymore-92cd32249314

Why Bulldog?

  • Avoid boilerplate, error prone code, improve productivity.
  • Write less code, avoid bugs.
  • Easily organize your preferences.

Motivation

Android library to simplify reading and writing to SharedPreferences, never write code like this anymore prefs.edit().putString("someKey","someString").apply()

How to use it

Add dependencies to build.gradle

 kapt "com.github.Sserra90.bulldog:bulldog-processor:$bulldog"
 implementation "com.github.Sserra90.bulldog:bulldog-runtime:$bulldog"

Create a spec with wanted properties and default values like the this:

@Bulldog(name = "UserSettings")
object UserModel {
    const val id: Int = 20 // Default value
    const val email: String = "[email protected]"
    const val likes: Long = 20L
    const val isPremium: Boolean = false
    const val minutesLeft: Float = 24.5F
}

Init Bulldog context in your application class.

bullDogCtx = applicationContext

Bulldog will generate a class from this specification with the name UserSettings. Use it like a normal object to access values, under the hood it uses Kotlin delegated properties to read and writes values to SharedPreferences.

UserSettings().apply {
    id = 2
    email = "[email protected]"
}
Log.d("PREFS", UserSettings().id)
Log.d("PREFS", UserSettings().toString())

If no name is specified in @Bulldog annotation, the generated class will have the name of the spec object prefixed with Bulldog.

Read values

Just access object property like a normal object

Log.d("UserId", UserSettings().id)

Write values

UserSettings().apply{
    id = 4
    Log.d("UserId", id)
}

Clear values

Bulldog generates a clear method for each entry

UserSettings().apply{
    clearId()
    Log.d("UserId", id) // Will return the default value
}

Print information

Bulldog also generates a toString() human readable implementation

Log.d("PREFS", UserSettings().toString())
// Ouput
// UserSettings  id=20, [email protected], likes=20, isPremium=false minutesLeft=24.

Enum support

Bulldog supports Enum type using the @Enum annotation.

@Bulldog(name = "UserSettings")
object UserModel {
    @Enum(value = Roles.user)
    val role: Roles = Roles.USER
}

enum class Roles{
  ADMIN, USER;
  
  companion object {
        const val user = "USER"
        const val admin = "ADMIN"
    }
}

You just need annotate the field the enum field and pass a default value.

bulldog's People

Contributors

sergioserra avatar sserra90 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

bulldog's Issues

Make Android Studio recognize the generated class

This is great work and is very simple to use. There could be improvements with that android studio doesn't seem to recognize the generated class even after building. It always says Unresolved reference. Fixing this allows for auto completion and removes all the red text where android studio yells at you because it thinks your code is wrong at every place you use one of the generated classes.

Readme: Dependency typo error

kapt "com.github.Sserra90.bulldog:bulldog-processor:$bulldog"
 implementation "com.github.Sserra90.bulldog:bulldog-runtime:$bulldog"

should be changed to

kapt "com.github.Sserra90.Bulldog:bulldog-processor:$bulldog"
 implementation "com.github.Sserra90.Bulldog:bulldog-runtime:$bulldog"

based on your jitpack https://jitpack.io/#Sserra90/Bulldog/v0.2-beta

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.