Skip to content

Commit

Permalink
Реализовал работу с фрагментами при переходе в горизонтальный режим и…
Browse files Browse the repository at this point in the history
… обратно.

Задание полностью выполнено.
  • Loading branch information
MayMih committed Feb 27, 2023
1 parent 4be219e commit 2c2b696
Show file tree
Hide file tree
Showing 6 changed files with 325 additions and 272 deletions.
205 changes: 18 additions & 187 deletions app/src/main/java/org/mmu/tinkoffkinolab/CardActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.mmu.tinkoffkinolab;

import static org.mmu.tinkoffkinolab.Constants.ADAPTER_FILM_ID;
import static org.mmu.tinkoffkinolab.Constants.ADAPTER_TITLE;
import static org.mmu.tinkoffkinolab.Constants.LOG_TAG;

import android.content.res.Configuration;
Expand Down Expand Up @@ -39,112 +41,9 @@
public class CardActivity extends AppCompatActivity
{

//region 'Поля и константы'
private static final Map<String, String> _cardData = new HashMap<>();
private ImageView imgPoster;
private String filmId;

private WebDataDownloadTask downloadTask;
private TextView txtHeader;
private TextView txtContent;
private View progBar;
private boolean _isHorizontal;

//endregion 'Поля и константы'



//region 'Типы'

private class WebDataDownloadTask extends AsyncTask<Void, Void, Void>
{
private final FilmsApi filmsApi;
private Map.Entry<Exception, String> error;

public Map.Entry<Exception, String> getError()
{
return error;
}

public WebDataDownloadTask(FilmsApi engine)
{
filmsApi = engine;
}

@Override
protected void onPreExecute()
{
super.onPreExecute();
progBar.setVisibility(View.VISIBLE);
Log.d(LOG_TAG, "Начало загрузки веб-ресурса...");
}

@Override
protected Void doInBackground(Void... unused)
{
try
{
final var response = filmsApi.apiV22FilmsIdGet(Integer.parseInt(filmId));
final var engName = Objects.requireNonNullElse(response.getNameEn(), "");
_cardData.put(Constants.ADAPTER_TITLE, response.getNameRu() + (!engName.isBlank() ?
System.lineSeparator() + "[" + engName +"]" : ""));
final var genres = "\n\n Жанры: " + response.getGenres().stream()
.map(g -> g.getGenre() + ", ")
.collect(Collectors.joining())
.replaceFirst(",\\s*$", "");
final var countries = "\n\n Страны: " + response.getCountries().stream()
.map(country -> country.getCountry() + ", ")
.collect(Collectors.joining()).replaceFirst(",\\s*$", "");
final var res = Objects.requireNonNullElse(response.getDescription(), "") +
genres + countries;
_cardData.put(Constants.ADAPTER_CONTENT, res);
_cardData.put(Constants.ADAPTER_POSTER_PREVIEW_URL, response.getPosterUrl());
}
catch (RuntimeException ex)
{
var mes = Objects.requireNonNullElse(ex.getMessage(), "");
error = new AbstractMap.SimpleEntry<>(ex, mes);
if (ex instanceof ApiException)
{
final var apiEx = (ApiException)ex;
final var headers = apiEx.getResponseHeaders();
final var headersText = headers == null ? "" : headers.entrySet().stream()
.map(entry -> entry.getKey() + ": " + String.join(" \n", entry.getValue()))
.collect(Collectors.joining());
mes += String.format(Locale.ROOT, " %s (ErrorCode: %d), ResponseHeaders: \n%s\n ResponseBody: \n%s\n",
Constants.KINO_API_ERROR_MES, apiEx.getCode(), headersText, apiEx.getResponseBody());
}
Log.e(LOG_TAG, mes.isEmpty() ? Constants.UNKNOWN_WEB_ERROR_MES : mes, ex);
}
return null;
}

/**
* @apiNote Этот метод выполняется в потоке интерфейса
*
* @param unused The result of the operation computed by {@link #doInBackground}.
*/
@Override
protected void onPostExecute(Void unused)
{
super.onPostExecute(unused);
progBar.setVisibility(View.GONE);
if (downloadTask.getError() != null)
{
final var mes = downloadTask.getError().getValue();
showSnackBar(Constants.UNKNOWN_WEB_ERROR_MES);
}
else
{
Log.d(LOG_TAG, "Загрузка Веб-ресурса завершена успешно");
fillCardUI();
}
}
}

//endregion 'Типы'





//region 'Обработчики'
Expand All @@ -160,38 +59,35 @@ protected void onCreate(Bundle savedInstanceState)
this.setSupportActionBar(customToolBar);
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
progBar = findViewById(R.id.progress_bar);

filmId = getIntent().getStringExtra(Constants.ADAPTER_FILM_ID);

getSupportFragmentManager().registerFragmentLifecycleCallbacks(new FragmentManager.FragmentLifecycleCallbacks()
{
private CardFragment cardFragment;
@Override
public void onFragmentViewCreated(@NonNull FragmentManager fm, @NonNull Fragment f,
@NonNull View v, @Nullable Bundle savedInstanceState)
{
imgPoster = v.findViewById(R.id.poster_image_view);
txtHeader = v.findViewById(R.id.card_title);
txtContent = v.findViewById(R.id.card_content);
super.onFragmentViewCreated(fm, f, v, savedInstanceState);
getFilmDataAsync();
cardFragment = ((CardFragment)f);
cardFragment.setDataLoadListener(() -> onDataLoaded_Handler());
cardFragment.getFilmDataAsync(filmId, customToolBar.getTitle().toString());
}

@Override
public void onFragmentDestroyed(@NonNull FragmentManager fm, @NonNull Fragment f)
{
super.onFragmentDestroyed(fm, f);
cardFragment.removeDataLoadListener();
}
}, false);

}


