safebundle-annotation

a library for validating types of the bundle

License

License

GroupId

GroupId

io.github.sabgilhun
ArtifactId

ArtifactId

safebundle-annotation
Last Version

Last Version

0.2.2
Release Date

Release Date

Type

Type

pom.sha512
Description

Description

safebundle-annotation
a library for validating types of the bundle
Source Code Management

Source Code Management

https://github.com/sabgilhun/SafeBundle

Download safebundle-annotation

Dependencies

compile (1)

Group / Artifact Type Version
org.jetbrains.kotlin : kotlin-stdlib jar 1.4.10

Project Modules

There are no modules declared in this project.

SafeBundle ๐Ÿ“ฆ

๋ชฉ์ 

์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๋ชฉ์ ์€ Activity/Fragment๊ฐ„ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ• ๋•Œ ์‚ฌ์šฉํ•˜๋Š” Bundle์— ๊ฐ’์„ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋„ฃ๊ณ , ๊ทธ๊ฒƒ์— ๋งž๊ฒŒ ๊บผ๋‚ด์„œ ์‚ฌ์šฉํ•˜๋Š”์ง€ ์ปดํŒŒ์ผ ํƒ€์ž„์— ํ™•์ธํ•˜์—ฌ ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๋ฅผ ๋ฏธ์—ฐ์— ๋ฐฉ์ง€ ํ•˜๋Š” ๊ฒƒ

class MyActivity1: AppCompatActivity() {

    ...
    
    fun startMyActivity2() {
        val intent = Intent(this, MyActivity2::class.java).apply {
            putExtra("param", "str")
        }
        startActivity(intent)
    }
    
    ...
    
}

class MyActivity2: AppCompatActivity() {
    ...
    
    override fun onCreate(savedInstanceState: Bundle?) {
        // wrong key value
        val param = intent.extras?.get("wrong key") as String -> Runtime Error
        
        // wrong type casting
        val param = intent.extras?.get("param") as Int -> Runtime Error
    }
}

์œ„์˜ ๊ฐ™์ด ๊ฐœ๋ฐœ์ž๊ฐ€ ์‹ค์ˆ˜๋ฅผ ํ•  ๊ฒฝ์šฐ ๋Ÿฐํƒ€์ž„์— ๋ฒ„๊ทธ๋ฅผ ์•Œ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์œ„ํ—˜ํ•˜๋‹ค.

์‚ฌ์šฉ๋ฒ•

0. ์˜์กด์„ฑ ์„ค์ •

SafeBundle ์˜์กด์„ฑ ๋“ฑ๋ก

implementation "io.github.sabgilhun:safebundle:x.y.z"
implementation "io.github.sabgilhun:safebundle-annotation:x.y.z"
kapt "io.github.sabgilhun:safebundle-processor:x.y.z"

ํ˜„์žฌ ๋ฒ„์ „(0.2.1)

1. ์ „๋‹ฌ ๋ฐ›์„ ์ธ์ž ์„ค์ •

Activity์—์„œ ๋ฐ›์„ ๋ฐ์ดํ„ฐ๋ฅผ ์ธ์ž๋“ค์„ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜ ์ •์˜ํ•˜๊ณ  by bundle<Type>()๋กœ property delegation ํ•œ๋‹ค.

class ReceiveActivity: AppCompatActivity() {

    private val p1 by bundle<Int>()
    
    private val p2 by bundle<String?>()
}

2. Activity๋ฅผ ์‹œ์ž‘ ํ•˜๋Š” Navgator์ •์˜

Activity๋ฅผ ์–ด๋–ค ์ธ์ž๋กœ ์‹œ์ž‘ํ• ์ง€ ์ •์˜ํ•œ Navigator๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

// Navigator๋Š” interface์—ฌ์•ผ ํ•˜๋ฉฐ ContextBasedCreatable์„ ํ™•์žฅํ•ด์•ผ ํ•œ๋‹ค.
interface Navigator : ContextBasedCreatable {
    // ReciveActivity์—์„œ ์ •์˜ํ•œ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋“ค์˜ ๋ณ€์ˆ˜๋ช…๊ณผ ํƒ€์ž…๊ณผ ์ผ์น˜ํ•ด์•ผ ํ•œ๋‹ค.
    // p1 - Int, p2 - String?
    fun start(p1: Int, p2: String?) // return ํƒ€์ž…์€ Unit์ด์—ฌ์•ผ ํ•œ๋‹ค.
    
    // p2๋Š” Nullableํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ธ์ž๋ฅผ ์•„์˜ˆ ์•ˆ์“ฐ๋Š” ํ•จ์ˆ˜ ์ •์˜๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.
    fun start(p1: Int)
    
    // ๋ฐ›๋Š”์ชฝ์˜ p2๋Š” Nullableํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ถฉ๋ถ„ ์กฐ๊ฑด์ธ p2๊ฐ€ NonNullํ•œ ํ•จ์ˆ˜ ์ •์˜๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.
    fun start(p1: Int, p2: String)
}

3. Activity์™€ Navgator ์—ฐ๊ฒฐ

Activity์™€ ํ•ด๋‹น Activity๊ฐ€ ์–ด๋–ค ์ธ์ž๋“ค๋กœ ์‹œ์ž‘๋  ์ˆ˜ ์žˆ๋Š”์ง€ ์ •์˜ํ•œ Navigator๋ฅผ SafeBundle ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ์—ฐ๊ฒฐํ•ด์ฃผ์–ด์•ผ ์ปดํŒŒ์ผ ํƒ€์ž„์— ์ •ํ•ฉ์„ฑ ์ฒดํฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค.

@SafeBundle(Navigator::class) // ์ด์ค„ ์ถ”๊ฐ€
class ReceiveActivity: AppCompatActivity() {

    private val p1 by bundle<Int>()
    
    private val p2 by bundle<String?>()
}

4. Navgator๋ฅผ ํ†ตํ•ด Activity ์‹คํ–‰

class MainActivity: AppCompatActivity() {
    
    private val navigator = create<Navigator>() // Activity Extension function์ธ create() ์‚ฌ์šฉ
    
    fun startReceiveActivity() {
        navigator.start(1, "p2")
    }
    
    ...
}

License

SafeBundle is under MIT License. (detail)

Versions

Version
0.2.2
0.2.1
0.2.0
0.1.9