88
99import androidx .annotation .NonNull ;
1010import androidx .annotation .Nullable ;
11- import androidx .core .util .Pair ;
1211import androidx .fragment .app .Fragment ;
1312import androidx .lifecycle .ViewModelProvider ;
1413import androidx .navigation .Navigation ;
3130import com .mrikso .anitube .app .utils .ViewUtils ;
3231
3332import dagger .hilt .android .AndroidEntryPoint ;
34-
3533import io .reactivex .rxjava3 .disposables .CompositeDisposable ;
3634
3735@ AndroidEntryPoint
@@ -73,26 +71,16 @@ private void initViews() {
7371 LoadState refreshLoadState = combinedLoadStates .getRefresh ();
7472 LoadState appendLoadState = combinedLoadStates .getAppend ();
7573 if (refreshLoadState instanceof LoadState .Loading ) {
76- binding .container .setVisibility (View .GONE );
77- binding .loadStateLayout .progressBar .setVisibility (View .VISIBLE );
78- binding .loadStateLayout .errorLayout .setVisibility (View .GONE );
74+ showLoadingState ();
7975 }
8076 if (refreshLoadState instanceof LoadState .NotLoading ) {
8177 if (refreshLoadState .getEndOfPaginationReached () && animePpagingAdapter .getItemCount () < 1 ) {
8278 showNoDataState ();
8379 } else {
84- binding .container .setVisibility (View .VISIBLE );
85- binding .loadStateLayout .progressBar .setVisibility (View .GONE );
86- binding .loadStateLayout .errorLayout .setVisibility (View .GONE );
80+ showReadyState ();
8781 }
8882 } else if (refreshLoadState instanceof LoadState .Error ) {
89- binding .loadStateLayout .progressBar .setVisibility (View .GONE );
90- binding .container .setVisibility (View .GONE );
91- binding .loadStateLayout .errorLayout .setVisibility (View .VISIBLE );
92- binding .loadStateLayout .repeat .setOnClickListener (v -> animePpagingAdapter .retry ());
93- LoadState .Error loadStateError = (LoadState .Error ) refreshLoadState ;
94- binding .loadStateLayout .errorMessage .setText (
95- loadStateError .getError ().getLocalizedMessage ());
83+ showErrorState ((LoadState .Error ) refreshLoadState );
9684 }
9785 if (!(refreshLoadState instanceof LoadState .Loading ) && appendLoadState instanceof LoadState .NotLoading ) {
9886 if (appendLoadState .getEndOfPaginationReached () && animePpagingAdapter .getItemCount () < 1 ) {
@@ -116,21 +104,50 @@ private void initViews() {
116104 binding .layoutToolbar .profileAvatar .setOnClickListener (v -> openProfileFragment ());
117105 }
118106
107+ private void showReadyState () {
108+ if (binding != null ) {
109+ binding .container .setVisibility (View .VISIBLE );
110+ binding .loadStateLayout .progressBar .setVisibility (View .GONE );
111+ binding .loadStateLayout .errorLayout .setVisibility (View .GONE );
112+ }
113+ }
114+
115+ private void showLoadingState () {
116+ if (binding != null ) {
117+ binding .container .setVisibility (View .GONE );
118+ binding .loadStateLayout .progressBar .setVisibility (View .VISIBLE );
119+ binding .loadStateLayout .errorLayout .setVisibility (View .GONE );
120+ }
121+ }
122+
123+ private void showErrorState (LoadState .Error refreshLoadState ) {
124+ if (binding != null ) {
125+ binding .loadStateLayout .progressBar .setVisibility (View .GONE );
126+ binding .container .setVisibility (View .GONE );
127+ binding .loadStateLayout .errorLayout .setVisibility (View .VISIBLE );
128+ binding .loadStateLayout .repeat .setOnClickListener (v -> animePpagingAdapter .retry ());
129+ binding .loadStateLayout .errorMessage .setText (
130+ refreshLoadState .getError ().getLocalizedMessage ());
131+ }
132+ }
133+
119134 private void openDetailsFragment (final String link ) {
120135 AnimeListFragmentDirections .ActionNavAnimeListToNavDetailsAnimeInfo action =
121136 AnimeListFragmentDirections .actionNavAnimeListToNavDetailsAnimeInfo (link );
122137 Navigation .findNavController (requireView ()).navigate (action );
123138 }
124139
125140 private void showNoDataState () {
126- binding .loadStateLayout .ivIcon .setImageResource (R .drawable .image_no_data );
127- binding .loadStateLayout .errorMessageTitle .setText (R .string .state_no_data );
128- binding .loadStateLayout .errorMessage .setText (R .string .state_no_data_anime_list_desc );
129-
130- binding .container .setVisibility (View .GONE );
131- binding .loadStateLayout .progressBar .setVisibility (View .GONE );
132- binding .loadStateLayout .buttonLl .setVisibility (View .GONE );
133- binding .loadStateLayout .errorLayout .setVisibility (View .VISIBLE );
141+ if (binding != null ) {
142+ binding .loadStateLayout .ivIcon .setImageResource (R .drawable .image_no_data );
143+ binding .loadStateLayout .errorMessageTitle .setText (R .string .state_no_data );
144+ binding .loadStateLayout .errorMessage .setText (R .string .state_no_data_anime_list_desc );
145+
146+ binding .container .setVisibility (View .GONE );
147+ binding .loadStateLayout .progressBar .setVisibility (View .GONE );
148+ binding .loadStateLayout .buttonLl .setVisibility (View .GONE );
149+ binding .loadStateLayout .errorLayout .setVisibility (View .VISIBLE );
150+ }
134151 }
135152
136153 private void initObservers () {
0 commit comments