@Override
public void onConfigurationChanged(@NonNull Configuration newConfig)
public void onDataLoaded_Handler()
{
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
_isHorizontal = true;
}
else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
_isHorizontal = false;
}
Objects.requireNonNull(getSupportActionBar()).setDisplayShowTitleEnabled(false);
}

/**
Expand All @@ -218,71 +114,6 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item)



//region 'Методы'

/**
* Метод заполнения UI карточки фильма из скачанных данных
*/
private void fillCardUI()
{
progBar.setVisibility(View.VISIBLE);
// N.B.: параметры fit() и centerCrop() могут сильно замедлять загрузку!
final var posterUrl = _cardData.get(Constants.ADAPTER_POSTER_PREVIEW_URL);
Picasso.get().load(posterUrl).into(imgPoster, new Callback()
{
@Override
public void onSuccess()
{
progBar.setVisibility(View.GONE);
Objects.requireNonNull(getSupportActionBar()).setDisplayShowTitleEnabled(false);
//noinspection ConstantConditions
txtHeader.setText(Objects.requireNonNullElse(_cardData.get(Constants.ADAPTER_TITLE),
getSupportActionBar().getTitle()));
final var textContent = _cardData.get(Constants.ADAPTER_CONTENT);
txtContent.setText(Objects.requireNonNullElse(textContent, ""));
imgPoster.setOnClickListener(v1 -> {
Utils.showFullScreenPhoto(Uri.parse(posterUrl), v1.getContext());
});
final ScrollView sv = findViewById(R.id.fragment_scroll);
sv.postDelayed(() -> {
if (!Utils.isViewOnScreen(txtContent))
{
sv.smoothScrollTo(0, imgPoster.getHeight() / 2);
}
}, 500);
}

@Override
public void onError(Exception e)
{
progBar.setVisibility(View.GONE);
Log.e(LOG_TAG, "Ошибка загрузки большого постера", e);
}
});
}

/**
* Метод отображения всплывющей подсказки
*/
private void showSnackBar(String message)
{
var popup = Snackbar.make(this.imgPoster, message, Snackbar.LENGTH_INDEFINITE);
popup.setAction(R.string.repeat_button_caption, view -> {
getFilmDataAsync();
popup.dismiss();
});
popup.show();
}

private void getFilmDataAsync()
{
if (downloadTask != null && !downloadTask.isCancelled() && (downloadTask.getStatus() == AsyncTask.Status.RUNNING))
{
downloadTask.cancel(true);
}
downloadTask = (WebDataDownloadTask)new WebDataDownloadTask(FilmsApiHelper.getFilmsApi()).execute();
}

//endregion 'Методы'


}
Loading

0 comments on commit 2c2b696

Please sign in to comment.