Skip to content

Commit 7810403

Browse files
Refactor and update dependencies
This commit includes several changes: - Updates Hilt and Mockito Core dependencies. - Refactors `FaqItem` in `HelpActivity.java` to a record. - Simplifies response body handling in `RetrofitActivity.java`. - Removes unused fields in test classes. - Adds null checks for method invocations in test classes. - Adds `tools:ignore="VectorRaster"` to `bg_contact_support_icon.xml`. - Adds `tools:targetApi="26"` for `android:tooltipText` in `item_android_studio_lesson.xml`.
1 parent 57dd872 commit 7810403

File tree

8 files changed

+30
-39
lines changed

8 files changed

+30
-39
lines changed

app/src/main/java/com/d4rk/androidtutorials/java/ui/screens/android/lessons/networking/retrofit/RetrofitActivity.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
import com.d4rk.androidtutorials.java.utils.EdgeToEdgeDelegate;
1616
import com.google.gson.annotations.SerializedName;
1717

18-
import java.util.Map;
19-
2018
import retrofit2.Call;
2119
import retrofit2.Callback;
2220
import retrofit2.Response;
@@ -79,19 +77,12 @@ protected void onDestroy() {
7977
}
8078

8179
private void displayTodoTitle(@NonNull Response<Todo> response) {
82-
Object body = response.body();
83-
if (body instanceof Todo) {
84-
Todo todo = (Todo) body;
80+
Todo todo = response.body();
81+
if (todo != null) {
8582
if (todo.title != null && !todo.title.isEmpty()) {
8683
binding.textViewResult.setText(todo.title);
8784
return;
8885
}
89-
} else if (body instanceof Map<?, ?> map) {
90-
Object title = map.get("title");
91-
if (title != null) {
92-
binding.textViewResult.setText(String.valueOf(title));
93-
return;
94-
}
9586
}
9687
showGeneralErrorMessage();
9788
}

app/src/main/java/com/d4rk/androidtutorials/java/ui/screens/help/HelpActivity.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.view.Menu;
1111
import android.view.MenuItem;
1212
import android.view.View;
13+
1314
import androidx.annotation.NonNull;
1415
import androidx.annotation.StringRes;
1516
import androidx.appcompat.app.AlertDialog;
@@ -29,12 +30,12 @@
2930
import com.google.android.material.snackbar.Snackbar;
3031
import com.google.android.play.core.review.ReviewInfo;
3132

32-
import dagger.hilt.android.AndroidEntryPoint;
33-
import me.zhanghai.android.fastscroll.FastScrollerBuilder;
34-
3533
import java.util.Arrays;
3634
import java.util.List;
3735

36+
import dagger.hilt.android.AndroidEntryPoint;
37+
import me.zhanghai.android.fastscroll.FastScrollerBuilder;
38+
3839
@AndroidEntryPoint
3940
public class HelpActivity extends BaseActivity {
4041

@@ -235,16 +236,7 @@ private void toggleFaqItem(ItemHelpFaqBinding binding) {
235236
ViewCompat.setStateDescription(binding.questionContainer, stateDescription);
236237
}
237238

238-
private static final class FaqItem {
239-
@StringRes
240-
private final int questionResId;
241-
@StringRes
242-
private final int answerResId;
243-
244-
private FaqItem(@StringRes int questionResId, @StringRes int answerResId) {
245-
this.questionResId = questionResId;
246-
this.answerResId = answerResId;
247-
}
239+
private record FaqItem(@StringRes int questionResId, @StringRes int answerResId) {
248240
}
249241

250242
@Override

app/src/main/res/drawable/bg_contact_support_icon.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<vector xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
34
android:width="289dp"
45
android:height="289dp"
56
android:viewportWidth="289"
6-
android:viewportHeight="289">
7+
android:viewportHeight="289"
8+
tools:ignore="VectorRaster">
79

810
<path
911
android:fillColor="?attr/colorSecondaryContainer"

app/src/main/res/layout/item_android_studio_lesson.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
45
android:id="@+id/lesson_card"
56
style="@style/Widget.Material3.CardView.Outlined"
67
android:layout_width="match_parent"
@@ -54,6 +55,7 @@
5455
android:contentDescription="@string/lesson_open_in_browser"
5556
android:tooltipText="@string/lesson_open_in_browser"
5657
android:visibility="gone"
57-
app:icon="@drawable/ic_open_in_new" />
58+
app:icon="@drawable/ic_open_in_new"
59+
tools:targetApi="26" />
5860
</androidx.appcompat.widget.LinearLayoutCompat>
5961
</com.google.android.material.card.MaterialCardView>

app/src/test/java/com/d4rk/androidtutorials/java/ads/managers/AppOpenAdManagerTest.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,16 @@
3131
import java.util.concurrent.TimeUnit;
3232

3333
/**
34-
* Unit tests for {@link AppOpenAd.AppOpenAdManager}.
34+
* Unit tests for AppOpenAd.AppOpenAdManager.
3535
*/
3636
public class AppOpenAdManagerTest {
3737

3838
private Class<?> managerClass;
3939
private Object manager;
40-
private Application application;
4140

4241
@Before
4342
public void setUp() throws Exception {
44-
application = mock(Application.class);
43+
Application application = mock(Application.class);
4544
when(application.getApplicationContext()).thenReturn(application);
4645

4746
managerClass = findManagerClass();
@@ -143,7 +142,11 @@ private Class<?> findManagerClass() {
143142
private boolean invokeIsAdAvailable() throws Exception {
144143
Method method = managerClass.getDeclaredMethod("isAdAvailable");
145144
method.setAccessible(true);
146-
return (boolean) method.invoke(manager);
145+
Object result = method.invoke(manager);
146+
if (result instanceof Boolean) {
147+
return (boolean) result;
148+
}
149+
throw new IllegalStateException("isAdAvailable method did not return a boolean value.");
147150
}
148151

149152
private void invokeShowAdIfAvailable(Activity activity, OnShowAdCompleteListener listener)

app/src/test/java/com/d4rk/androidtutorials/java/data/repository/DefaultMainRepositoryTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public class DefaultMainRepositoryTest {
3636

3737
private Context context;
3838
private FakeSharedPreferences defaultPrefs;
39-
private FakeSharedPreferences startupPrefs;
4039
private AppUpdateManager updateManager;
4140
private MockedStatic<PreferenceManager> prefManager;
4241
private MockedStatic<AppUpdateManagerFactory> updateManagerFactory;
@@ -46,7 +45,7 @@ public class DefaultMainRepositoryTest {
4645
public void setUp() {
4746
context = mock(Context.class);
4847
defaultPrefs = new FakeSharedPreferences();
49-
startupPrefs = new FakeSharedPreferences();
48+
FakeSharedPreferences startupPrefs = new FakeSharedPreferences();
5049
updateManager = mock(AppUpdateManager.class);
5150

5251
prefManager = mockStatic(PreferenceManager.class);

app/src/test/java/com/d4rk/androidtutorials/java/ui/screens/main/MainViewModelTest.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,8 @@ public class MainViewModelTest {
3434
private ApplyThemeSettingsUseCase applyThemeSettingsUseCase;
3535
private GetBottomNavLabelVisibilityUseCase getBottomNavLabelVisibilityUseCase;
3636
private GetDefaultTabPreferenceUseCase getDefaultTabPreferenceUseCase;
37-
private ApplyLanguageSettingsUseCase applyLanguageSettingsUseCase;
3837
private ShouldShowStartupScreenUseCase shouldShowStartupScreenUseCase;
3938
private MarkStartupScreenShownUseCase markStartupScreenShownUseCase;
40-
private GetAppUpdateManagerUseCase getAppUpdateManagerUseCase;
4139
private MainViewModel viewModel;
4240

4341
private final String[] themeValues = {"MODE_NIGHT_FOLLOW_SYSTEM", "MODE_NIGHT_NO", "MODE_NIGHT_YES"};
@@ -49,10 +47,10 @@ public void setUp() {
4947
applyThemeSettingsUseCase = Mockito.mock(ApplyThemeSettingsUseCase.class);
5048
getBottomNavLabelVisibilityUseCase = Mockito.mock(GetBottomNavLabelVisibilityUseCase.class);
5149
getDefaultTabPreferenceUseCase = Mockito.mock(GetDefaultTabPreferenceUseCase.class);
52-
applyLanguageSettingsUseCase = Mockito.mock(ApplyLanguageSettingsUseCase.class);
50+
ApplyLanguageSettingsUseCase applyLanguageSettingsUseCase = Mockito.mock(ApplyLanguageSettingsUseCase.class);
5351
shouldShowStartupScreenUseCase = Mockito.mock(ShouldShowStartupScreenUseCase.class);
5452
markStartupScreenShownUseCase = Mockito.mock(MarkStartupScreenShownUseCase.class);
55-
getAppUpdateManagerUseCase = Mockito.mock(GetAppUpdateManagerUseCase.class);
53+
GetAppUpdateManagerUseCase getAppUpdateManagerUseCase = Mockito.mock(GetAppUpdateManagerUseCase.class);
5654

5755
viewModel = new MainViewModel(
5856
applyThemeSettingsUseCase,
@@ -144,7 +142,11 @@ private int callVisibilityMode(String value) throws Exception {
144142
Method method = MainViewModel.class.getDeclaredMethod(
145143
"getVisibilityMode", String.class, String[].class);
146144
method.setAccessible(true);
147-
return (Integer) method.invoke(null, value, bottomNavBarLabelsValues);
145+
Object result = method.invoke(null, value, bottomNavBarLabelsValues);
146+
if (result == null) {
147+
throw new NullPointerException("Method invocation returned null");
148+
}
149+
return (Integer) result;
148150
}
149151

150152
@Test

gradle/libs.versions.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ library = "1.3.0"
1717
libraryVersion = "1.4.0"
1818
lifecycle = "2.9.4"
1919
lottie = "6.6.9"
20-
mockitoCore = "5.19.0"
20+
mockitoCore = "5.20.0"
2121
mockitoInline = "5.2.0"
2222
navigationUi = "2.9.5"
2323
preference = "1.2.1"
@@ -29,7 +29,7 @@ multidex = "2.0.1"
2929
playServicesAds = "24.6.0"
3030
playServicesOssLicenses = "17.3.0"
3131
codeview = "1.3.9"
32-
hilt = "2.57.1"
32+
hilt = "2.57.2"
3333
room = "2.8.1"
3434
glide = "5.0.5"
3535
retrofit = "3.0.0"

0 commit comments

Comments
 (0)