Skip to content

Commit

Permalink
- Created repo list page to fetch the trending git repos
Browse files Browse the repository at this point in the history
- Created RepoListViewModel and RepoRepository to process the API data
- Using live data and data binding to display data
  • Loading branch information
ankitb committed Sep 10, 2018
1 parent 84de1d1 commit 58c2158
Show file tree
Hide file tree
Showing 32 changed files with 953 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
61 changes: 61 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'androidx.navigation.safeargs'
apply plugin: 'kotlin-kapt'

android {
compileSdkVersion 28
defaultConfig {
applicationId "com.ankit.trendinggit"
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
dataBinding {
enabled = true
}
}

androidExtensions {
experimental = true
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
implementation 'androidx.constraintlayout:constraintlayout:1.1.2'

// Navigation component
implementation "android.arch.navigation:navigation-fragment:$rootProject.nav_version" // use -ktx for Kotlin
implementation "android.arch.navigation:navigation-ui:$rootProject.nav_version" // use -ktx for Kotlin
implementation "android.arch.navigation:navigation-runtime-ktx:$rootProject.nav_version" // use -ktx for Kotlin
implementation "android.arch.work:work-runtime-ktx:$rootProject.workVersion" // use -ktx for Kotlin

// Anko
implementation "org.jetbrains.anko:anko:$rootProject.anko_version"
implementation "org.jetbrains.anko:anko-commons:$rootProject.anko_version"

// Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.1'

// Databinding compiler
kapt 'com.android.databinding:compiler:3.2.0-alpha10'
}

kotlin {
experimental {
coroutines "enable"
}
}
21 changes: 21 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
26 changes: 26 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.ankit.trendinggit">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="false"
android:icon="@drawable/git_icon"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity
android:name="com.ankit.trendinggit.view.ui.MainActivity"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
15 changes: 15 additions & 0 deletions app/src/main/java/com/ankit/trendinggit/TrendingGitApp.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.ankit.trendinggit

import android.app.Application

class TrendingGitApp : Application() {

override fun onCreate() {
super.onCreate()
instance = this
}

companion object {
lateinit var instance: TrendingGitApp
}
}
112 changes: 112 additions & 0 deletions app/src/main/java/com/ankit/trendinggit/model/ApiResponse.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package com.ankit.trendinggit.model

data class GitResponse(
val total_count: Int,
val incomplete_results: Boolean,
val items: List<Item>
)

data class Item(
val id: Int,
val node_id: String,
val name: String,
val full_name: String,
val owner: Owner,
val private: Boolean,
val html_url: String,
val description: String,
val fork: Boolean,
val url: String,
val forks_url: String,
val keys_url: String,
val collaborators_url: String,
val teams_url: String,
val hooks_url: String,
val issue_events_url: String,
val events_url: String,
val assignees_url: String,
val branches_url: String,
val tags_url: String,
val blobs_url: String,
val git_tags_url: String,
val git_refs_url: String,
val trees_url: String,
val statuses_url: String,
val languages_url: String,
val stargazers_url: String,
val contributors_url: String,
val subscribers_url: String,
val subscription_url: String,
val commits_url: String,
val git_commits_url: String,
val comments_url: String,
val issue_comment_url: String,
val contents_url: String,
val compare_url: String,
val merges_url: String,
val archive_url: String,
val downloads_url: String,
val issues_url: String,
val pulls_url: String,
val milestones_url: String,
val notifications_url: String,
val labels_url: String,
val releases_url: String,
val deployments_url: String,
val created_at: String,
val updated_at: String,
val pushed_at: String,
val git_url: String,
val ssh_url: String,
val clone_url: String,
val svn_url: String,
val homepage: String,
val size: Int,
val stargazers_count: Int,
val watchers_count: Int,
val language: String,
val has_issues: Boolean,
val has_projects: Boolean,
val has_downloads: Boolean,
val has_wiki: Boolean,
val has_pages: Boolean,
val forks_count: Int,
val mirror_url: Any,
val archived: Boolean,
val open_issues_count: Int,
val license: License,
val forks: Int,
val open_issues: Int,
val watchers: Int,
val default_branch: String,
val score: Double
)

data class Owner(
val login: String,
val id: Int,
val node_id: String,
val avatar_url: String,
val gravatar_id: String,
val url: String,
val html_url: String,
val followers_url: String,
val following_url: String,
val gists_url: String,
val starred_url: String,
val subscriptions_url: String,
val organizations_url: String,
val repos_url: String,
val events_url: String,
val received_events_url: String,
val type: String,
val site_admin: Boolean
)

data class License(
val key: String,
val name: String,
val spdx_id: String,
val url: String,
val node_id: String
)
54 changes: 54 additions & 0 deletions app/src/main/java/com/ankit/trendinggit/model/api/ApiClient.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.ankit.trendinggit.model.api

import com.ankit.trendinggit.view.utils.Constants.Companion.BASE_URL
import com.ankit.trendinggit.view.utils.Constants.Companion.DEBUG
import com.ankit.trendinggit.view.utils.Constants.Companion.REQUEST_TIMEOUT_DURATION
import com.google.gson.GsonBuilder
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

object ApiClient {

val instance: ApiService = Retrofit.Builder().run {
val gson = GsonBuilder()
.enableComplexMapKeySerialization()
.setPrettyPrinting()
.create()

baseUrl(BASE_URL)
addConverterFactory(GsonConverterFactory.create(gson))
client(createRequestInterceptorClient())
build()
}.create(ApiService::class.java)


private fun createRequestInterceptorClient(): OkHttpClient {
val interceptor = Interceptor { chain ->
val original = chain.request()
val requestBuilder = original.newBuilder()
val request = requestBuilder.build()
chain.proceed(request)
}

return if (DEBUG) {
OkHttpClient.Builder()
.addInterceptor(interceptor)
.addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
.connectTimeout(REQUEST_TIMEOUT_DURATION.toLong(), TimeUnit.SECONDS)
.readTimeout(REQUEST_TIMEOUT_DURATION.toLong(), TimeUnit.SECONDS)
.writeTimeout(REQUEST_TIMEOUT_DURATION.toLong(), TimeUnit.SECONDS)
.build()
} else {
OkHttpClient.Builder()
.addInterceptor(interceptor)
.connectTimeout(REQUEST_TIMEOUT_DURATION.toLong(), TimeUnit.SECONDS)
.readTimeout(REQUEST_TIMEOUT_DURATION.toLong(), TimeUnit.SECONDS)
.writeTimeout(REQUEST_TIMEOUT_DURATION.toLong(), TimeUnit.SECONDS)
.build()
}
}
}
12 changes: 12 additions & 0 deletions app/src/main/java/com/ankit/trendinggit/model/api/ApiService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.ankit.trendinggit.model.api

import com.ankit.trendinggit.model.GitResponse
import retrofit2.Call
import retrofit2.http.GET
import retrofit2.http.Query

interface ApiService {

@GET("search/repositories")
fun getRepo(@Query("q") search: String = "trending"): Call<GitResponse>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.ankit.trendinggit.model.api

import com.ankit.trendinggit.model.GitResponse
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class RepoRepository {

// GET repo list
fun getRepoList(onResult: (isSuccess: Boolean, response: GitResponse?) -> Unit) {

ApiClient.instance.getRepo().enqueue(object :Callback<GitResponse> {
override fun onResponse(call: Call<GitResponse>?, response: Response<GitResponse>?) {
if (response != null && response.isSuccessful)
onResult(true, response.body()!!)
else
onResult(false, null)
}

override fun onFailure(call: Call<GitResponse>?, t: Throwable?) {
onResult(false, null)
}

})
}

companion object {
private var INSTANCE: RepoRepository? = null
fun getInstance() = INSTANCE ?: RepoRepository().also {
INSTANCE = it
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.ankit.trendinggit.view.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.ankit.trendinggit.databinding.ViewRepoListItemBinding
import com.ankit.trendinggit.model.Item
import com.ankit.trendinggit.view.adapter.viewHolders.RepoListViewHolder
import com.ankit.trendinggit.view.ui.repolist.RepoListViewModel

class RepoListAdapter(private val repoListViewModel: RepoListViewModel) : RecyclerView.Adapter<RepoListViewHolder>() {
var repoList: List<Item> = emptyList()

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RepoListViewHolder {
val inflater = LayoutInflater.from(parent.context)
val dataBinding = ViewRepoListItemBinding.inflate(inflater, parent, false)
return RepoListViewHolder(dataBinding, repoListViewModel)
}

override fun getItemCount() = repoList.size

override fun onBindViewHolder(holder: RepoListViewHolder, position: Int) {
holder.setup(repoList[position])
}

fun updateRepoList(repoList: List<Item>) {
this.repoList = repoList
notifyDataSetChanged()
}
}
Loading

0 comments on commit 58c2158

Please sign in to comment.