Created
November 8, 2018 03:41
-
-
Save kyawhtut-cu/e8a07588bb27c73d3c848e4def66f0b5 to your computer and use it in GitHub Desktop.
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
package com.team10.wastetomoney.buyerFragment; | |
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
import android.support.v4.app.Fragment; | |
import android.support.v7.widget.DefaultItemAnimator; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import com.google.firebase.database.DataSnapshot; | |
import com.google.firebase.database.DatabaseError; | |
import com.google.firebase.database.FirebaseDatabase; | |
import com.google.firebase.database.ValueEventListener; | |
import com.team10.wastetomoney.R; | |
import com.team10.wastetomoney.adapter.AddSellListAdapter; | |
import com.team10.wastetomoney.adapter.SellAdapter; | |
import com.team10.wastetomoney.model.SellModel; | |
import com.team10.wastetomoney.model.SellerModel; | |
import java.util.ArrayList; | |
import java.util.List; | |
import io.reactivex.Observable; | |
import io.reactivex.Observer; | |
import io.reactivex.android.schedulers.AndroidSchedulers; | |
import io.reactivex.disposables.CompositeDisposable; | |
import io.reactivex.disposables.Disposable; | |
import io.reactivex.functions.BiFunction; | |
import io.reactivex.schedulers.Schedulers; | |
public class HomeFragment extends Fragment { | |
private SellAdapter mAdapter; | |
RecyclerView recyclerView; | |
private List<SellModel.Sell> sellList = new ArrayList<>(); | |
private AddSellListAdapter addSellListAdapter; | |
CompositeDisposable mCompositeDisposable; | |
public HomeFragment() { | |
} | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
View view = inflater.inflate(R.layout.fragment_post, container, false); | |
mCompositeDisposable = new CompositeDisposable(); | |
addSellListAdapter = new AddSellListAdapter(getContext(), sellList); | |
recyclerView = view.findViewById(R.id.recycler_view); | |
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext()); | |
recyclerView.setLayoutManager(layoutManager); | |
recyclerView.setItemAnimator(new DefaultItemAnimator()); | |
recyclerView.setAdapter(addSellListAdapter); | |
bindFirebase(); | |
bindData(); | |
return view; | |
} | |
private void bindFirebase() { | |
FirebaseDatabase.getInstance().getReference().child("SellTable").addValueEventListener(new ValueEventListener() { | |
@Override | |
public void onDataChange(@NonNull DataSnapshot dataSnapshot) { | |
List<SellModel.Sell> sellList = new ArrayList<>(); | |
for (DataSnapshot ds : dataSnapshot.getChildren()) { | |
SellModel.Sell sell = ds.getValue(SellModel.Sell.class); | |
sell.setSellId(ds.getKey()); | |
sellList.add(sell); | |
} | |
// observer.onNext(sellList); | |
} | |
@Override | |
public void onCancelled(@NonNull DatabaseError databaseError) { | |
// observer.onError(databaseError.toException()); | |
} | |
}); | |
} | |
private void bindData() { | |
Observable.combineLatest( | |
getAllSell(), | |
getAllSeller(), | |
new BiFunction<List<SellModel.Sell>, List<SellerModel.Seller>, List<SellModel.Sell>>() { | |
@Override | |
public List<SellModel.Sell> apply(List<SellModel.Sell> sell, List<SellerModel.Seller> sellerModel) throws Exception { | |
List<SellModel.Sell> finalItem = new ArrayList<>(); | |
for (SellModel.Sell item : sell) { | |
boolean isHas = false; | |
for (int index = 0; index < sellerModel.size() && !isHas; index++) { | |
if (item.getSellerId().equals(sellerModel.get(index).getSellerId())) { | |
isHas = true; | |
SellModel.Sell foundItem = item; | |
foundItem.setSellerName(sellerModel.get(index).getSellerName()); | |
foundItem.setSellerImage(sellerModel.get(index).getSellerImage()); | |
foundItem.setSellerCity(sellerModel.get(index).getSellerCity()); | |
foundItem.setSellerTownship(sellerModel.get(index).getSellerTownship()); | |
finalItem.add(foundItem); | |
} | |
} | |
} | |
return finalItem; | |
} | |
} | |
).subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(new Observer<List<SellModel.Sell>>() { | |
@Override | |
public void onSubscribe(Disposable d) { | |
mCompositeDisposable.add(d); | |
} | |
@Override | |
public void onNext(List<SellModel.Sell> sellList) { | |
addSellListAdapter.swipeData(sellList); | |
} | |
@Override | |
public void onError(Throwable e) { | |
e.printStackTrace(); | |
} | |
@Override | |
public void onComplete() { | |
} | |
}); | |
} | |
private Observable<List<SellModel.Sell>> getAllSell() { | |
return new Observable<List<SellModel.Sell>>() { | |
@Override | |
protected void subscribeActual(final Observer<? super List<SellModel.Sell>> observer) { | |
FirebaseDatabase.getInstance().getReference().child("SellTable").addValueEventListener(new ValueEventListener() { | |
@Override | |
public void onDataChange(@NonNull DataSnapshot dataSnapshot) { | |
List<SellModel.Sell> sellList = new ArrayList<>(); | |
for (DataSnapshot ds : dataSnapshot.getChildren()) { | |
SellModel.Sell sell = ds.getValue(SellModel.Sell.class); | |
sell.setSellId(ds.getKey()); | |
sellList.add(sell); | |
} | |
observer.onNext(sellList); | |
} | |
@Override | |
public void onCancelled(@NonNull DatabaseError databaseError) { | |
observer.onError(databaseError.toException()); | |
} | |
}); | |
} | |
}; | |
} | |
private Observable<List<SellerModel.Seller>> getAllSeller() { | |
return new Observable<List<SellerModel.Seller>>() { | |
@Override | |
protected void subscribeActual(final Observer<? super List<SellerModel.Seller>> observer) { | |
FirebaseDatabase.getInstance().getReference().child("SellerTable").addValueEventListener(new ValueEventListener() { | |
@Override | |
public void onDataChange(@NonNull DataSnapshot dataSnapshot) { | |
List<SellerModel.Seller> userList = new ArrayList<>(); | |
for (DataSnapshot ds : dataSnapshot.getChildren()) { | |
SellerModel.Seller user = ds.getValue(SellerModel.Seller.class); | |
user.setSellerId(ds.getKey()); | |
userList.add(user); | |
} | |
observer.onNext(userList); | |
} | |
@Override | |
public void onCancelled(@NonNull DatabaseError databaseError) { | |
observer.onError(databaseError.toException()); | |
} | |
}); | |
} | |
}; | |
} | |
@Override | |
public void onStop() { | |
super.onStop(); | |
mCompositeDisposable.clear(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment