Skip to content

Commit

Permalink
0.9.3
Browse files Browse the repository at this point in the history
  • Loading branch information
quartack committed Feb 23, 2021
0 parents commit a2c1ac3
Show file tree
Hide file tree
Showing 42 changed files with 1,110 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
trim_trailing_whitespace = true
insert_final_newline = true
max_line_length = 120
tab_width = 4
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*.iml
.gradle
.idea
.DS_Store
/local.properties
/build
/captures
152 changes: 152 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# welcome to bdapter!
Hello! ***bdapter*** is made for developers who are tired of coding `RecyclerView.Adapter` and `RecyclerView.ViewHolder`.

With ***bdapter*** You can use `RecyclerView` without implementation of `RecyclerView.Adapter` and `RecyclerView.ViewHolder` anymore, Also multiple viewHolder is works fine.

And, ***bdapter*** supports `DataBinding` and `MVVM`.

# samples
![](resources/sample_multiple.png) | ![](resources/sample_grid.png) | ![](./resources/sample_update.gif)
:---: | :---: | :---:
[multiple viewHolder](https://github.com/quartack/bdapter_sample/tree/main/app/src/main/java/com/quartack/bdapter/sample/basic) | [any layoutManager](https://github.com/quartack/bdapter_sample/tree/main/app/src/main/java/com/quartack/bdapter/sample/grid) | [update items](https://github.com/quartack/bdapter_sample/tree/main/app/src/main/java/com/quartack/bdapter/sample/update)

![](resources/sample_tree.gif) | ![](./resources/sample_filter.gif) | ![](./resources/sample_sort.gif)
:---: | :---: | :---:
[tree](https://github.com/quartack/bdapter_sample/tree/main/app/src/main/java/com/quartack/bdapter/sample/tree) | [filter](https://github.com/quartack/bdapter_sample/tree/main/app/src/main/java/com/quartack/bdapter/sample/filter) | [sort](https://github.com/quartack/bdapter_sample/tree/main/app/src/main/java/com/quartack/bdapter/sample/sort)

![](./resources/sample_in_recyclerview.gif) | ![](./resources/sample_drag.gif) | ![](./resources/sample_event.gif)
:---: | :---: | :---:
[recyclerView inside recyclerView](https://github.com/quartack/bdapter_sample/tree/main/app/src/main/java/com/quartack/bdapter/sample/recycler_inside_recycler) | [drag](https://github.com/quartack/bdapter_sample/tree/main/app/src/main/java/com/quartack/bdapter/sample/drag) | [event](https://github.com/quartack/bdapter_sample/tree/main/app/src/main/java/com/quartack/bdapter/sample/event)

# download
[![](https://jitpack.io/v/quartack/bdapter.svg)](https://jitpack.io/#quartack/bdapter)
```
repositories {
maven { url 'https://jitpack.io' }}
}
dependencies {
implementation "com.github.quartack.bdapter:bdapter:0.9.3"
kapt "com.github.quartack.bdapter:generator:0.9.3"
}
```
# how to use
## model
**MyModel.kt**
```
@Keep
@bdapterViewHolder(
dataBinding = ViewholderMyModelBinding::class,
viewModelClass = MyViewModel::class
)
data class MyModel(
val id: Int
)
```
Add `@Keep` and `@bdapterViewHolder` annotations in your model class.
If you are using ProGuard's rule, `@Keep` can be omitted.

## view holder - layout
**viewholder_my_model.xml**
```
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="item"
type="com.your.package.MyModel" />
<variable
name="viewModel"
type="com.your.package.MyViewModel" />
</data>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text='@{item.toString()}' />
</layout>
```
Written in general `DataBinding` style.

## view model
**MyViewModel.kt**
```
class MyViewModel : ViewModel() {
private val _items = MutableLiveData<List<Any>>().apply {
value = listOf(
MyModel(1), MyModel(2), MyModel(3)
)
}
val items: LiveData<List<Any>>
get() = _items
}
```
Create `items` it will be the source of RecyclerView.

***bdapter*** will update the RecyclerView if the value of the `items` changed. 👏

## activity - layout
**activity_main.xml**
```
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:binding="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewModel"
type="com.quartack.bdapter.sample.viewmodel.MainViewModel" />
</data>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layoutManager="LinearLayoutManager"
binding:bdapterItems="@{viewModel.items}"
binding:bdapterViewModel="@{viewModel}" />
</layout>
```
Written in general `DataBinding` style.
Set a `viewModel.items` to `binding:bdapterItems` and `viewModel` to `binding:bdapterViewModel`.

## activity
**MyActivity.kt**
```
class MyActivity : AppCompatActivity() {
private val viewModel by viewModels<MyViewModel>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
initDataBinding()
}
private fun initDataBinding() {
ActivityMainBinding.inflate(layoutInflater).also {
it.viewModel = viewModel
it.lifecycleOwner = this
it.lifecycleOwner!!.lifecycle.addObserver(viewModel)
setContentView(it.root)
}
}
}
```
Written in general `DataBinding` style.

## complete!
You used `RecyclerView` without implementing `RecyclerView.Adater` and `RecyclerView.ViewHolder` through ***bdapter***.

# FAQ
* Is multiple ViewHolder possible?
-> **YEP**
* Need code for `RecyclerView.ViewHolder` Class?
-> **NOPE**
* Need code for `RecyclerView.Adapter` Class?
-> **NOPE**
* No need for `RecyclerView.Adapter` logic? Like a add, edit, delete, sort, hide and etc for items.
-> **NOPE, only manage items in ViewMoles**
* Can I use my own the `DiffUtil`?
-> **[SURE, checkout it sample project](https://github.com/quartack/bdapter_sample/tree/main/app/src/main/java/com/quartack/bdapter/sample/diff_util)**
* Can I change the variable name used in Databinding?
-> **[SURE, checkout it sample project](https://github.com/quartack/bdapter_sample/tree/main/app/src/main/java/com/quartack/bdapter/sample/variable_name)**

# license
Apache License 2.0
1 change: 1 addition & 0 deletions annotation/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
12 changes: 12 additions & 0 deletions annotation/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apply plugin: 'java-library'
apply plugin: 'kotlin'

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

buildscript {
repositories {
jcenter()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.quartack.bdapter.annotation

import javax.lang.model.element.Element
import javax.lang.model.element.ElementKind

const val BDAPTER_VIEW_HOLDER_PACKAGE = "com.quartack.bdapter"
const val BDAPTER_VIEW_HOLDER_CLASS = "BdapterViewHolder"

fun Element.getPackageNameOfElement(): String {
var item: Element = this
while (item.kind !== ElementKind.PACKAGE) {
item = item.enclosingElement
}
return item.toString()
}

fun createViewHolderClassName(element: Element) =
createViewHolderClassName(
element.toString()
.replace(element.getPackageNameOfElement(), "")
.replace(".", "")
)

fun <T> createViewHolderClassName(klass: Class<T>) =
createViewHolderClassName(
klass.name.replace("$", "")
)

fun createViewHolderClassName(modelClass: String) =
"$modelClass$BDAPTER_VIEW_HOLDER_CLASS"
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.quartack.bdapter.annotation

import kotlin.reflect.KClass

@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
annotation class BdapterViewHolder(
val dataBinding: KClass<*>,
val viewModelClass: KClass<*>,
val viewModelVariableName: String = "viewModel",
val itemVariableName: String = "item",
val eventListener: KClass<out OnBdapterViewHolderEventListener<*, *, *>> =
EmptyBdapterViewHolderEventListener::class
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.quartack.bdapter.annotation

open class OnBdapterViewHolderEventListener<M, V, VM> {
// From RecyclerView.Adapter#onBindViewHolder
open fun onBindViewHolder(item: M, holder: V, viewModel: VM) {
}

// From RecyclerView.Adapter#onViewAttachedToWindow
open fun onViewAttachedToWindow(item: M, holder: V, viewModel: VM) {
}

// From RecyclerView.Adapter#onViewDetachedFromWindow
open fun onViewDetachedFromWindow(holder: V, viewModel: VM) {
}

// From RecyclerView.Adapter#onViewRecycled
open fun onViewRecycled(holder: V) {
}

// From RecyclerView.Adapter#onFailedToRecycleView
open fun onFailedToRecycleView(holder: V): Boolean {
return false
}
}

class EmptyBdapterViewHolderEventListener : OnBdapterViewHolderEventListener<Any, Any, Any>()
1 change: 1 addition & 0 deletions bdapter/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
60 changes: 60 additions & 0 deletions bdapter/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

android {
compileSdkVersion 30
buildToolsVersion "29.0.2"
defaultConfig {
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName rootProject.ext.version_name
}

buildFeatures {
dataBinding true
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

lintOptions {
abortOnError false
}
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"

api files('libs/annotation.jar')
}

buildscript {
repositories {
jcenter()
}
}

apply plugin: 'maven-publish'
afterEvaluate {
publishing {
publications {
release(MavenPublication) {
from components.release

groupId = rootProject.ext.package_name
artifactId = 'bdapter'
version = rootProject.ext.version_name
}
}
}
}
Empty file added bdapter/consumer-rules.pro
Empty file.
Binary file added bdapter/libs/annotation.jar
Binary file not shown.
21 changes: 21 additions & 0 deletions bdapter/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
2 changes: 2 additions & 0 deletions bdapter/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.quartack.bdapter" />
Loading

0 comments on commit a2c1ac3

Please sign in to comment.