Created
June 7, 2017 18:13
-
-
Save JoaquimLey/104d268186ebe1cef07bed71df25dd82 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
public class BusListActivity extends AppCompatActivity implements LifecycleRegistryOwner, | |
BusListAdapter.ClickListener { | |
private final LifecycleRegistry lifecycleRegistry = new LifecycleRegistry(this); | |
private BusListViewModel viewModel; | |
private BusListAdapter adapter; | |
private ContentLoadingProgressBar progressDialogView; | |
private SwipeRefreshLayout swipeToRefreshView; | |
@Override | |
public LifecycleRegistry getLifecycle() { | |
return this.lifecycleRegistry; | |
} | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_bus_list); | |
initViews(); | |
initViewModel(); | |
} | |
private void initViews() { | |
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); | |
toolbar.setTitle(R.string.title_bus_list_activity); | |
setSupportActionBar(toolbar); | |
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); | |
recyclerView.setHasFixedSize(true); | |
recyclerView.setLayoutManager(new LinearLayoutManager(this)); | |
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL)); | |
adapter = new BusListAdapter(this); | |
recyclerView.setAdapter(adapter); | |
progressDialogView = (ContentLoadingProgressBar) findViewById(R.id.progress_dialog); | |
swipeToRefreshView = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh); | |
swipeToRefreshView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { | |
@Override | |
public void onRefresh() { | |
viewModel.onPullRequested(); | |
} | |
}); | |
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); | |
fab.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
viewModel.onFabButtonClicked(); | |
} | |
}); | |
} | |
private void initViewModel() { | |
viewModel = ViewModelProviders.of(this).get(BusListViewModel.class); | |
subscribeDataStreams(viewModel); | |
} | |
private void subscribeDataStreams(BusListViewModel viewModel) { | |
viewModel.getBusList().observe(this, new Observer<List<BusEntity>>() { | |
@Override | |
public void onChanged(@Nullable List<BusEntity> busEntities) { | |
if (busEntities != null) { | |
hideProgress(); | |
showBusListInUi(busEntities); | |
} else { | |
showProgress(); | |
} | |
} | |
}); | |
} | |
private void showBusListInUi(List<BusEntity> busEntities) { | |
adapter.setItems(busEntities); | |
} | |
private void showProgress() { | |
if (adapter.isEmpty() && !progressDialogView.isShown()) { | |
progressDialogView.setVisibility(View.VISIBLE); | |
} | |
} | |
private void hideProgress() { | |
if (swipeToRefreshView.isRefreshing()) { | |
swipeToRefreshView.setRefreshing(false); | |
} | |
if (progressDialogView.isShown()) { | |
progressDialogView.setVisibility(View.GONE); | |
} | |
} | |
@Override | |
public void onItemClicked(BusEntity bus) { | |
viewModel.onListItemClicked(bus); | |
openBusDetailActivity(bus); | |
} | |
private void openBusDetailActivity(BusEntity bus) { | |
BusProfileActivity.start(this, bus.getId()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment