Skip to content

Commit 61fe7d9

Browse files
author
Josh
committed
Update readme
1 parent 3e07739 commit 61fe7d9

File tree

1 file changed

+199
-0
lines changed

1 file changed

+199
-0
lines changed

README.md

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,201 @@
11
# easydatabinding
22
This project goal is to create activity fragment and views, with less code to be more efficient on the view models
3+
4+
5+
# Goal
6+
7+
I used DataBinding for a few weeks since Google allows us to use it.
8+
9+
For now i saw a lot of duplicated code to instantiate an activity or a fragment
10+
11+
## DataBinding
12+
13+
- Each Activity or Fragmentis linked to a viewmodel who will expose the data to the view (layout) and handle some specific fucntionnalities
14+
15+
16+
- ViewModel will also handle some treatments :
17+
18+
- WS Calls / Database to update the recyclerview of the layout
19+
- activityresult of activity (for facebook / google login for example)
20+
21+
So in a very general case, code of an activity will look like this :
22+
23+
### Activity
24+
25+
For example, in class DB, we have to do
26+
27+
- layout
28+
- class activity
29+
- class view model linked to the activity
30+
31+
32+
33+
#### Layout
34+
35+
First of all we need something like this for the layout
36+
37+
```
38+
<?xml version="1.0" encoding="utf-8"?>
39+
40+
<layout xmlns:android="http://schemas.android.com/apk/res/android"
41+
xmlns:app="http://schemas.android.com/apk/res-auto"
42+
xmlns:tools="http://schemas.android.com/tools">
43+
44+
<data>
45+
46+
47+
<variable
48+
name="activityXXXViewModel"
49+
type="package.to.your.viewmodel.ActivityXXXViewModel" />
50+
</data>
51+
52+
<android.support.design.widget.CoordinatorLayout
53+
android:layout_width="match_parent"
54+
android:layout_height="match_parent"
55+
android:fitsSystemWindows="true"
56+
tools:bindingContext=".core.MainActivity">
57+
58+
<android.support.design.widget.AppBarLayout
59+
android:layout_width="match_parent"
60+
android:layout_height="wrap_content"
61+
android:theme="@style/AppTheme.AppBarOverlay">
62+
63+
<android.support.v7.widget.Toolbar
64+
android:id="@+id/toolbar"
65+
android:layout_width="match_parent"
66+
android:layout_height="?attr/actionBarSize"
67+
android:background="?attr/colorPrimary"
68+
app:popupTheme="@style/AppTheme.PopupOverlay" />
69+
70+
</android.support.design.widget.AppBarLayout>
71+
72+
73+
<RelativeLayout
74+
android:layout_width="match_parent"
75+
android:layout_height="match_parent"
76+
app:layout_behavior="@string/appbar_scrolling_view_behavior">
77+
78+
79+
80+
</RelativeLayout>
81+
82+
83+
</android.support.design.widget.CoordinatorLayout>
84+
</layout>
85+
86+
```
87+
88+
Then Comes our view model :
89+
90+
### ViewModel : XXXActivityViewModel
91+
92+
```
93+
94+
/**
95+
* Created by josh on 11/03/16.
96+
*/
97+
public class ActivityXXXViewModel extends BaseObservable implements ViewModel {
98+
private static final String TAG = ActivityXViewModel.class.getSimpleName();
99+
100+
101+
private final Context context;
102+
private final ActivityXXXBinding binding;
103+
/***
104+
* @param context
105+
*/
106+
public ActivityXXXViewModel(ActivityXXX context, ActivityXXXBinding binding) {
107+
this.context = context;
108+
this.binding = binding;
109+
}
110+
111+
@Override
112+
public void onDestroy() {
113+
114+
}
115+
116+
}
117+
118+
```
119+
120+
121+
### Activity : XXXActivity
122+
123+
ELle va juste servir de "pont" pour créer le Binding avec le view model.
124+
125+
126+
```
127+
/***
128+
Activity exemple pour du databinding contenant une recyclerview
129+
***/
130+
public class XXXActivity extends AppCompatActivity {
131+
132+
/***
133+
* Cette classe est générée par le DataBinding en prenant le nom du layout {@link com.phoceis.kioskcultura_mobile.R.layout#activity_stores}
134+
* et en ajoutant Binding à la fin
135+
*/
136+
ActivityXXXBinding binding;
137+
138+
@Override
139+
protected void onCreate(Bundle savedInstanceState) {
140+
super.onCreate(savedInstanceState);
141+
binding = DataBindingUtil.setContentView(this, R.layout.activity_xxx);
142+
setSupportActionBar(binding.toolbar);
143+
ActivityXXXViewModel activityXXXViewModel = new ActivityXXXViewModel(this);
144+
binding.setActivityXXXViewModel(activityXXXViewModel);
145+
binding.xxxRecyclerView.setLayoutManager(new LinearLayoutManager(this));
146+
}
147+
148+
}
149+
150+
```
151+
152+
153+
154+
With the library an activity will be :
155+
156+
157+
```
158+
public class ActivityBaseMain extends ActivityBase<ActivityMainBinding,ActivityMainBaseVM> {
159+
160+
@Override
161+
public int data() {
162+
return joxad.easydatabinding.sample.BR.mainActivityVM;
163+
}
164+
165+
@Override
166+
public int layoutResources() {
167+
return R.layout.activity_main;
168+
}
169+
170+
@Override
171+
public ActivityMainBaseVM baseActivityVM(ActivityMainBinding binding) {
172+
return new ActivityMainBaseVM(this, binding);
173+
}
174+
175+
}
176+
```
177+
178+
And your VM :
179+
180+
```
181+
public class ActivityMainBaseVM extends ActivityBaseVM<ActivityBaseMain, ActivityMainBinding> {
182+
/***
183+
* @param activity
184+
* @param binding
185+
*/
186+
public ActivityMainBaseVM(ActivityBaseMain activity, ActivityMainBinding binding) {
187+
super(activity, binding);
188+
}
189+
190+
@Override
191+
public void init() {
192+
193+
}
194+
195+
@Override
196+
public void destroy() {
197+
198+
}
199+
}
200+
```
201+

0 commit comments

Comments
 (0)