Skip to content

Commit dcb74fe

Browse files
committed
added other backup layouts
If a driver/constructor is not found (neither online or offline), a backup layout is showed
1 parent 91e3a43 commit dcb74fe

8 files changed

Lines changed: 239 additions & 107 deletions

File tree

app/src/main/java/com/the_coffe_coders/fastestlap/adapter/ConstructorStandingsRecyclerAdapter.java

Lines changed: 123 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

app/src/main/java/com/the_coffe_coders/fastestlap/adapter/DriversStandingRecyclerAdapter.java

Lines changed: 66 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -73,49 +73,76 @@ public void onBindViewHolder(@NonNull DriverViewHolder holder, int position) {
7373
driverStandingsElement = driversStandingList.get(position);
7474
}
7575

76-
driverViewModel.getDriver(driverStandingsElement.getDriver().getDriverId()).observe(lifecycleOwner, result -> {
77-
if (result instanceof Result.Loading) {
78-
return;
79-
}
80-
if (result.isSuccess()) {
81-
Driver driver = ((Result.DriverSuccess) result).getData();
82-
83-
UIUtils.multipleSetTextViewText(
84-
new String[]{
85-
driver.getFullName(),
86-
driverStandingsElement.getPoints(),
87-
},
88-
new TextView[]{
89-
holder.driverName,
90-
holder.driverPoints,
91-
92-
});
93-
94-
UIUtils.setTextViewTextWithCondition(driverStandingsElement.getPosition() == null || driverStandingsElement.getPosition().equals("-"),
95-
ContextCompat.getString(context, R.string.last_driver_position), //if true
96-
driverStandingsElement.getPosition(), //if false
97-
holder.driverPosition);
98-
99-
if (driverId != null) {
100-
if (driverStandingsElement.getDriver().getDriverId().equals(driverId)) {
101-
UIUtils.animateCardBackgroundColor(context, holder.driverCard.findViewById(R.id.driver_card_view), R.color.yellow, Color.TRANSPARENT, 1000, 10);
102-
}
76+
try{
77+
driverViewModel.getDriver(driverStandingsElement.getDriver().getDriverId()).observe(lifecycleOwner, result -> {
78+
if (result instanceof Result.Loading) {
79+
return;
10380
}
81+
if (result.isSuccess()) {
82+
showDriverFound(holder);
83+
Driver driver = ((Result.DriverSuccess) result).getData();
84+
85+
UIUtils.multipleSetTextViewText(
86+
new String[]{
87+
driver.getFullName(),
88+
driverStandingsElement.getPoints(),
89+
},
90+
new TextView[]{
91+
holder.driverName,
92+
holder.driverPoints,
93+
94+
});
95+
96+
UIUtils.setTextViewTextWithCondition(driverStandingsElement.getPosition() == null || driverStandingsElement.getPosition().equals("-"),
97+
ContextCompat.getString(context, R.string.last_driver_position), //if true
98+
driverStandingsElement.getPosition(), //if false
99+
holder.driverPosition);
100+
101+
if (driverId != null) {
102+
if (driverStandingsElement.getDriver().getDriverId().equals(driverId)) {
103+
UIUtils.animateCardBackgroundColor(context, holder.driverCard.findViewById(R.id.driver_card_view), R.color.yellow, Color.TRANSPARENT, 1000, 10);
104+
}
105+
}
106+
107+
holder.driverCard.setOnClickListener(v -> goToBioPage(position));
104108

105-
holder.driverCard.setOnClickListener(v -> goToBioPage(position));
109+
if (driver.getTeam_id() != null) {
110+
holder.driverCardInnerLayout.setBackground(AppCompatResources.getDrawable(context, Constants.TEAM_GRADIENT_COLOR.get(driver.getTeam_id())));
111+
} else {
112+
holder.driverCardInnerLayout.setBackground(AppCompatResources.getDrawable(context, R.color.timer_gray));
113+
holder.driverTeamImage.setImageDrawable(AppCompatResources.getDrawable(context, R.drawable.f1_car_icon_filled));
114+
}
106115

107-
if (driver.getTeam_id() != null) {
108-
holder.driverCardInnerLayout.setBackground(AppCompatResources.getDrawable(context, Constants.TEAM_GRADIENT_COLOR.get(driver.getTeam_id())));
109-
} else {
110-
holder.driverCardInnerLayout.setBackground(AppCompatResources.getDrawable(context, R.color.timer_gray));
111-
holder.driverTeamImage.setImageDrawable(AppCompatResources.getDrawable(context, R.drawable.f1_car_icon_filled));
116+
UIUtils.loadImageWithGlide(context, driver.getDriver_pic_url(), holder.driverImage, () ->
117+
generateForConstructor(holder, driver, position));
118+
119+
}else{
120+
showDriverNotFound(holder, driverStandingsElement.getDriver().getDriverId());
112121
}
122+
});
123+
} catch (RuntimeException e) {
124+
Log.e("DriversStandingAdapter", "driver error: " + e.getMessage());
125+
showDriverNotFound(holder, driverStandingsElement.getDriver().getDriverId());
126+
}
113127

114-
UIUtils.loadImageWithGlide(context, driver.getDriver_pic_url(), holder.driverImage, () ->
115-
generateForConstructor(holder, driver, position));
128+
}
129+
130+
private void showDriverFound(DriverViewHolder holder) {
131+
holder.driverCardInnerLayout.setVisibility(View.VISIBLE);
132+
holder.driverNotFound.setVisibility(View.GONE);
133+
}
134+
135+
private void showDriverNotFound(DriverViewHolder holder, String driverId) {
136+
holder.driverCardInnerLayout.setVisibility(View.INVISIBLE);
137+
holder.driverNotFound.setVisibility(View.VISIBLE);
138+
Log.i("DriversStandingAdapter", "Driver not found id test: " + driverId + " -> " + driverId.contains("_"));
139+
140+
if(driverId.contains("_")) {
141+
driverId = driverId.split("_")[1];
142+
}
143+
144+
UIUtils.singleSetTextViewText(context.getString(R.string.driver_info_not_found_upper_case, driverId.toUpperCase()), holder.driverNotFound);
116145

117-
}
118-
});
119146
}
120147

121148
private void goToBioPage(int position) {
@@ -164,7 +191,7 @@ public int getItemCount() {
164191
public static class DriverViewHolder extends RecyclerView.ViewHolder {
165192

166193
MaterialCardView driverCard;
167-
TextView driverName, driverPoints, driverPosition;
194+
TextView driverName, driverPoints, driverPosition, driverNotFound;
168195
ImageView driverImage, driverTeamImage;
169196
RelativeLayout driverCardInnerLayout;
170197

@@ -178,6 +205,7 @@ public DriverViewHolder(@NonNull View itemView) {
178205
driverImage = itemView.findViewById(R.id.driver_image);
179206
driverTeamImage = itemView.findViewById(R.id.team_logo);
180207
driverCardInnerLayout = itemView.findViewById(R.id.small_driver_card);
208+
driverNotFound = itemView.findViewById(R.id.driver_not_found);
181209
}
182210
}
183211
}

app/src/main/java/com/the_coffe_coders/fastestlap/repository/constructor/ConstructorRepository.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,16 @@ public void onConstructorLoaded(Constructor constructor) {
118118
lastUpdateTimestamps.put(constructorId, System.currentTimeMillis());
119119
Objects.requireNonNull(constructorCache.get(constructorId)).postValue(new Result.ConstructorSuccess(constructor));
120120
} else {
121-
Log.e(TAG, "Driver not found: " + constructorId);
121+
Log.e(TAG, "Constructor not found: " + constructorId);
122+
throw new RuntimeException("Constructor not found in local database: " + constructorId);
122123
}
123124
}
124125

125126
@Override
126127
public void onError(Exception e) {
127-
Log.e(TAG, "Error loading driver from local database: " + e.getMessage());
128+
Log.e(TAG, "Error loading constructor from local database: " + e.getMessage());
129+
Objects.requireNonNull(constructorCache.get(constructorId)).postValue(
130+
new Result.Error("Error loading constructor from local database: " + e.getMessage()));
128131
}
129132
});
130133
}

app/src/main/java/com/the_coffe_coders/fastestlap/repository/driver/DriverRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ public void onDriverLoaded(Driver driver) {
119119
} else {
120120
Objects.requireNonNull(driverCache.get(driverId)).postValue(
121121
new Result.Error("Driver not found locally and no network connection available"));
122+
123+
throw new RuntimeException("Driver not found in local database: " + driverId);
122124
}
123125
}
124126
}

app/src/main/java/com/the_coffe_coders/fastestlap/util/LoadingScreen.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public LoadingScreen(View view, Context context, View activityView, View fragmen
3737
}
3838

3939
public void showLoadingScreen(boolean invisible) {
40+
resetTimer();
4041
if (fragmentView != null) {
4142
if (invisible) {
4243
fragmentView.setVisibility(View.INVISIBLE);

0 commit comments

Comments
 (0)