Skip to content

Commit 3573fdd

Browse files
committed
code challenge
1 parent b4e899b commit 3573fdd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1036
-0
lines changed

.gitignore

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Built application files
2+
*.apk
3+
*.ap_
4+
5+
# Files for the ART/Dalvik VM
6+
*.dex
7+
8+
# Java class files
9+
*.class
10+
11+
# Generated files
12+
bin/
13+
gen/
14+
out/
15+
16+
# Gradle files
17+
.gradle/
18+
build/
19+
20+
# Local configuration file (sdk path, etc)
21+
local.properties
22+
23+
# Proguard folder generated by Eclipse
24+
proguard/
25+
26+
# Log Files
27+
*.log
28+
29+
# Android Studio Navigation editor temp files
30+
.navigation/
31+
32+
# Android Studio captures folder
33+
captures/
34+
35+
# IntelliJ
36+
*.iml
37+
.idea/*
38+

app/.DS_Store

6 KB
Binary file not shown.

app/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
apply plugin: 'com.android.application'
2+
3+
apply plugin: 'kotlin-android'
4+
5+
apply plugin: 'kotlin-android-extensions'
6+
7+
android {
8+
compileSdkVersion 27
9+
defaultConfig {
10+
applicationId "com.arctouch.codechallenge"
11+
minSdkVersion 19
12+
targetSdkVersion 27
13+
versionCode 1
14+
versionName "1.0"
15+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
16+
}
17+
buildTypes {
18+
release {
19+
minifyEnabled false
20+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
21+
}
22+
}
23+
}
24+
25+
dependencies {
26+
implementation fileTree(dir: 'libs', include: ['*.jar'])
27+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
28+
implementation "com.android.support:appcompat-v7:$support_library_version"
29+
implementation "com.android.support:recyclerview-v7:$support_library_version"
30+
implementation "com.android.support:cardview-v7:$support_library_version"
31+
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
32+
33+
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
34+
implementation 'com.squareup.retrofit2:converter-moshi:2.3.0'
35+
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
36+
implementation "io.reactivex.rxjava2:rxandroid:2.0.2"
37+
implementation 'com.github.bumptech.glide:glide:4.6.1'
38+
39+
testImplementation 'junit:junit:4.12'
40+
androidTestImplementation 'com.android.support.test:runner:1.0.1'
41+
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
42+
}

app/proguard-rules.pro

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile

app/src/.DS_Store

6 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.arctouch.codechallenge
2+
3+
import android.support.test.InstrumentationRegistry
4+
import android.support.test.runner.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.*
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getTargetContext()
22+
assertEquals("com.arctouch.codechallenge", appContext.packageName)
23+
}
24+
}

app/src/main/AndroidManifest.xml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.arctouch.codechallenge">
4+
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
7+
<application
8+
android:allowBackup="true"
9+
android:icon="@mipmap/ic_launcher"
10+
android:label="@string/app_name"
11+
android:roundIcon="@mipmap/ic_launcher_round"
12+
android:supportsRtl="true"
13+
android:theme="@style/AppTheme">
14+
15+
<activity
16+
android:name=".splash.SplashActivity"
17+
android:theme="@style/Theme.AppCompat.NoActionBar">
18+
<intent-filter>
19+
<action android:name="android.intent.action.MAIN" />
20+
<category android:name="android.intent.category.LAUNCHER" />
21+
</intent-filter>
22+
</activity>
23+
24+
<activity android:name=".home.HomeActivity" />
25+
26+
</application>
27+
28+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.arctouch.codechallenge.api
2+
3+
import com.arctouch.codechallenge.model.GenreResponse
4+
import com.arctouch.codechallenge.model.Movie
5+
import com.arctouch.codechallenge.model.UpcomingMoviesResponse
6+
import io.reactivex.Observable
7+
import retrofit2.http.GET
8+
import retrofit2.http.Path
9+
import retrofit2.http.Query
10+
11+
interface TmdbApi {
12+
13+
companion object {
14+
const val URL = "https://api.themoviedb.org/3/"
15+
const val API_KEY = "1f54bd990f1cdfb230adb312546d765d"
16+
const val DEFAULT_LANGUAGE = "pt-BR"
17+
const val DEFAULT_REGION = "BR"
18+
}
19+
20+
@GET("genre/movie/list")
21+
fun genres(
22+
@Query("api_key") apiKey: String,
23+
@Query("language") language: String
24+
): Observable<GenreResponse>
25+
26+
@GET("movie/upcoming")
27+
fun upcomingMovies(
28+
@Query("api_key") apiKey: String,
29+
@Query("language") language: String,
30+
@Query("page") page: Long,
31+
@Query("region") region: String
32+
): Observable<UpcomingMoviesResponse>
33+
34+
@GET("movie/{id}")
35+
fun movie(
36+
@Path("id") id: Long,
37+
@Query("api_key") apiKey: String,
38+
@Query("language") language: String
39+
): Observable<Movie>
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.arctouch.codechallenge.base
2+
3+
import android.support.v7.app.AppCompatActivity
4+
import com.arctouch.codechallenge.api.TmdbApi
5+
import okhttp3.OkHttpClient
6+
import retrofit2.Retrofit
7+
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
8+
import retrofit2.converter.moshi.MoshiConverterFactory
9+
10+
abstract class BaseActivity : AppCompatActivity() {
11+
12+
protected val api: TmdbApi = Retrofit.Builder()
13+
.baseUrl(TmdbApi.URL)
14+
.client(OkHttpClient.Builder().build())
15+
.addConverterFactory(MoshiConverterFactory.create())
16+
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
17+
.build()
18+
.create(TmdbApi::class.java)
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.arctouch.codechallenge.data
2+
3+
import com.arctouch.codechallenge.model.Genre
4+
5+
object Cache {
6+
7+
var genres = listOf<Genre>()
8+
9+
fun cacheGenres(genres: List<Genre>) {
10+
this.genres = genres
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.arctouch.codechallenge.home
2+
3+
import android.os.Bundle
4+
import android.view.View
5+
import com.arctouch.codechallenge.R
6+
import com.arctouch.codechallenge.api.TmdbApi
7+
import com.arctouch.codechallenge.base.BaseActivity
8+
import com.arctouch.codechallenge.data.Cache
9+
import io.reactivex.android.schedulers.AndroidSchedulers
10+
import io.reactivex.schedulers.Schedulers
11+
import kotlinx.android.synthetic.main.home_activity.*
12+
13+
class HomeActivity : BaseActivity() {
14+
15+
override fun onCreate(savedInstanceState: Bundle?) {
16+
super.onCreate(savedInstanceState)
17+
setContentView(R.layout.home_activity)
18+
19+
api.upcomingMovies(TmdbApi.API_KEY, TmdbApi.DEFAULT_LANGUAGE, 1, TmdbApi.DEFAULT_REGION)
20+
.subscribeOn(Schedulers.io())
21+
.observeOn(AndroidSchedulers.mainThread())
22+
.subscribe {
23+
val moviesWithGenres = it.results.map { movie ->
24+
movie.copy(genres = Cache.genres.filter { movie.genreIds?.contains(it.id) == true })
25+
}
26+
recyclerView.adapter = HomeAdapter(moviesWithGenres)
27+
progressBar.visibility = View.GONE
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.arctouch.codechallenge.home
2+
3+
import android.support.v7.widget.RecyclerView
4+
import android.view.LayoutInflater
5+
import android.view.View
6+
import android.view.ViewGroup
7+
import com.arctouch.codechallenge.R
8+
import com.arctouch.codechallenge.model.Movie
9+
import com.arctouch.codechallenge.util.MovieImageUrlBuilder
10+
import com.bumptech.glide.Glide
11+
import com.bumptech.glide.request.RequestOptions
12+
import kotlinx.android.synthetic.main.movie_item.view.*
13+
14+
class HomeAdapter(private val movies: List<Movie>) : RecyclerView.Adapter<HomeAdapter.ViewHolder>() {
15+
16+
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
17+
18+
private val movieImageUrlBuilder = MovieImageUrlBuilder()
19+
20+
fun bind(movie: Movie) {
21+
itemView.titleTextView.text = movie.title
22+
itemView.genresTextView.text = movie.genres?.joinToString(separator = ", ") { it.name }
23+
itemView.releaseDateTextView.text = movie.releaseDate
24+
25+
Glide.with(itemView)
26+
.load(movie.posterPath?.let { movieImageUrlBuilder.buildPosterUrl(it) })
27+
.apply(RequestOptions().placeholder(R.drawable.ic_image_placeholder))
28+
.into(itemView.posterImageView)
29+
}
30+
}
31+
32+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
33+
val view = LayoutInflater.from(parent.context).inflate(R.layout.movie_item, parent, false)
34+
return ViewHolder(view)
35+
}
36+
37+
override fun getItemCount() = movies.size
38+
39+
override fun onBindViewHolder(holder: ViewHolder, position: Int) = holder.bind(movies[position])
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.arctouch.codechallenge.model
2+
3+
import com.squareup.moshi.Json
4+
5+
data class GenreResponse(val genres: List<Genre>)
6+
7+
data class Genre(val id: Int, val name: String)
8+
9+
data class UpcomingMoviesResponse(
10+
val page: Int,
11+
val results: List<Movie>,
12+
@Json(name = "total_pages") val totalPages: Int,
13+
@Json(name = "total_results") val totalResults: Int
14+
)
15+
16+
data class Movie(
17+
val id: Int,
18+
val title: String,
19+
val overview: String?,
20+
val genres: List<Genre>?,
21+
@Json(name = "genre_ids") val genreIds: List<Int>?,
22+
@Json(name = "poster_path") val posterPath: String?,
23+
@Json(name = "backdrop_path") val backdropPath: String?,
24+
@Json(name = "release_date") val releaseDate: String?
25+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.arctouch.codechallenge.splash
2+
3+
import android.content.Intent
4+
import android.os.Bundle
5+
import com.arctouch.codechallenge.R
6+
import com.arctouch.codechallenge.api.TmdbApi
7+
import com.arctouch.codechallenge.base.BaseActivity
8+
import com.arctouch.codechallenge.data.Cache
9+
import com.arctouch.codechallenge.home.HomeActivity
10+
import io.reactivex.android.schedulers.AndroidSchedulers
11+
import io.reactivex.schedulers.Schedulers
12+
13+
class SplashActivity : BaseActivity() {
14+
15+
override fun onCreate(savedInstanceState: Bundle?) {
16+
super.onCreate(savedInstanceState)
17+
setContentView(R.layout.splash_activity)
18+
19+
api.genres(TmdbApi.API_KEY, TmdbApi.DEFAULT_LANGUAGE)
20+
.subscribeOn(Schedulers.io())
21+
.observeOn(AndroidSchedulers.mainThread())
22+
.subscribe {
23+
Cache.cacheGenres(it.genres)
24+
startActivity(Intent(this, HomeActivity::class.java))
25+
finish()
26+
}
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.arctouch.codechallenge.util
2+
3+
import com.arctouch.codechallenge.api.TmdbApi
4+
5+
private val POSTER_URL = "https://image.tmdb.org/t/p/w154"
6+
private val BACKDROP_URL = "https://image.tmdb.org/t/p/w780"
7+
8+
class MovieImageUrlBuilder {
9+
10+
fun buildPosterUrl(posterPath: String): String {
11+
return POSTER_URL + posterPath + "?api_key=" + TmdbApi.API_KEY
12+
}
13+
14+
fun buildBackdropUrl(backdropPath: String): String {
15+
return BACKDROP_URL + backdropPath + "?api_key=" + TmdbApi.API_KEY
16+
}
17+
}

0 commit comments

Comments
 (0)