Advertisement
Guest User

Untitled

a guest
Jan 12th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. public class RestClient {
  2. private static final String BASE_URL = "твой юрл";
  3.  
  4. private static final String username = "sportExtern";
  5. private static final String password = "xsgrnt4dNcpny";
  6.  
  7. private final ApiInterface service;
  8.  
  9. public RestClient() {
  10. Retrofit retrofit = new Retrofit.Builder()
  11. .baseUrl(BASE_URL)
  12. .client(new OkHttpClient())
  13. .addConverterFactory(SimpleXmlConverterFactory.create())
  14. .build();
  15. service = retrofit.create(ApiInterface.class);
  16. }
  17.  
  18. private void getData(String user, String password, Callback<GamesData> callback) {
  19. Call<GamesData> call = service.getData(user, password);
  20. call.enqueue(callback);
  21. }
  22.  
  23. public void getGamesData(Callback<GamesData> callback) {
  24. getData(username, password, callback);
  25. }
  26. }
  27. репозиторий
  28. public class GameRepository {
  29. private RestClient restClient;
  30. private List<Branch> branches = new ArrayList<>();
  31.  
  32. private MutableLiveData<List<Branch>> branchesLiveData = new MutableLiveData<>();
  33. private MutableLiveData<List<Game>> gamesLiveData = new MutableLiveData<>();
  34.  
  35. public GameRepository(RestClient restClient) {
  36. this.restClient = restClient;
  37. }
  38.  
  39. public LiveData<List<Game>> getGamesLiveData() {
  40. if (gamesLiveData.getValue() == null) {
  41. loadGamesData();
  42. }
  43. return gamesLiveData;
  44. }
  45.  
  46. private void loadGamesData() {
  47. restClient.getGamesData(new Callback<GamesData>() {
  48. @Override
  49. public void onResponse(@NonNull Call<GamesData> call, @NonNull Response<GamesData> response) {
  50. if (response.isSuccessful()) {
  51. GamesData gamesData = response.body();
  52. if (gamesData != null) {
  53. branches.addAll(gamesData.getBranchesList());
  54. branchesLiveData.postValue(branches);
  55.  
  56. List<Game> games = new ArrayList<>();
  57. for (Branch branch : branches) {
  58. games.addAll(branch.getGames());
  59. }
  60. gamesLiveData.postValue(games);
  61. }
  62. } else {
  63. L.d("responseCode " + response.code() + "response.isSuccessful " + response.isSuccessful());
  64. }
  65. }
  66.  
  67. @Override
  68. public void onFailure(@NonNull Call<GamesData> call, @NonNull Throwable t) {
  69. L.d("Error " + t);
  70. }
  71. });
  72. }
  73. }
  74.  
  75. и в фрагменте подписываешся на ливдата и отдаеш в адаптер
  76. public class ScheduledGamesFragment extends Fragment implements GamesAdapter.GameClickListener {
  77. @BindView(R.id.fsg_game_list)
  78. RecyclerView gamesList;
  79. @BindView(R.id.fsg_dates_list)
  80. RecyclerView datesList;
  81.  
  82. private GamesAdapter gamesAdapter;
  83. private DatesAdapter datesAdapter;
  84.  
  85. private Unbinder unbinder;
  86.  
  87. public static ScheduledGamesFragment newInstance() {
  88. return new ScheduledGamesFragment();
  89. }
  90.  
  91. @Override
  92. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  93. Bundle savedInstanceState) {
  94. View view = inflater.inflate(R.layout.fragment_scheduled_games, container, false);
  95. unbinder = ButterKnife.bind(this, view);
  96.  
  97. getActivity().setTitle(getString(R.string.sgf_scheduled_games));
  98.  
  99. initGamesList();
  100. gamesAdapter = new GamesAdapter();
  101. gamesAdapter.setGamesClickListener(this);
  102. LinearLayoutManager managerGames = new LinearLayoutManager(getActivity());
  103. gamesList.setLayoutManager(managerGames);
  104. gamesList.setAdapter(gamesAdapter);
  105.  
  106. datesAdapter = new DatesAdapter(getListDates());
  107. LinearLayoutManager managerDates = new LinearLayoutManager(getActivity());
  108. datesList.setLayoutManager(managerDates);
  109. datesList.setAdapter(datesAdapter);
  110.  
  111. return view;
  112. }
  113.  
  114. @Override
  115. public void onDestroyView() {
  116. super.onDestroyView();
  117. unbinder.unbind();
  118. }
  119.  
  120. @OnClick(R.id.fsg_btn_choose_league)
  121. public void onClickChooseLeague() {
  122. startActivity(new Intent(getActivity(), AddTeamActivity.class));
  123. }
  124.  
  125. @Override
  126. public void onGameClick(int position) {
  127. startActivity(new Intent(getActivity(), GamesActivity.class));
  128. }
  129.  
  130. private void initGamesList() {
  131. LiveData<List<Game>> gamesLiveData = App.getInstance().getGameRepository().getGamesLiveData();
  132.  
  133. gamesLiveData.observe(this, new Observer<List<Game>>() {
  134. @Override
  135. public void onChanged(@Nullable List<Game> games) {
  136. if (games != null) {
  137. gamesAdapter.setGames(games);
  138. }
  139. }
  140. });
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement