Skip to content

Commit e7c627a

Browse files
feat(top): add top anime fetch
1 parent c05e533 commit e7c627a

File tree

17 files changed

+465
-92
lines changed

17 files changed

+465
-92
lines changed

.idea/appInsightsSettings.xml

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle.kts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ dependencies {
3939
testImplementation("junit:junit:4.13.2")
4040
androidTestImplementation("androidx.test.ext:junit:1.1.5")
4141
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
42-
compileOnly("org.projectlombok:lombok:1.18.30")
4342
annotationProcessor("org.projectlombok:lombok:1.18.30")
43+
implementation("com.squareup.okhttp3:okhttp:4.12.0")
44+
implementation("com.fasterxml.jackson.core:jackson-databind:2.16.1")
45+
implementation("com.github.bumptech.glide:glide:4.16.0")
46+
annotationProcessor("com.github.bumptech.glide:compiler:4.16.0")
47+
compileOnly("com.github.bumptech.glide:annotations:4.16.0")
48+
annotationProcessor("com.github.bumptech.glide:compiler:4.16.0")
4449
}

app/src/androidTest/java/com/luka/anidroid/ExampleInstrumentedTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package com.luka.anidroid;
22

3+
import static org.junit.Assert.assertEquals;
4+
35
import android.content.Context;
46

5-
import androidx.test.platform.app.InstrumentationRegistry;
67
import androidx.test.ext.junit.runners.AndroidJUnit4;
8+
import androidx.test.platform.app.InstrumentationRegistry;
79

810
import org.junit.Test;
911
import org.junit.runner.RunWith;
1012

