Skip to content
This repository has been archived by the owner on Feb 1, 2025. It is now read-only.

Update media to provide image #48

Merged
merged 13 commits into from
Mar 23, 2023
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/androidTestResultsUserPreferences.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ dependencies {

testImplementation 'junit:junit:4.13.2'
testImplementation 'com.squareup.okhttp3:mockwebserver:5.0.0-alpha.11'
androidTestImplementation 'com.squareup.okhttp3:mockwebserver:5.0.0-alpha.11'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test:runner:1.5.2'
androidTestImplementation 'androidx.test:rules:1.5.0'
Expand Down Expand Up @@ -89,12 +90,15 @@ dependencies {
implementation 'com.google.firebase:firebase-storage'
implementation 'com.google.firebase:firebase-auth'
implementation 'com.google.firebase:firebase-firestore'
implementation 'com.google.firebase:firebase-analytics:17.2.1'
implementation 'com.google.firebase:firebase-core:11.0.4'
implementation 'com.google.firebase:firebase-database:11.0.4'
implementation 'com.google.firebase:firebase-analytics:21.2.0'
implementation 'com.google.firebase:firebase-core:21.1.1'
implementation 'com.google.firebase:firebase-database:20.1.0'
implementation 'com.firebaseui:firebase-ui-auth:7.2.0'
implementation 'com.google.android.gms:play-services-auth:20.4.1'
implementation platform('com.google.firebase:firebase-bom:31.2.2')
// Glide image downloader and caching
implementation 'com.github.bumptech.glide:glide:4.15.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.15.0'

androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void testMovieItem() {
ViewInteraction movieItem = onView(withId(R.id.test_movie_item));
movieItem.check(matches(isDisplayed()));
movieItem.check(matches(hasDescendant(withText("Skyfall"))));
movieItem.check(matches(hasDescendant(withText("Rating: 10"))));
movieItem.check(matches(hasDescendant(withText("Rating: "))));
}

}
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:icon="@drawable/movie1"
android:label="@string/mt_app_name"
android:supportsRtl="true"
android:theme="@style/Theme.MediaTo"
Expand All @@ -28,7 +29,9 @@
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="false" />
android:exported="false" >

</activity>

<activity
android:name=".AuthenticationActivity"
Expand Down
35 changes: 32 additions & 3 deletions app/src/main/java/com/github/sdp/mediato/HomeFragment.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,46 @@
package com.github.sdp.mediato;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.sdp.mediato.databinding.FragmentHomeBinding;

public class HomeFragment extends Fragment {
private HomeViewModel viewModel;
private FragmentHomeBinding binding;
private MediaRecyclerViewAdapter adapter;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
public View onCreateView( @NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false);
return binding.getRoot();
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
viewModel = new ViewModelProvider(this).get(HomeViewModel.class);
adapter = new MediaRecyclerViewAdapter();
binding.trendingItems.setAdapter(adapter);
binding.trendingItems.setLayoutManager(new GridLayoutManager(getContext(), 3));
binding.trendingItems.setHasFixedSize(true);
viewModel.getMovies().observe(getViewLifecycleOwner(), adapter::submitList);


}
}
37 changes: 37 additions & 0 deletions app/src/main/java/com/github/sdp/mediato/HomeViewModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.github.sdp.mediato;


import android.app.Application;

import androidx.annotation.Nullable;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.github.sdp.mediato.api.themoviedb.TheMovieDB;
import com.github.sdp.mediato.model.media.Media;
import com.github.sdp.mediato.model.media.Movie;

import java.util.List;
import java.util.stream.Collectors;

