Skip to content

This lib is needed in order to generate functions for creating fragments and activities.

License

Notifications You must be signed in to change notification settings

LiteSoftware/ScreenFactoryGenerator

Repository files navigation

ScreenFactoryGenerator

This lib is needed in order to generate functions for creating fragments and activities.

Generated classes:

import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import com.example.ksp.*

object MainFragmentScreen : BaseScreen {

    fun newInstance(test1: Int, test2: String) = MainFragment().apply {
       arguments = bundleOf("TEST1" to test1,"TEST2" to test2)
    }

    fun MainFragment.initArguments() {
       test1 = arguments?.get("TEST1") as Int
       test2 = arguments?.get("TEST2") as String
    }
}
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import com.example.ksp.*

object MainActivityScreen : BaseScreen {

    fun newIntent(context: Context, test1: String): Intent { 
       val intent = Intent(context, MainActivity::class.java)
       intent.putExtra("TEST1", test1)
       return intent
    }

    fun MainActivity.initArguments() {
       test1 = intent.getStringExtra("TEST1")!!
    }
}

How use

  1. Add ksp plugin:
repositories {
    gradlePluginPortal()
    // ...
}
plugins {
    // ...
    id 'com.google.devtools.ksp' version '1.7.10-1.0.6'
}
  1. Specify where the generated files will be located
android {
//..
kotlin {
        sourceSets.debug {
            kotlin.srcDirs += 'build/generated/ksp/debug/kotlin'
        }
        sourceSets.release {
            kotlin.srcDirs += 'build/generated/ksp/release/kotlin'
        }
    }
}
  1. Download using Gradle

Add this in your root build.gradle at the end of repositories in allprojects section:

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}

Then add this dependency to your module-level build.gradle in dependencies section:

implementation 'com.github.LiteSoftware.ScreenFactoryGenerator:screen-generator-annotation:$version'
ksp 'com.github.LiteSoftware.ScreenFactoryGenerator:screen-generator-processor:$version'
  1. Annotate activities or fragments with @JScreen annotation
@JScreen()
class MainActivity : AppCompatActivity() {
// ...
}
  1. Add properties as arguments. To do this, you need to mark the parameters with the annotation @JParam
// ...
    @JParam
    var test1: String = ""
// ...
  1. Compile the application

  2. That's all) you can Use the generated classes and methods

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // ...
        initArguments() // must be called in order to fill in your arguments
    }
val intent = MainActivityScreen.newIntent(this, "Hello World!!!")
startActivity(intent)

Supported types

primitive type

java.lang.String

java.lang.Boolean

java.lang.Byte

java.lang.Character

java.lang.Short

java.lang.Integer

java.lang.Long

java.lang.Float

java.lang.Double


License

   Copyright 2022 Javavirys. All rights reserved.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.