11-
import static org.junit.Assert.*;
12-
1313
/**
1414
* Instrumented test, which will execute on an Android device.
1515
*

app/src/main/AndroidManifest.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/tools">
44

5+
<uses-permission android:name="android.permission.INTERNET" />
6+
7+
58
<application
69
android:allowBackup="true"
10+
android:enableOnBackInvokedCallback="true"
711
android:dataExtractionRules="@xml/data_extraction_rules"
812
android:fullBackupContent="@xml/backup_rules"
913
android:icon="@mipmap/ic_launcher"

app/src/main/java/com/luka/anidroid/HomeFragment.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import android.view.ViewGroup;
88

99
import androidx.fragment.app.Fragment;
10-
import androidx.recyclerview.widget.LinearLayoutManager;
10+
import androidx.recyclerview.widget.GridLayoutManager;
1111
import androidx.recyclerview.widget.RecyclerView;
1212

1313
import com.luka.anidroid.adapter.AnimeAdapter;
@@ -33,7 +33,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
3333
animeList = new ArrayList<>();
3434
animeAdapter = new AnimeAdapter(animeList);
3535

36-
final LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
36+
final GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 2);
3737
recyclerView.setLayoutManager(layoutManager);
3838
recyclerView.setAdapter(animeAdapter);
3939

@@ -50,21 +50,15 @@ public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
5050
}
5151
});
5252

53-
loadAnimeData(currentPage);
54-
5553
return view;
5654
}
5755

5856
private void loadAnimeData(int page) {
5957
// Make network request to Anilist API, add data to animeList, and notify adapter
6058
// MOCK DATA
6159
Log.d("HomeFragment", "loadAnimeData called with page: " + page);
62-
animeList.add(new Anime("Naruto", "https://cdn.myanimelist.net/images/anime/13/17405.jpg", "Naruto is a young ninja who seeks recognition from his peers and dreams of becoming the Hokage, the leader of his village.", 2002, "TV"));
63-
animeList.add(new Anime("One Piece", "https://cdn.myanimelist.net/images/anime/6/73245.jpg", "Gol D. Roger was known as the Pirate King, the strongest and most infamous being to have sailed the Grand Line. The capture and execution of Roger by the World Government brought a change throughout the world.", 1999, "TV"));
64-
animeList.add(new Anime("Bleach", "https://cdn.myanimelist.net/images/anime/3/40451.jpg", "15-year-old Kurosaki Ichigo is not your everyday high school student. He has from as far he can remember always had the ability to see ghosts and spirits.", 2004, "TV"));
65-
animeList.add(new Anime("Dragon Ball Z", "https://cdn.myanimelist.net/images/anime/6/50135.jpg", "Five years after winning the World Martial Arts tournament, Gokuu is now living a peaceful life with his wife and son.", 1989, "TV"));
66-
animeList.add(new Anime("Attack on Titan", "https://cdn.myanimelist.net/images/anime/10/47347.jpg", "Centuries ago, mankind was slaughtered to near extinction by monstrous humanoid creatures called titans, forcing humans to hide in fear behind enormous concentric walls.", 2013, "TV"));
67-
// After adding anime data
60+
List<Anime> newAnimeList = new ArrayList<>();
61+
6862
Log.d("HomeFragment", "Number of anime in the list: " + animeList.size());
6963

7064
animeAdapter.notifyDataSetChanged();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.luka.anidroid;
2+
3+
import com.bumptech.glide.annotation.GlideModule;
4+
import com.bumptech.glide.module.AppGlideModule;
5+
6+
@GlideModule
7+
public final class MyAppGlideModule extends AppGlideModule {
8+
// Leave this class empty for now
9+
}

app/src/main/java/com/luka/anidroid/activity/MainActivity.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,44 @@
11
package com.luka.anidroid.activity;
22

3+
import android.os.Bundle;
4+
35
import androidx.appcompat.app.AppCompatActivity;
46
import androidx.fragment.app.Fragment;
57

6-
import android.os.Bundle;
7-
88
import com.google.android.material.bottomnavigation.BottomNavigationView;
99
import com.luka.anidroid.R;
1010
import com.luka.anidroid.fragment.HomeFragment;
1111

1212
public class MainActivity extends AppCompatActivity {
1313

14+
@Override
15+
protected void onSaveInstanceState(Bundle outState) {
16+
super.onSaveInstanceState(outState);
17+
// Save the state of your activity or any data you want to retain
18+
// For example, you can save the selected item id of the BottomNavigationView
19+
outState.putInt("selectedItemId", bottomNavigationView.getSelectedItemId());
20+
}
21+
22+
@Override
23+
protected void onRestoreInstanceState(Bundle savedInstanceState) {
24+
super.onRestoreInstanceState(savedInstanceState);
25+
// Restore the saved state of your activity or any data
26+
// For example, you can restore the selected item id of the BottomNavigationView
27+
int selectedItemId = savedInstanceState.getInt("selectedItemId");
28+
bottomNavigationView.setSelectedItemId(selectedItemId);
29+
}
30+
31+
BottomNavigationView bottomNavigationView;
32+
1433
@Override
1534
protected void onCreate(Bundle savedInstanceState) {
1635
super.onCreate(savedInstanceState);
1736
setContentView(R.layout.activity_main);
1837

19-
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
38+
// Set HomeFragment as default fragment
39+
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();
40+
41+
bottomNavigationView = findViewById(R.id.bottom_navigation);
2042

2143
// Set home item as selected
2244
bottomNavigationView.setSelectedItemId(R.id.navigation_home);
@@ -26,7 +48,7 @@ protected void onCreate(Bundle savedInstanceState) {
2648

2749
if (item.getItemId() == R.id.navigation_home) {
2850
selectedFragment = new HomeFragment();
29-
}else if (item.getItemId() == R.id.navigation_profile) {
51+
} else if (item.getItemId() == R.id.navigation_profile) {
3052
//selectedFragment = new ProfileFragment();
3153
} else if (item.getItemId() == R.id.navigation_settings) {
3254
//selectedFragment = new SettingsFragment();

app/src/main/java/com/luka/anidroid/adapter/AnimeAdapter.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.luka.anidroid.adapter;
22

3+
import android.util.Log;
34
import android.view.LayoutInflater;
45
import android.view.View;
56
import android.view.ViewGroup;
@@ -9,6 +10,7 @@
910
import androidx.annotation.NonNull;
1011
import androidx.recyclerview.widget.RecyclerView;
1112

13+
import com.bumptech.glide.Glide;
1214
import com.luka.anidroid.R;
1315
import com.luka.anidroid.model.Anime;
1416

@@ -32,11 +34,29 @@ public AnimeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
3234
@Override
3335
public void onBindViewHolder(AnimeViewHolder holder, int position) {
3436
Anime anime = animeList.get(position);
35-
// Bind data to views in holder
3637
holder.animeTitle.setText(anime.getTitle());
37-
holder.animeDescription.setText(anime.getDescription());
38-
// skip image loading for now
38+
holder.animeImage.setImageResource(R.drawable.ic_launcher_foreground);
39+
//holder.animeDescription.setText(anime.getDescription());
40+
Log.d("AnimeAdapter", "image url: " + anime.getImageUrl());
41+
if (anime.getImageUrl() != null && !anime.getImageUrl().isEmpty()) {
42+
Glide.with(holder.itemView.getContext())
43+
.load(anime.getImageUrl())
44+
.placeholder(R.drawable.ic_launcher_foreground) // Show placeholder while image is loading
45+
.error(R.drawable.ic_launcher_foreground) // Show placeholder if there's an error loading the image
46+
.into(holder.animeImage);
47+
} else {
48+
Glide.with(holder.itemView.getContext())
49+
.load(R.drawable.ic_launcher_foreground)
50+
.into(holder.animeImage);
51+
}
52+
holder.animeScore.setText(String.valueOf(anime.getAverageScore()));
53+
}
3954

55+
@Override
56+
public void onViewRecycled(@NonNull AnimeViewHolder holder) {
57+
super.onViewRecycled(holder);
58+
// Clear the image from the recycled view
59+
Glide.with(holder.itemView.getContext()).clear(holder.animeImage);
4060
}
4161

4262
@Override
@@ -49,11 +69,14 @@ public static class AnimeViewHolder extends RecyclerView.ViewHolder {
4969
TextView animeTitle;
5070
TextView animeDescription;
5171
TextView animeYear;
72+
TextView animeScore;
5273

5374
public AnimeViewHolder(View itemView) {
5475
super(itemView);
5576
animeTitle = itemView.findViewById(R.id.anime_title);
5677
animeDescription = itemView.findViewById(R.id.anime_description);
78+
animeImage = itemView.findViewById(R.id.anime_image);
79+
animeScore = itemView.findViewById(R.id.anime_score);
5780
}
5881
}
5982
}

0 commit comments

Comments
 (0)