public class HomeViewModel extends AndroidViewModel {

Application application;
private final MutableLiveData<List<Media>> movies = new MutableLiveData<>();
private final TheMovieDB api;

public HomeViewModel(Application application){
super(application);
this.application = application;
api = new TheMovieDB(application.getApplicationContext().getString(R.string.tmdb_url),
application.getApplicationContext().getString(R.string.TMDBAPIKEY));
}
public MutableLiveData<List<Media>> getMovies() {
api.trending(20).thenAccept( list -> {
movies.setValue(list.stream().map(Movie::new).collect(Collectors.toList()));
});
return movies;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.github.sdp.mediato;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.LiveData;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.github.sdp.mediato.databinding.LayoutMovieItemBinding;
import com.github.sdp.mediato.model.media.Media;


public class MediaRecyclerViewAdapter extends ListAdapter<Media, MediaRecyclerViewAdapter.MyViewHolder> {
private static final DiffUtil.ItemCallback<Media> MEDIA_COMPARATOR = new DiffUtil.ItemCallback<>() {
@Override
public boolean areItemsTheSame(@NonNull Media oldItem, @NonNull Media newItem) {
return oldItem.getId() == newItem.getId() && oldItem.getMediaType() == newItem.getMediaType();
}

@Override
public boolean areContentsTheSame(@NonNull Media oldItem, @NonNull Media newItem) {
return areItemsTheSame(oldItem, newItem);
}
};

protected MediaRecyclerViewAdapter() {
super(MEDIA_COMPARATOR);
}

@NonNull
@Override
public MediaRecyclerViewAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.layout_movie_item, parent, false);
return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
// Todo add placeholder
holder.title.setText(getItem(position).getTitle());
Glide.with(holder.itemView.getContext())
.load(getItem(position).getImageURL())
.into(holder.image);
}

// Todo can extend View.OnclickListener to implement the OnClickCallback
public static class MyViewHolder extends RecyclerView.ViewHolder {
private final TextView title;
private final ImageView image;

public MyViewHolder(@NonNull View itemView) {
super(itemView);
image = (ImageView) itemView.findViewById(R.id.media_cover);
title = (TextView) itemView.findViewById(R.id.text_title);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.sdp.mediato.api.themoviedb;

import androidx.lifecycle.MutableLiveData;

import com.github.sdp.mediato.api.API;
import com.github.sdp.mediato.api.themoviedb.models.PagedResult;
import com.github.sdp.mediato.api.themoviedb.models.TMDBMovie;
Expand All @@ -26,12 +28,16 @@ public class TheMovieDB implements API<TMDBMovie> {
private List<TMDBMovie> trendingCache;
private int trendingPage;

private MutableLiveData<TMDBMovie> livedata;

/**
* Default constructor
* @param serverUrl domain name of the api (used to inject tests)
* @param apikey the key provided by TheMovieDB to use their API
*/
public TheMovieDB(String serverUrl, String apikey){
Preconditions.checkNullOrEmptyString(serverUrl, "serverUrl");
Preconditions.checkNullOrEmptyString(apikey, "apikey");
this.trendingCache = new ArrayList<>();
this.trendingPage = 0;
this.searchCache = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Class used by the retrofit library to store the movies returned by TheMovieDB api.
*/
public final class TMDBMovie {
private static final String BASE_URL = "https://image.tmdb.org/t/p/original";
private String poster_path;
private boolean adult;
private String overview;
Expand Down Expand Up @@ -34,6 +35,10 @@ public String getTitle() {
}

public String getPoster_path() {
return poster_path;
return BASE_URL + poster_path;
}

public String getOverview() {
return overview;
}
}
36 changes: 28 additions & 8 deletions app/src/main/java/com/github/sdp/mediato/model/media/Media.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package com.github.sdp.mediato.model.media;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;

import com.github.sdp.mediato.errorCheck.Preconditions;

import java.util.Objects;
import java.io.InputStream;
import java.net.URL;
import java.util.concurrent.CompletableFuture;

public class Media {

Expand All @@ -11,22 +17,36 @@ public class Media {
private String summary;
private String imageUrl;

private int id;

Media(){}
Media() {
}

public Media(MediaType mediaType, String title, String summary, String imageUrl) {
Preconditions.checkMedia(mediaType,title, summary, imageUrl);
public Media(MediaType mediaType, String title, String summary, String imageUrl, int id) {
Preconditions.checkMedia(mediaType, title, summary, imageUrl);
this.mediaType = mediaType;
this.title = title;
this.summary = summary;
this.imageUrl = imageUrl;
this.id = id;
}

public MediaType getMediaType() {
return mediaType;
}

public MediaType getMediaType() {return mediaType;}
public String getTitle() {
return title;
}

public String getSummary() {
return summary;
}

public String getTitle() {return title;}
public String getImageURL() {
return imageUrl;
}

public String getSummary() {return summary;}
public String getimageUrl() {return imageUrl;}
public int getId() { return id; }

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.github.sdp.mediato.model.media;

import com.github.sdp.mediato.api.themoviedb.models.TMDBMovie;
import com.github.sdp.mediato.model.media.MediaType;

public class Movie extends Media{

private Movie(){super();}

public Movie(String title, String summary, String imageUrl) {
super(MediaType.MOVIE, title, summary, imageUrl);
public Movie(String title, String summary, String imageUrl, int id) {
super(MediaType.MOVIE, title, summary, imageUrl, id);
}
public Movie(TMDBMovie tmdbMovie){
this(tmdbMovie.getTitle(), tmdbMovie.getOverview(),
tmdbMovie.getPoster_path(), tmdbMovie.getId());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public void onResponse(Call<T> call, Response<T> response) {
*/
@Override @EverythingIsNonNull
public void onFailure(Call<T> call, Throwable t) {
System.out.println("failure");
future.completeExceptionally(t);
}
}
Loading