This repository has been archived by the owner on Sep 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathREADME.md.template
229 lines (168 loc) · 4.68 KB
/
README.md.template
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# Fragment Creator
[![Circle CI](https://circleci.com/gh/sys1yagi/fragment-creator.svg?style=svg)](https://circleci.com/gh/sys1yagi/fragment-creator)
Fragment Creator is a code generation library to manage fragment class creation and arguments for Android.
I wrote the newInstance method every time when declaring a new Fragment. Then, call `Fragment#getArguments()` and call a get method that is adapted parameter type (ex. getString(), getParcelable()). It's so a boring boilerplate code.
This library helps you to manage fragment class creation and arguments.
# Fragment Creator 2.0.0
Fragment Creator 2.0.0 or later supports AndroidX.
It can be used only with androidx.fragment.app.Fragment.
## How to use
### Annotate the arguments of Fragment
```java
@FragmentCreator
public class MainFragment extends Fragment {
@Args
String keyword;
@Args(require = false)
String userId;
}
```
Then, MainFragmentCreator is generated.
### Create fragment with the FragmentCreator
```java
MainFragment instance = MainFragmentCreator
.newBuilder("keyword")
.build();
MainFragment instance = MainFragmentCreator
.newBuilder("keyword") // required
.setUserId("mike") // optional
.build();
```
### Read the arguments with the FragmentCreator
```java
public MainFragment extends Fragment {
@Args
String keyword;
@Args(require = false)
String userId;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MainFragmentCreator.read(this);
// this.keyword, this.userId are initialized.
}
}
```
### Default value
You can set default value.
```java
public MainFragment extends Fragment {
// The default value can be set only if the argument is optional.
@Args(require = false, defaultString = "unknown")
String keyword;
@Args(require = false, defaultInt = -1)
int userId;
}
```
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
### Private field
You __should__ declare a setter method if using private field.
```java
public MainFragment extends Fragment {
@Args
String keyword;
@Args(require = false)
private String userId;
public String setUserId(String userId){
this.userId = userId;
}
}
```
### Type Serializer
If you want to use the class such as the following, you can use the ArgsSerializer.
```java
// It is not Serializable or Parcelable.
public class Product {
public int id;
public String name;
}
```
At first, define a class that implements the ArgsSerializer.
```java
public class StringSerializer implements ArgsSerializer<Product, String> {
@Override
public String serialize(Product product) {
return product.getId() + "," + product.getName();
}
@Override
public Product deserialize(String str) {
String[] split = str.split(",");
Product product = new Product();
product.setId(Integer.parseInt(split[0]));
product.setName(split[1]);
return product;
}
}
```
Set `@Serializer`.
```java
@FragmentCreator
public class MainFragment extends Fragment {
@Args
@Serializer(to = String.class, serializer = StringSerializer.class)
Product product;
//...
```
### 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`
- `java.lang.CharSequence`
- `android.os.Parcelable`
- `java.io.Serializable`
- `java.util.ArrayList<? extends android.os.Parcelable>`
- `java.util.ArrayList<java.lang.Integer>`
- `java.util.ArrayList<java.lang.String>`
- `java.util.ArrayList<java.lang.CharSequence>`
### Not supported yet
- `android.os.Parcelable[]`
- `android.util.SparseArray<? extends android.os.Parcelable>`
- `boolean[]`
- `byte[]`
- `short[]`
- `char[]`
- `int[]`
- `long[]`
- `float[]`
- `double[]`
- `java.lang.String[]`
- `java.lang.CharSequence[]`
- `android.os.Bundle`
### Installation
This library is distributed by [JitPack](https://jitpack.io/). Add dependencies your build.gradle
```
apt 'com.github.sys1yagi.fragment-creator:processor:%%version%%'
compile 'com.github.sys1yagi.fragment-creator:library:%%version%%'
```
## Development
__Show version__
```
$ ./gradlew version
```
__Bump version__
```
$ ./gradlew bumpMajor
$ ./gradlew bumpMinor
$ ./gradlew bumpPatch
```
__Generate README__
```
$ ./gradlew genReadMe
```