项目作者: mohfathy0

项目描述 :
Lazy MVVM FireStore
高级语言: Java
项目地址: git://github.com/mohfathy0/LazyMVVMFireStoreQuery.git
创建时间: 2019-09-02T18:26:14Z
项目社区:https://github.com/mohfathy0/LazyMVVMFireStoreQuery

开源协议:

下载


Lazy MVVM FireStore Query

About:

This is a MVVM ready to use with Firebase FireStore Database Query builder and no need to rewrite the declaration for each Activity or Fragment If you need a new instance of the ViewModel just use new Request code for each Activity or Fragment If you need to get the same exact result and have another Activity or fragment observing the same data just use the same Request Code.
Do not forget to link your application to FireStore and it’s dependencies to your project in addition to Step 2

How to import:

Step 1

Make sure to add to build.gradle (Project)

  1. allprojects {
  2. repositories {
  3. google()
  4. jcenter()
  5. maven { url 'https://jitpack.io' }
  6. }
  7. }

Step 2

Import the liberaries to your project

  1. def lifecycle_version = "2.0.0" /*or whatever is avialable*/
  2. // ViewModel and LiveData
  3. implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
  4. annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
  5. def LazyVersion= "1.0.1" /*or whatever is avialable*/
  6. // Lazy MVVM FireStore Query
  7. implementation "com.github.mohfathy0:LazyMVVMFireStoreQuery:$LazyVersion"

How to use:

Create your own Models and follow this example in your activity

  1. public class MainActivity extends AppCompatActivity {
  2. private String CollectionPath = "users";
  3. private String QueryField="id";
  4. private int QueryValue=1;
  5. private int RequestCode=123;
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_main);
  10. ILazyViewModel viewModel = ViewModelProviders.of(this).get(LazyViewModel.class);
  11. viewModel.init(RequestCode);
  12. viewModel.getDataWhereEqualTo(CollectionPath,QueryField,QueryValue,RequestCode).observe(this, new Observer<DataOrException<List<QueryDocumentSnapshot>>>() {
  13. @Override
  14. public void onChanged(DataOrException<List<QueryDocumentSnapshot>> e) {
  15. if (e.getData()!=null || e.getException()!=null){
  16. for (QueryDocumentSnapshot snap:e.getData()){
  17. Log.i("LazyMVVMFireStoreQuery",snap.toObject(UsersModel.class).getFirstName()+snap.toObject(UsersModel.class).getLastName());
  18. }
  19. }else {
  20. Log.i("LazyMVVMFireStoreQuery", e.getException().getMessage());
  21. }
  22. }
  23. });
  24. }
  25. }

Note:

Feel free to create your own class the converts from QueryDocumentSnapshot to whatever you need and start running in within OnChanged and you need to do the necessary validation