Skip to content

Commit c888697

Browse files
committed
Project Init
0 parents  commit c888697

Some content is hidden

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

48 files changed

+1176
-0
lines changed

.gitignore

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild
14+
.cxx
15+
/.idea

README.md

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Dagger Hilt Tutorial - Step by Step Guide
2+
[![MindOrks](https://img.shields.io/badge/mindorks-opensource-blue.svg)](https://mindorks.com/open-source-projects)
3+
[![MindOrks Community](https://img.shields.io/badge/join-community-blue.svg)](https://mindorks.com/join-community)
4+
5+
This repository contains a sample app that implements Dagger-Hilt in MVVM architecture using Kotlin.
6+
<p align="center">
7+
<img src="https://raw.githubusercontent.com/MindorksOpenSource/MVVM-Architecture-Android-Beginners/master/assets/banner-mvvm-arch-beginners.jpg">
8+
</p>
9+
10+
11+
12+
#### The app has following packages:
13+
1. **data**: It contains all the data accessing and manipulating components.
14+
2. **di**: It contains the files required by Dagger
15+
3. **ui**: View classes along with their corresponding ViewModel.
16+
4. **utils**: Utility classes.
17+
18+
### Library reference resources:
19+
1. Kotlin-Coroutines: https://github.com/MindorksOpenSource/Kotlin-Coroutines-Android-Examples
20+
21+
### Looking for Kotlin MVP Architecture - [Check here](https://github.com/MindorksOpenSource/android-kotlin-mvp-architecture)
22+
23+
### Looking for MVVM Architecture for begineers - [Check here](https://github.com/MindorksOpenSource/MVVM-Architecture-Android-Beginners)
24+
25+
### Looking for MVP Architecture - [Check here](https://github.com/MindorksOpenSource/android-mvp-architecture)
26+
27+
### Learn to build a ride-sharing Android app like Uber, Lyft - [Check here](https://github.com/MindorksOpenSource/ridesharing-uber-lyft-app)
28+
29+
[Check out MindOrks awesome open source projects here](https://mindorks.com/open-source-projects)
30+
31+
### License
32+
```
33+
Copyright (C) 2020 MINDORKS NEXTGEN PRIVATE LIMITED
34+
35+
Licensed under the Apache License, Version 2.0 (the "License");
36+
you may not use this file except in compliance with the License.
37+
You may obtain a copy of the License at
38+
39+
http://www.apache.org/licenses/LICENSE-2.0
40+
41+
Unless required by applicable law or agreed to in writing, software
42+
distributed under the License is distributed on an "AS IS" BASIS,
43+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
44+
See the License for the specific language governing permissions and
45+
limitations under the License.
46+
```
47+
48+
### Contributing to Android MVVM Architecture
49+
Just make pull request. You are in!

app/.gitignore

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

app/build.gradle

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
apply plugin: 'com.android.application'
2+
apply plugin: 'kotlin-android'
3+
apply plugin: 'kotlin-android-extensions'
4+
apply plugin: 'kotlin-kapt'
5+
apply plugin: 'dagger.hilt.android.plugin'
6+
7+
android {
8+
compileSdkVersion 29
9+
buildToolsVersion "29.0.3"
10+
11+
defaultConfig {
12+
applicationId "com.mindorks.framework.mvvm"
13+
minSdkVersion 16
14+
targetSdkVersion 29
15+
versionCode 1
16+
versionName "1.0"
17+
18+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
19+
20+
buildConfigField 'String', 'BASE_URL', "\"https://5e510330f2c0d300147c034c.mockapi.io/\""
21+
22+
}
23+
24+
buildTypes {
25+
release {
26+
minifyEnabled false
27+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
28+
}
29+
}
30+
compileOptions {
31+
sourceCompatibility JavaVersion.VERSION_1_8
32+
targetCompatibility JavaVersion.VERSION_1_8
33+
}
34+
// work-runtime-ktx 2.1.0 and above now requires Java 8
35+
kotlinOptions {
36+
jvmTarget = "1.8"
37+
}
38+
39+
}
40+
41+
dependencies {
42+
implementation fileTree(dir: 'libs', include: ['*.jar'])
43+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
44+
implementation 'androidx.appcompat:appcompat:1.1.0'
45+
implementation 'androidx.core:core-ktx:1.3.0'
46+
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
47+
testImplementation 'junit:junit:4.13'
48+
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
49+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
50+
51+
// Added Dependencies
52+
implementation "androidx.recyclerview:recyclerview:1.1.0"
53+
implementation 'android.arch.lifecycle:extensions:1.1.1'
54+
implementation 'com.github.bumptech.glide:glide:4.11.0'
55+
implementation 'androidx.activity:activity-ktx:1.1.0'
56+
57+
58+
//Dagger
59+
implementation 'com.google.dagger:hilt-android:2.28-alpha'
60+
kapt 'com.google.dagger:hilt-android-compiler:2.28-alpha'
61+
implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha01'
62+
kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha01'
63+
64+
65+
// Networking
66+
implementation "com.squareup.retrofit2:converter-moshi:2.6.2"
67+
implementation "com.squareup.retrofit2:retrofit:2.8.1"
68+
implementation "com.squareup.okhttp3:okhttp:4.7.2"
69+
implementation "com.squareup.okhttp3:logging-interceptor:4.7.2"
70+
71+
//Coroutine
72+
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.6"
73+
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.6"
74+
75+
}

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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.mindorks.framework.mvvm
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import androidx.test.ext.junit.runners.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.getInstrumentation().targetContext
22+
assertEquals("com.mindorks.framework.mvvm", appContext.packageName)
23+
}
24+
}

app/src/main/AndroidManifest.xml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.mindorks.framework.mvvm">
4+
5+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
6+
<uses-permission android:name="android.permission.INTERNET" />
7+
8+
<application
9+
android:allowBackup="true"
10+
android:icon="@mipmap/ic_launcher"
11+
android:name=".App"
12+
android:label="@string/app_name"
13+
android:roundIcon="@mipmap/ic_launcher_round"
14+
android:supportsRtl="true"
15+
android:theme="@style/AppTheme">
16+
<activity android:name=".ui.main.view.MainActivity">
17+
<intent-filter>
18+
<action android:name="android.intent.action.MAIN" />
19+
20+
<category android:name="android.intent.category.LAUNCHER" />
21+
</intent-filter>
22+
</activity>
23+
</application>
24+
25+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.mindorks.framework.mvvm
2+
3+
import android.app.Application
4+
import dagger.hilt.android.HiltAndroidApp
5+
6+
@HiltAndroidApp
7+
class App : Application()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.mindorks.framework.mvvm.data.api
2+
3+
import com.mindorks.framework.mvvm.data.model.User
4+
import retrofit2.Response
5+
6+
interface ApiHelper {
7+
8+
suspend fun getUsers(): Response<List<User>>
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.mindorks.framework.mvvm.data.api
2+
3+
import com.mindorks.framework.mvvm.data.model.User
4+
import retrofit2.Response
5+
import javax.inject.Inject
6+
7+
class ApiHelperImpl @Inject constructor(private val apiService: ApiService) : ApiHelper {
8+
9+
override suspend fun getUsers(): Response<List<User>> = apiService.getUsers()
10+
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.mindorks.framework.mvvm.data.api
2+
3+
import com.mindorks.framework.mvvm.data.model.User
4+
import retrofit2.Response
5+
import retrofit2.http.GET
6+
7+
interface ApiService {
8+
9+
@GET("users")
10+
suspend fun getUsers(): Response<List<User>>
11+
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.mindorks.framework.mvvm.data.model
2+
3+
import com.squareup.moshi.Json
4+
5+
6+
data class User(
7+
@Json(name = "id")
8+
val id: Int = 0,
9+
@Json(name = "name")
10+
val name: String = "",
11+
@Json(name = "email")
12+
val email: String = "",
13+
@Json(name = "avatar")
14+
val avatar: String = ""
15+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.mindorks.framework.mvvm.data.repository
2+
3+
import com.mindorks.framework.mvvm.data.api.ApiHelper
4+
import javax.inject.Inject
5+
6+
class MainRepository @Inject constructor(private val apiHelper: ApiHelper) {
7+
8+
suspend fun getUsers() = apiHelper.getUsers()
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.mindorks.framework.mvvm.di.module
2+
3+
import com.mindorks.framework.mvvm.BuildConfig
4+
import com.mindorks.framework.mvvm.data.api.ApiHelper
5+
import com.mindorks.framework.mvvm.data.api.ApiHelperImpl
6+
import com.mindorks.framework.mvvm.data.api.ApiService
7+
import dagger.Module
8+
import dagger.Provides
9+
import okhttp3.OkHttpClient
10+
import okhttp3.logging.HttpLoggingInterceptor
11+
import retrofit2.Retrofit
12+
import javax.inject.Singleton
13+
import dagger.hilt.InstallIn
14+
import dagger.hilt.android.components.ApplicationComponent
15+
import retrofit2.converter.moshi.MoshiConverterFactory
16+
17+
@Module
18+
@InstallIn(ApplicationComponent::class)
19+
class ApplicationModule {
20+
21+
@Provides
22+
fun provideBaseUrl() = BuildConfig.BASE_URL
23+
24+
@Provides
25+
@Singleton
26+
fun provideOkHttpClient() = if (BuildConfig.DEBUG) {
27+
val loggingInterceptor = HttpLoggingInterceptor()
28+
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
29+
OkHttpClient.Builder()
30+
.addInterceptor(loggingInterceptor)
31+
.build()
32+
} else OkHttpClient
33+
.Builder()
34+
.build()
35+
36+
37+
@Provides
38+
@Singleton
39+
fun provideRetrofit(
40+
okHttpClient: OkHttpClient,
41+
BASE_URL: String
42+
): Retrofit =
43+
Retrofit.Builder()
44+
.addConverterFactory(MoshiConverterFactory.create())
45+
.baseUrl(BASE_URL)
46+
.client(okHttpClient)
47+
.build()
48+
49+
@Provides
50+
@Singleton
51+
fun provideApiService(retrofit: Retrofit): ApiService = retrofit.create(ApiService::class.java)
52+
53+
@Provides
54+
@Singleton
55+
fun provideApiHelper(apiHelper: ApiHelperImpl): ApiHelper = apiHelper
56+
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.mindorks.framework.mvvm.ui.main.adapter
2+
3+
import android.view.LayoutInflater
4+
import android.view.View
5+
import android.view.ViewGroup
6+
import androidx.recyclerview.widget.RecyclerView
7+
import com.bumptech.glide.Glide
8+
import com.mindorks.framework.mvvm.R
9+
import com.mindorks.framework.mvvm.data.model.User
10+
import kotlinx.android.synthetic.main.item_layout.view.*
11+
12+
class MainAdapter(
13+
private val users: ArrayList<User>
14+
) : RecyclerView.Adapter<MainAdapter.DataViewHolder>() {
15+
16+
class DataViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
17+
fun bind(user: User) {
18+
itemView.textViewUserName.text = user.name
19+
itemView.textViewUserEmail.text = user.email
20+
Glide.with(itemView.imageViewAvatar.context)
21+
.load(user.avatar)
22+
.into(itemView.imageViewAvatar)
23+
}
24+
}
25+
26+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
27+
DataViewHolder(
28+
LayoutInflater.from(parent.context).inflate(
29+
R.layout.item_layout, parent,
30+
false
31+
)
32+
)
33+
34+
override fun getItemCount(): Int = users.size
35+
36+
override fun onBindViewHolder(holder: DataViewHolder, position: Int) =
37+
holder.bind(users[position])
38+
39+
fun addData(list: List<User>) {
40+
users.addAll(list)
41+
}
42+
}

0 commit comments

Comments
 (0)