Last active
October 3, 2019 08:39
-
-
Save inan-mahmud/fc01472b5c69b4dc958a7d5397ba02d8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public class EventDataSource extends PageKeyedDataSource<Integer, EventsItem> { | |
public static final int FIRST_PAGE = 0; | |
private RestRepository restRepository; | |
private String date; | |
private CompositeDisposable disposable; | |
private MutableLiveData<Resource> resource = new MutableLiveData<>(); | |
public EventDataSource(Context context, String date) { | |
restRepository = RestRepository.getInstance(context); | |
this.date = date; | |
disposable = new CompositeDisposable(); | |
} | |
@Override | |
public void loadInitial(@NonNull LoadInitialParams<Integer> params, @NonNull LoadInitialCallback<Integer, EventsItem> callback) { | |
resource.postValue(new Resource(Status.LOADING, null, "")); | |
disposable.add(restRepository.getPagedEventList(date, FIRST_PAGE) | |
.subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(eventResult -> { | |
resource.postValue(Resource.success(eventResult.getEvents())); | |
callback.onResult(eventResult.getEvents(), null, FIRST_PAGE + 1); | |
}) | |
); | |
} | |
@Override | |
public void loadBefore(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, EventsItem> callback) { | |
resource.postValue(new Resource(Status.LOADING, null, "")); | |
disposable.add( | |
restRepository.getPagedEventList(date, params.key) | |
.subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.delay(2, TimeUnit.SECONDS) | |
.subscribe(eventResult -> { | |
int key = FIRST_PAGE; | |
resource.postValue(Resource.success(eventResult.getEvents())); | |
callback.onResult(eventResult.getEvents(), key); | |
}) | |
); | |
} | |
@Override | |
public void loadAfter(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, EventsItem> callback) { | |
resource.postValue(new Resource(Status.LOADING, null, "")); | |
disposable.add( | |
restRepository.getPagedEventList(date, params.key) | |
.subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.delay(2, TimeUnit.SECONDS) | |
.subscribe(eventResult -> { | |
int key = FIRST_PAGE; | |
resource.postValue(Resource.success(eventResult.getEvents())); | |
callback.onResult(eventResult.getEvents(), key); | |
}) | |
); | |
} | |
public void clear() { | |
disposable.clear(); | |
} | |
} |
This file contains hidden or 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
public class EventDataSourceFactory extends DataSource.Factory { | |
private String date; | |
private Context context; | |
private MutableLiveData<PageKeyedDataSource<Integer, EventsItem>> liveDataSource = new MutableLiveData<>(); | |
public EventDataSourceFactory(String date, Context context) { | |
this.date = date; | |
this.context = context; | |
} | |
@Override | |
public DataSource create() { | |
EventDataSource eventDataSource = new EventDataSource(context, date); | |
liveDataSource.postValue(eventDataSource); | |
return eventDataSource; | |
} | |
public MutableLiveData<PageKeyedDataSource<Integer, EventsItem>> getLiveDataSource() { | |
return liveDataSource; | |
} | |
} |
This file contains hidden or 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
public class EventResult { | |
@SerializedName("events") | |
private List<EventsItem> events; | |
public void setEvents(List<EventsItem> events) { | |
this.events = events; | |
} | |
public List<EventsItem> getEvents() { | |
return events; | |
} | |
@Override | |
public String toString() { | |
return | |
"EventResult{" + | |
"events = '" + events + '\'' + | |
"}"; | |
} | |
} |
This file contains hidden or 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
public class EventsItem implements Parcelable { | |
@SerializedName("date") | |
private String date; | |
@SerializedName("start_time") | |
private String startTime; | |
@SerializedName("image") | |
private String image; | |
@SerializedName("u_id") | |
private String uId; | |
@SerializedName("end_time") | |
private String endTime; | |
@SerializedName("details") | |
private String details; | |
@SerializedName("id") | |
private String id; | |
@SerializedName("title") | |
private String title; | |
protected EventsItem(Parcel in) { | |
date = in.readString(); | |
startTime = in.readString(); | |
image = in.readString(); | |
uId = in.readString(); | |
endTime = in.readString(); | |
details = in.readString(); | |
id = in.readString(); | |
title = in.readString(); | |
} | |
public static final Creator<EventsItem> CREATOR = new Creator<EventsItem>() { | |
@Override | |
public EventsItem createFromParcel(Parcel in) { | |
return new EventsItem(in); | |
} | |
@Override | |
public EventsItem[] newArray(int size) { | |
return new EventsItem[size]; | |
} | |
}; | |
public void setDate(String date) { | |
this.date = date; | |
} | |
public String getDate() { | |
return date; | |
} | |
public void setStartTime(String startTime) { | |
this.startTime = startTime; | |
} | |
public String getStartTime() { | |
return startTime; | |
} | |
public void setImage(String image) { | |
this.image = image; | |
} | |
public String getImage() { | |
return image; | |
} | |
public void setUId(String uId) { | |
this.uId = uId; | |
} | |
public String getUId() { | |
return uId; | |
} | |
public void setEndTime(String endTime) { | |
this.endTime = endTime; | |
} | |
public String getEndTime() { | |
return endTime; | |
} | |
public void setDetails(String details) { | |
this.details = details; | |
} | |
public String getDetails() { | |
return details; | |
} | |
public void setId(String id) { | |
this.id = id; | |
} | |
public String getId() { | |
return id; | |
} | |
public void setTitle(String title) { | |
this.title = title; | |
} | |
public String getTitle() { | |
return title; | |
} | |
@Override | |
public String toString() { | |
return | |
"EventsItem{" + | |
"date = '" + date + '\'' + | |
",start_time = '" + startTime + '\'' + | |
",image = '" + image + '\'' + | |
",u_id = '" + uId + '\'' + | |
",end_time = '" + endTime + '\'' + | |
",details = '" + details + '\'' + | |
",id = '" + id + '\'' + | |
",title = '" + title + '\'' + | |
"}"; | |
} | |
@Override | |
public int describeContents() { | |
return 0; | |
} | |
@Override | |
public void writeToParcel(Parcel parcel, int i) { | |
parcel.writeString(date); | |
parcel.writeString(startTime); | |
parcel.writeString(image); | |
parcel.writeString(uId); | |
parcel.writeString(endTime); | |
parcel.writeString(details); | |
parcel.writeString(id); | |
parcel.writeString(title); | |
} | |
} |
This file contains hidden or 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
public class EventViewModel extends BaseViewModel { | |
public LiveData<PagedList<EventsItem>> itemPagedList; | |
public LiveData<PageKeyedDataSource<Integer, EventsItem>> liveDataSource; | |
public MediatorLiveData<Resource<EventResult>> eventList = new MediatorLiveData<>(); | |
public RestRepository restRepository; | |
public EventViewModel(String date, Context context) { | |
EventDataSourceFactory factory = new EventDataSourceFactory(date, context); | |
liveDataSource = factory.getLiveDataSource(); | |
restRepository = RestRepository.getInstance(context); | |
PagedList.Config config = new PagedList.Config.Builder() | |
.setEnablePlaceholders(false) | |
.setPageSize(2) | |
.build(); | |
itemPagedList = new LivePagedListBuilder(factory, config).build(); | |
} | |
public void getEventList(int pageno, String date) { | |
eventList.setValue(Resource.loading(null)); | |
LiveData<Resource<EventResult>> source = restRepository.getEventList(date, pageno); | |
eventList.addSource(source, eventResultResource -> eventList.setValue(eventResultResource)); | |
eventList.removeSource(source); | |
} | |
@Override | |
protected void onCleared() { | |
super.onCleared(); | |
} | |
} |
This file contains hidden or 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
public class HomeFragment extends BaseFragment { | |
FragmentHomeBinding binding; | |
private Calendar mCalender; | |
private SimpleDateFormat mDateFormat; | |
private SimpleDateFormat dateFormatForMonth = new SimpleDateFormat("MMMM yyyy", Locale.getDefault()); | |
private List<Event> events = new ArrayList<>(); | |
private EventListAdapter adapter; | |
EventViewModel viewModel; | |
public HomeFragment() { | |
} | |
public static HomeFragment newInstance() { | |
HomeFragment homeFragment = new HomeFragment(); | |
return homeFragment; | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
View view = provideMyFragment(inflater, container, savedInstanceState); | |
return view; | |
} | |
@Override | |
public View provideMyFragment(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { | |
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, parent, false); | |
mDateFormat = new SimpleDateFormat("dd MMMM yyyy"); | |
viewModel = ViewModelProviders.of(this, new EventViewModelFactory("2019-10-06", FoodHubApplication.getInstance())).get(EventViewModel.class); | |
mCalender = Calendar.getInstance(); | |
binding.monthName.setText(dateFormatForMonth.format(binding.compactcalendarView.getFirstDayOfCurrentMonth())); | |
binding.nextBtn.setOnClickListener(view -> binding.compactcalendarView.scrollRight()); | |
binding.backBtn.setOnClickListener(view -> binding.compactcalendarView.scrollLeft()); | |
binding.compactcalendarView.setFirstDayOfWeek(Calendar.SUNDAY); | |
binding.compactcalendarView.setLocale(TimeZone.getTimeZone("EST"), Locale.US); | |
binding.compactcalendarView.setListener(new CompactCalendarView.CompactCalendarViewListener() { | |
@Override | |
public void onDayClick(Date dateClicked) { | |
} | |
@Override | |
public void onMonthScroll(Date firstDayOfNewMonth) { | |
binding.monthName.setText(dateFormatForMonth.format(firstDayOfNewMonth)); | |
} | |
}); | |
iniViews(); | |
return binding.getRoot(); | |
} | |
private void iniViews() { | |
binding.todayEventBtn.setOnClickListener(view -> { | |
binding.group.setVisibility(View.VISIBLE); | |
binding.monthlyEventCard.setVisibility(View.GONE); | |
toggleBackground(binding.todayEventBtn, binding.monthlyeEventBtn); | |
}); | |
binding.monthlyeEventBtn.setOnClickListener(view -> { | |
binding.group.setVisibility(View.GONE); | |
binding.monthlyEventCard.setVisibility(View.VISIBLE); | |
toggleBackground(binding.monthlyeEventBtn, binding.todayEventBtn); | |
}); | |
binding.upcomingEventListView.setLayoutManager(new LinearLayoutManager(FoodHubApplication.getInstance())); | |
binding.upcomingEventListView.setFocusable(false); | |
adapter = new EventListAdapter(FoodHubApplication.getInstance()); | |
viewModel.itemPagedList.observe(this, eventsItems -> { | |
adapter.submitList(eventsItems); | |
}); | |
binding.upcomingEventListView.setAdapter(adapter); | |
/*adapter.setItemClickListener((view, position) -> { | |
getItemSelectedListener().onItemSelected(adapter.getItem(position)); | |
})*/; | |
binding.monthlyEventListView.setLayoutManager(new LinearLayoutManager(FoodHubApplication.getInstance())); | |
binding.monthlyEventListView.setFocusable(false); | |
binding.monthlyEventListView.setAdapter(new MonthlyAdapter(FoodHubApplication.getInstance())); | |
} | |
private void toggleBackground(Button btn1, Button btn2) { | |
btn1.setBackgroundColor(ContextCompat.getColor(FoodHubApplication.getInstance(), R.color.food_hub_color)); | |
btn1.setTextColor(ContextCompat.getColor(FoodHubApplication.getInstance(), R.color.white)); | |
btn2.setTextColor(ContextCompat.getColor(FoodHubApplication.getInstance(), R.color.black)); | |
btn2.setBackgroundColor(ContextCompat.getColor(FoodHubApplication.getInstance(), R.color.white)); | |
} | |
} |
This file contains hidden or 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
public class RestRepository { | |
private ApiInterface apiInterface; | |
private CacheStorageFactory storageFactory; | |
private static RestRepository restRepository; | |
public RestRepository(Context context) { | |
apiInterface = ApiClient.getClient(context).create(ApiInterface.class); | |
storageFactory = CacheStorageFactory.getInstance(context); | |
} | |
public static RestRepository getInstance(Context context) { | |
if (restRepository == null) { | |
restRepository = new RestRepository(context); | |
} | |
return restRepository; | |
} | |
public LiveData<Resource<EventResult>> getEventList(String date, int pageno) { | |
return LiveDataReactiveStreams.fromPublisher( | |
apiInterface.getEventList(date, pageno) | |
.subscribeOn(Schedulers.io()) | |
.map(Resource::success) | |
.onErrorReturn(e -> Resource.error(e.getMessage(), null)) | |
); | |
} | |
public Flowable<EventResult> getPagedEventList(String date, int pageno) { | |
return apiInterface.getEventList(date, pageno); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment