forked from jakubkinst/Android-ViewModelBinding
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2da3153
commit 79e66df
Showing
9 changed files
with
73 additions
and
60 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
library/src/main/java/cz/kinst/jakub/viewmodelbinding/Navigator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package cz.kinst.jakub.viewmodelbinding; | ||
|
||
import android.os.Bundle; | ||
|
||
public class Navigator<T extends ViewInterface> { | ||
|
||
private Bundle params; | ||
private Class<T> cls; | ||
private int id; | ||
private Object extraData; | ||
|
||
public Navigator(Class<T> cls, Bundle params, int id, Object extraData) { | ||
this.cls = cls; | ||
this.params = params; | ||
this.id = id; | ||
this.extraData = extraData; | ||
} | ||
|
||
public Navigator(Class<T> cls, Bundle params) { | ||
this(cls, params, 0, null); | ||
} | ||
|
||
public Navigator(Class<T> cls) { | ||
this(cls, null, 0, null); | ||
} | ||
|
||
|
||
public Bundle getParams() { | ||
return params; | ||
} | ||
|
||
public Class<T> getCls() { | ||
return cls; | ||
} | ||
|
||
public int getId() { | ||
return id; | ||
} | ||
|
||
public Object getExtraData() { | ||
return extraData; | ||
} | ||
|
||
public Navigator<T> with(Bundle params) { | ||
this.params = params; | ||
return this; | ||
} | ||
|
||
public static <T extends ViewInterface> Navigator<T> to(Class<T> cls) { | ||
return new Navigator<>(cls); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 3 additions & 26 deletions
29
sample/src/main/java/cz/kinst/jakub/sample/viewmodelbinding/SampleDialogFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,18 @@ | ||
package cz.kinst.jakub.sample.viewmodelbinding; | ||
|
||
import android.os.Bundle; | ||
import android.support.annotation.Nullable; | ||
import android.view.View; | ||
|
||
import cz.kinst.jakub.sample.viewmodelbinding.databinding.DialogSampleBinding; | ||
import cz.kinst.jakub.viewmodelbinding.ViewModelBindingConfig; | ||
import cz.kinst.jakub.viewmodelbinding.ViewModelDialogFragment; | ||
import cz.kinst.jakub.viewmodelbinding.*; | ||
|
||
|
||
public class SampleDialogFragment extends ViewModelDialogFragment<DialogSampleBinding, SampleDialogViewModel> { | ||
|
||
private SampleDialogViewModel.SampleDialogListener mListener; | ||
|
||
|
||
public static SampleDialogFragment newInstance() { | ||
Bundle args = new Bundle(); | ||
SampleDialogFragment fragment = new SampleDialogFragment(); | ||
fragment.setArguments(args); | ||
return fragment; | ||
} | ||
|
||
|
||
@Override | ||
public ViewModelBindingConfig getViewModelBindingConfig() { | ||
return new ViewModelBindingConfig(R.layout.dialog_sample, SampleDialogViewModel.class); | ||
} | ||
|
||
|
||
@Override | ||
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { | ||
super.onViewCreated(view, savedInstanceState); | ||
getViewModel().setListener(mListener); | ||
} | ||
|
||
|
||
public void setListener(SampleDialogViewModel.SampleDialogListener listener) { | ||
mListener = listener; | ||
public void navigate(Navigator n) { | ||
dismiss(); | ||
} | ||
} |
16 changes: 3 additions & 13 deletions
16
sample/src/main/java/cz/kinst/jakub/sample/viewmodelbinding/SampleDialogViewModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,17 @@ | ||
package cz.kinst.jakub.sample.viewmodelbinding; | ||
|
||
import android.view.View; | ||
import android.widget.Toast; | ||
|
||
import cz.kinst.jakub.sample.viewmodelbinding.databinding.DialogSampleBinding; | ||
import cz.kinst.jakub.viewmodelbinding.ViewModel; | ||
|
||
|
||
public class SampleDialogViewModel extends ViewModel<DialogSampleBinding> { | ||
SampleDialogListener mListener; | ||
|
||
|
||
public interface SampleDialogListener { | ||
void onButtonClicked(); | ||
} | ||
|
||
|
||
public void onClickedButton(View v) { | ||
if(mListener != null) | ||
mListener.onButtonClicked(); | ||
Toast.makeText(getContext(), "Button in dialog clicked", Toast.LENGTH_SHORT).show(); | ||
getView().navigate(null); | ||
} | ||
|
||
|
||
public void setListener(SampleDialogListener listener) { | ||
mListener = listener; | ||
} | ||
} |