Created
October 15, 2019 07:20
-
-
Save inan-mahmud/f5d428ee3671c905c37ebdf731902920 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> { | |
private String date; | |
private int sourceIndex = 0; | |
private Context context; | |
private ApiInterface apiInterface; | |
private MutableLiveData<Resource> liveData = new MutableLiveData<>(); | |
private MutableLiveData<List<EventsItem>> itemMutableLiveData = new MutableLiveData<>(); | |
public EventDataSource(Context context, String date) { | |
this.date = date; | |
this.context = context; | |
apiInterface = ApiClient.getClient(context).create(ApiInterface.class); | |
} | |
@Override | |
public void loadInitial(@NonNull LoadInitialParams<Integer> params, @NonNull LoadInitialCallback<Integer, EventsItem> callback) { | |
try { | |
Response<EventResult> response = apiInterface.getEventsData(date, sourceIndex).execute(); | |
if (response.body().getEvents() != null) { | |
liveData.postValue(Resource.success(response)); | |
itemMutableLiveData.postValue(response.body().getEvents()); | |
callback.onResult(response.body().getEvents(), null, sourceIndex + 1); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
@Override | |
public void loadBefore(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, EventsItem> callback) { | |
try { | |
Response<EventResult> response = apiInterface.getEventsData(date, params.key).execute(); | |
if (response.body().getEvents() != null) { | |
liveData.postValue(Resource.success(response)); | |
itemMutableLiveData.postValue(response.body().getEvents()); | |
callback.onResult(response.body().getEvents(), params.key - 1); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
@Override | |
public void loadAfter(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, EventsItem> callback) { | |
try { | |
Response<EventResult> response = apiInterface.getEventsData(date, params.key).execute(); | |
if (response.body().getEvents() != null) { | |
liveData.postValue(Resource.success(response)); | |
itemMutableLiveData.postValue(response.body().getEvents()); | |
callback.onResult(response.body().getEvents(), params.key + 1); | |
} | |
} catch (Exception e) { | |
Log.d("EventDataSource", e.getMessage()); | |
} | |
} | |
public MutableLiveData<Resource> getResourceLiveData() { | |
return liveData; | |
} | |
public void updateDate(String date) { | |
this.date = date; | |
} | |
} |
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 MutableLiveData<PageKeyedDataSource<Integer, EventsItem>> itemLiveDataSource = new MutableLiveData<>(); | |
private Context context; | |
private EventDataSource eventDataSource; | |
private String date; | |
public EventDataSourceFactory(Context context, String date) { | |
this.context = context; | |
this.date = date; | |
} | |
@Override | |
public DataSource create() { | |
eventDataSource = new EventDataSource(context, date); | |
itemLiveDataSource.postValue(eventDataSource); | |
return eventDataSource; | |
} | |
public MutableLiveData<PageKeyedDataSource<Integer, EventsItem>> getItemLiveDataSource(String input) { | |
return itemLiveDataSource; | |
} | |
public MutableLiveData<Resource> getLiveData() { | |
return eventDataSource.getResourceLiveData(); | |
} | |
public void updateDate(String date) { | |
eventDataSource.updateDate(date); | |
} |
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<PageKeyedDataSource<Integer, EventsItem>> liveDataSource; | |
public LiveData<PagedList<EventsItem>> itemPagedList; | |
public LiveData<PageKeyedDataSource<Integer, EventsItem>> monthlyLiveDataSource; | |
public LiveData<PagedList<EventsItem>> monthlyItemPagedList; | |
public EventDataSourceFactory dataSourceFactory; | |
private String date; | |
public EventViewModel(Context context, String date) { | |
this.date = date; | |
dataSourceFactory = new EventDataSourceFactory(context, this.date); | |
liveDataSource = dataSourceFactory.getItemLiveDataSource(this.date); | |
MonthlyEventDataSourceFactory monthlyEventDataSourceFactory = new MonthlyEventDataSourceFactory(context, date); | |
monthlyLiveDataSource = monthlyEventDataSourceFactory.getItemLiveDataSource(); | |
PagedList.Config config = new PagedList.Config.Builder() | |
.setEnablePlaceholders(true) | |
.setPrefetchDistance(10) | |
.setPageSize(10) | |
.build(); | |
itemPagedList = createFilteredUsers(dataSourceFactory); | |
monthlyItemPagedList = new LivePagedListBuilder(monthlyEventDataSourceFactory, config).build(); | |
} | |
private LiveData<PagedList<EventsItem>> createFilteredUsers(EventDataSourceFactory dataSourceFactory) { | |
// TODO: handle if `null` and load all data instead | |
return new LivePagedListBuilder(dataSourceFactory, | |
new PagedList.Config.Builder() // | |
.setPageSize(20) // | |
.setPrefetchDistance(20) // | |
.setEnablePlaceholders(true) // | |
.build()) | |
.setInitialLoadKey(0) | |
.build(); | |
} | |
public void updateDate(LifecycleOwner lifecycleOwner, String date) { | |
this.date = date; | |
itemPagedList.removeObservers(lifecycleOwner); | |
itemPagedList = createFilteredUsers(dataSourceFactory); | |
dataSourceFactory.updateDate(date); | |
} | |
} |
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 { | |
private 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; | |
private EventViewModel viewModel; | |
private MonthlyAdapter monthlyAdapter; | |
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"); | |
Date c = Calendar.getInstance().getTime(); | |
System.out.println("Current time => " + c); | |
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); | |
String formattedDate = df.format(c); | |
viewModel = ViewModelProviders.of(this, new EventViewModelFactory(formattedDate, 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) { | |
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); | |
String formattedDate = df.format(dateClicked); | |
replaceSubscription(formattedDate); | |
} | |
@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.setFocusable(false); | |
binding.monthlyEventListView.setFocusable(false); | |
binding.upcomingEventListView.setLayoutManager(new LinearLayoutManager(getContext())); | |
binding.monthlyEventListView.setLayoutManager(new LinearLayoutManager(getContext())); | |
init(); | |
} | |
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)); | |
} | |
private void init() { | |
try { | |
adapter = new EventListAdapter(getContext()); | |
monthlyAdapter = new MonthlyAdapter(getContext()); | |
showProgressDialog("Loading!!!", false); | |
observeViewModel(); | |
binding.upcomingEventListView.setAdapter(adapter); | |
binding.monthlyEventListView.setAdapter(monthlyAdapter); | |
} catch (Exception e) { | |
} | |
} | |
private void observeViewModel() { | |
viewModel.itemPagedList.observe(this, eventsItems -> { | |
if (eventsItems != null && eventsItems.size() > 0) { | |
updateViews(eventsItems); | |
adapter.submitList(eventsItems); | |
dissmissDialog(); | |
} | |
}); | |
viewModel.monthlyItemPagedList.observe(this, eventsItems -> { | |
monthlyAdapter.submitList(eventsItems); | |
dissmissDialog(); | |
}); | |
} | |
private void updateViews(PagedList<EventsItem> eventsItems) { | |
binding.eventCard.setVisibility(View.VISIBLE); | |
binding.upcomingEventCard.setVisibility(View.VISIBLE); | |
binding.emptyImage.setVisibility(View.GONE); | |
binding.eventName.setText(eventsItems.get(0).getTitle()); | |
binding.eventTime.setText(eventsItems.get(0).getStartTime()); | |
binding.duration.setText(String.format("%s - %s", eventsItems.get(0).getStartTime(), eventsItems.get(0).getEndTime())); | |
binding.dateNumber.setText(eventsItems.get(0).getDate()); | |
} | |
private void replaceSubscription(String date) { | |
viewModel.updateDate(this, date); | |
observeViewModel(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment