Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class RestClient {
- private static final String BASE_URL = "твой юрл";
- private static final String username = "sportExtern";
- private static final String password = "xsgrnt4dNcpny";
- private final ApiInterface service;
- public RestClient() {
- Retrofit retrofit = new Retrofit.Builder()
- .baseUrl(BASE_URL)
- .client(new OkHttpClient())
- .addConverterFactory(SimpleXmlConverterFactory.create())
- .build();
- service = retrofit.create(ApiInterface.class);
- }
- private void getData(String user, String password, Callback<GamesData> callback) {
- Call<GamesData> call = service.getData(user, password);
- call.enqueue(callback);
- }
- public void getGamesData(Callback<GamesData> callback) {
- getData(username, password, callback);
- }
- }
- репозиторий
- public class GameRepository {
- private RestClient restClient;
- private List<Branch> branches = new ArrayList<>();
- private MutableLiveData<List<Branch>> branchesLiveData = new MutableLiveData<>();
- private MutableLiveData<List<Game>> gamesLiveData = new MutableLiveData<>();
- public GameRepository(RestClient restClient) {
- this.restClient = restClient;
- }
- public LiveData<List<Game>> getGamesLiveData() {
- if (gamesLiveData.getValue() == null) {
- loadGamesData();
- }
- return gamesLiveData;
- }
- private void loadGamesData() {
- restClient.getGamesData(new Callback<GamesData>() {
- @Override
- public void onResponse(@NonNull Call<GamesData> call, @NonNull Response<GamesData> response) {
- if (response.isSuccessful()) {
- GamesData gamesData = response.body();
- if (gamesData != null) {
- branches.addAll(gamesData.getBranchesList());
- branchesLiveData.postValue(branches);
- List<Game> games = new ArrayList<>();
- for (Branch branch : branches) {
- games.addAll(branch.getGames());
- }
- gamesLiveData.postValue(games);
- }
- } else {
- L.d("responseCode " + response.code() + "response.isSuccessful " + response.isSuccessful());
- }
- }
- @Override
- public void onFailure(@NonNull Call<GamesData> call, @NonNull Throwable t) {
- L.d("Error " + t);
- }
- });
- }
- }
- и в фрагменте подписываешся на ливдата и отдаеш в адаптер
- public class ScheduledGamesFragment extends Fragment implements GamesAdapter.GameClickListener {
- @BindView(R.id.fsg_game_list)
- RecyclerView gamesList;
- @BindView(R.id.fsg_dates_list)
- RecyclerView datesList;
- private GamesAdapter gamesAdapter;
- private DatesAdapter datesAdapter;
- private Unbinder unbinder;
- public static ScheduledGamesFragment newInstance() {
- return new ScheduledGamesFragment();
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.fragment_scheduled_games, container, false);
- unbinder = ButterKnife.bind(this, view);
- getActivity().setTitle(getString(R.string.sgf_scheduled_games));
- initGamesList();
- gamesAdapter = new GamesAdapter();
- gamesAdapter.setGamesClickListener(this);
- LinearLayoutManager managerGames = new LinearLayoutManager(getActivity());
- gamesList.setLayoutManager(managerGames);
- gamesList.setAdapter(gamesAdapter);
- datesAdapter = new DatesAdapter(getListDates());
- LinearLayoutManager managerDates = new LinearLayoutManager(getActivity());
- datesList.setLayoutManager(managerDates);
- datesList.setAdapter(datesAdapter);
- return view;
- }
- @Override
- public void onDestroyView() {
- super.onDestroyView();
- unbinder.unbind();
- }
- @OnClick(R.id.fsg_btn_choose_league)
- public void onClickChooseLeague() {
- startActivity(new Intent(getActivity(), AddTeamActivity.class));
- }
- @Override
- public void onGameClick(int position) {
- startActivity(new Intent(getActivity(), GamesActivity.class));
- }
- private void initGamesList() {
- LiveData<List<Game>> gamesLiveData = App.getInstance().getGameRepository().getGamesLiveData();
- gamesLiveData.observe(this, new Observer<List<Game>>() {
- @Override
- public void onChanged(@Nullable List<Game> games) {
- if (games != null) {
- gamesAdapter.setGames(games);
- }
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement