@@ -65,49 +65,54 @@ public void onBindViewHolder(@NonNull ConstructorViewHolder holder, int position
6565 constructorStandingsElement = constructorStandingsList .get (position );
6666 String currentConstructorId = constructorStandingsElement .getConstructor ().getConstructorId ();
6767
68- constructorViewModel .getSelectedConstructor (currentConstructorId ).observe (lifecycleOwner , result -> {
69- if (result instanceof Result .Loading ) {
70- return ;
71- }
72- if (result .isSuccess ()) {
73- Constructor constructor = ((Result .ConstructorSuccess ) result ).getData ();
74- constructorStandingsElement .setConstructor (constructor );
75-
76- holder .constructorCardInnerLayout .setBackground (AppCompatResources .getDrawable (context ,
77- Objects .requireNonNull (Constants .TEAM_GRADIENT_COLOR .get (currentConstructorId ))));
78-
79- UIUtils .setTextViewTextWithCondition (constructorStandingsElement .getPosition () == null ,
80- ContextCompat .getString (context , R .string .last_constructor_position ), //if true
81- constructorStandingsElement .getPosition (), //if false
82- holder .constructorPosition );
83-
84- UIUtils .multipleSetTextViewText (
85- new String []{
86- constructor .getName (),
87- constructorStandingsElement .getPoints ()},
88- new TextView []{
89- holder .constructorName ,
90- holder .constructorPoints });
91-
92- if (constructorId != null ) {
93- if (currentConstructorId .equals (constructorId )) {
94- UIUtils .animateCardBackgroundColor (context , holder .constructorCard , R .color .yellow , Color .TRANSPARENT , 1000 , 10 );
95- }
68+
69+ constructorViewModel .getSelectedConstructor (currentConstructorId ).observe (lifecycleOwner , result -> {
70+ if (result instanceof Result .Loading ) {
71+ return ;
9672 }
73+ if (result .isSuccess ()) {
74+ Constructor constructor = ((Result .ConstructorSuccess ) result ).getData ();
75+ constructorStandingsElement .setConstructor (constructor );
76+
77+ holder .constructorCardInnerLayout .setBackground (AppCompatResources .getDrawable (context ,
78+ Objects .requireNonNull (Constants .TEAM_GRADIENT_COLOR .get (currentConstructorId ))));
79+
80+ UIUtils .setTextViewTextWithCondition (constructorStandingsElement .getPosition () == null ,
81+ ContextCompat .getString (context , R .string .last_constructor_position ), //if true
82+ constructorStandingsElement .getPosition (), //if false
83+ holder .constructorPosition );
84+
85+ UIUtils .multipleSetTextViewText (
86+ new String []{
87+ constructor .getName (),
88+ constructorStandingsElement .getPoints ()},
89+ new TextView []{
90+ holder .constructorName ,
91+ holder .constructorPoints });
92+
93+ if (constructorId != null ) {
94+ if (currentConstructorId .equals (constructorId )) {
95+ UIUtils .animateCardBackgroundColor (context , holder .constructorCard , R .color .yellow , Color .TRANSPARENT , 1000 , 10 );
96+ }
97+ }
98+
99+ holder .constructorCard .setOnClickListener (v -> goToBioPage (position ));
97100
98- holder .constructorCard .setOnClickListener (v -> goToBioPage (position ));
101+ UIUtils .loadSequenceOfImagesWithGlide (context ,
102+ new String []{
103+ constructor .getCar_pic_url (),
104+ constructor .getTeam_logo_url ()},
105+ new ImageView []{
106+ holder .constructorCarImage ,
107+ holder .constructorLogo },
108+
109+ () -> processDriverOne (holder , constructor , position ));
110+ }else {
111+ showConstructorNotFound (holder , currentConstructorId );
112+ }
113+ });
99114
100- UIUtils .loadSequenceOfImagesWithGlide (context ,
101- new String []{
102- constructor .getCar_pic_url (),
103- constructor .getTeam_logo_url ()},
104- new ImageView []{
105- holder .constructorCarImage ,
106- holder .constructorLogo },
107115
108- () -> processDriverOne (holder , constructor , position ));
109- }
110- });
111116
112117 }
113118
@@ -120,39 +125,90 @@ private void goToBioPage(int position) {
120125 }
121126
122127 private void processDriverOne (ConstructorViewHolder holder , Constructor constructor , int position ) {
123- driverViewModel .getDriver (constructor .getDriverOneId ()).observe (lifecycleOwner , result -> {
124- if (result instanceof Result .Loading ) {
125- return ;
126- }
127- if (result .isSuccess ()) {
128- Driver driverOne = ((Result .DriverSuccess ) result ).getData ();
129-
130- UIUtils .singleSetTextViewText (driverOne .getFullName (), holder .driverOneName );
131- UIUtils .loadImageWithGlide (context , driverOne .getDriver_pic_url (), holder .driverOneImage ,
132- () -> processDriverTwo (holder , constructor , position ));
133- }
134- });
128+ try {
129+ driverViewModel .getDriver (constructor .getDriverOneId ()).observe (lifecycleOwner , result -> {
130+ if (result instanceof Result .Loading ) {
131+ return ;
132+ }
133+ if (result .isSuccess ()) {
134+ Driver driverOne = ((Result .DriverSuccess ) result ).getData ();
135+
136+ UIUtils .singleSetTextViewText (driverOne .getFullName (), holder .driverOneName );
137+ UIUtils .loadImageWithGlide (context , driverOne .getDriver_pic_url (), holder .driverOneImage ,
138+ () -> processDriverTwo (holder , constructor , position ));
139+ }else {
140+ setMissingDriver (holder , constructor .getDriverOneId (), constructor , position , 1 );
141+ }
142+ });
143+ } catch (RuntimeException e ) {
144+ setMissingDriver (holder , constructor .getDriverOneId (), constructor , position , 1 );
145+ }
146+
135147 }
136148
137149 private void processDriverTwo (ConstructorViewHolder holder , Constructor constructor , int position ) {
138- driverViewModel .getDriver (constructor .getDriverTwoId ()).observe (lifecycleOwner , result -> {
139- if (result instanceof Result .Loading ) {
140- return ;
141- }
142- if (result .isSuccess ()) {
143- Driver driverTwo = ((Result .DriverSuccess ) result ).getData ();
150+ try {
151+ driverViewModel .getDriver (constructor .getDriverTwoId ()).observe (lifecycleOwner , result -> {
152+ if (result instanceof Result .Loading ) {
153+ return ;
154+ }
155+ if (result .isSuccess ()) {
156+ Driver driverTwo = ((Result .DriverSuccess ) result ).getData ();
144157
145- UIUtils .singleSetTextViewText (driverTwo .getFullName (), holder .driverTwoName );
146- UIUtils .loadImageWithGlide (context , driverTwo .getDriver_pic_url (), holder .driverTwoImage , () -> {
158+ UIUtils .singleSetTextViewText (driverTwo .getFullName (), holder .driverTwoName );
159+ UIUtils .loadImageWithGlide (context , driverTwo .getDriver_pic_url (), holder .driverTwoImage , () -> endLoading (position ));
160+ }else {
161+ setMissingDriver (holder , constructor .getDriverTwoId (), constructor , position , 2 );
162+ }
163+ });
164+ } catch (RuntimeException e ) {
165+ setMissingDriver (holder , constructor .getDriverTwoId (), constructor , position , 2 );
166+ }
167+
168+ }
169+
170+ private void endLoading (int position ) {
171+ loadingScreen .updateProgress ();
172+
173+ Log .i ("ConstructorsStanding" , "onBindViewHolder " + position + "/" + getItemCount ());
174+ loadingScreen .hideLoadingScreenWithCondition (position == getItemCount () - 1 );
175+ }
176+
177+ private void setMissingDriver (ConstructorViewHolder holder , String driverId , Constructor constructor , int position , int driverType ) {
178+ if (driverId .contains ("_" )) {
179+ driverId = driverId .split ("_" )[1 ];
180+ }
181+
182+ switch (driverType ){
183+ case 1 :
184+ UIUtils .singleSetTextViewText (driverId .toUpperCase (), holder .driverOneName );
185+ UIUtils .loadImageWithGlide (context , null , holder .driverOneImage ,
186+ () -> processDriverTwo (holder , constructor , position ));
187+ break ;
188+ case 2 :
189+ UIUtils .singleSetTextViewText (driverId .toUpperCase (), holder .driverTwoName );
190+ UIUtils .loadImageWithGlide (context , null , holder .driverTwoImage , () -> endLoading (position ));
191+ }
192+
193+ }
194+
195+ private void showConstructorFound (ConstructorStandingsRecyclerAdapter .ConstructorViewHolder holder ) {
196+ holder .constructorCardInnerLayout .setVisibility (View .VISIBLE );
197+ holder .constructorNotFound .setVisibility (View .GONE );
198+ }
199+
200+ private void showConstructorNotFound (ConstructorViewHolder holder , String constructorId ) {
201+ holder .constructorCardInnerLayout .setVisibility (View .INVISIBLE );
202+ holder .constructorNotFound .setVisibility (View .VISIBLE );
203+ Log .i ("ConstructorsStandingAdapter" , "Constructor not found id test: " + constructorId + " -> " + constructorId .contains ("_" ));
204+
205+ if (constructorId .contains ("_" )) {
206+ constructorId = constructorId .split ("_" )[0 ] + " " + constructorId .split ("_" )[1 ];
207+ }
147208
148- loadingScreen . updateProgress ( );
209+ UIUtils . singleSetTextViewText ( context . getString ( R . string . constructor_info_not_found_upper_case , constructorId . toUpperCase ()), holder . constructorNotFound );
149210
150- Log .i ("ConstructorsStanding" , "onBindViewHolder " + position + "/" + getItemCount ());
151- loadingScreen .hideLoadingScreenWithCondition (position == getItemCount () - 1 );
152211
153- });
154- }
155- });
156212 }
157213
158214 @ Override
@@ -162,7 +218,7 @@ public int getItemCount() {
162218
163219 public static class ConstructorViewHolder extends RecyclerView .ViewHolder {
164220
165- TextView constructorName , constructorPoints , constructorPosition , driverOneName , driverTwoName ;
221+ TextView constructorName , constructorPoints , constructorPosition , driverOneName , driverTwoName , constructorNotFound ;
166222 ImageView constructorLogo , constructorCarImage , driverOneImage , driverTwoImage ;
167223 LinearLayout constructorCardInnerLayout ;
168224 MaterialCardView constructorCard ;
@@ -181,6 +237,7 @@ public ConstructorViewHolder(@NonNull View itemView) {
181237 driverTwoImage = itemView .findViewById (R .id .driver_2_pic );
182238 constructorCardInnerLayout = itemView .findViewById (R .id .team_card );
183239 constructorCard = itemView .findViewById (R .id .team_card_view );
240+ constructorNotFound = itemView .findViewById (R .id .constructor_not_found );
184241 }
185242 }
186243}
0 commit comments