-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathViewModelBaseBindingFragment.java
More file actions
48 lines (41 loc) · 1.64 KB
/
Copy pathViewModelBaseBindingFragment.java
File metadata and controls
48 lines (41 loc) · 1.64 KB
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
package eu.inloop.viewmodel.binding;
import androidx.databinding.ViewDataBinding;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import eu.inloop.viewmodel.AbstractViewModel;
import eu.inloop.viewmodel.IView;
import eu.inloop.viewmodel.base.ViewModelBaseFragment;
public abstract class ViewModelBaseBindingFragment<T extends IView, R extends AbstractViewModel<T>, B extends ViewDataBinding>
extends ViewModelBaseFragment<T, R>
implements IView {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getViewModelHelper().performBinding(this);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
getViewModelHelper().performBinding(this);
final ViewDataBinding binding = getViewModelHelper().getBinding();
if (binding != null) {
return binding.getRoot();
} else {
throw new IllegalStateException("Binding cannot be null. Perform binding before calling getBinding()");
}
}
@SuppressWarnings("unused")
@Nullable
public B getBinding() {
try {
return (B) getViewModelHelper().getBinding();
} catch (ClassCastException ex) {
throw new IllegalStateException("Method getViewModelBindingConfig() has to return same " +
"ViewDataBinding type as it is set to base Fragment");
}
}
}