Skip to content

Commit

Permalink
Merge pull request #15 from YuanLiou/feature/hilt
Browse files Browse the repository at this point in the history
Feature/hilt
  • Loading branch information
YuanLiou authored Aug 15, 2021
2 parents 4b7e1d1 + 3476472 commit 9203d19
Show file tree
Hide file tree
Showing 45 changed files with 529 additions and 416 deletions.
10 changes: 8 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ apply plugin: 'kotlin-android'
apply plugin: 'kotlin-parcelize'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlinx-serialization'
apply plugin: 'dagger.hilt.android.plugin'
apply from: "${rootDir}/gradle/build-script.gradle"

android {
Expand All @@ -22,6 +23,11 @@ android {
buildConfigField 'String', 'HOST', APIHost
}

buildFeatures {
viewBinding true
dataBinding true
}

compileOptions {
coreLibraryDesugaringEnabled true
sourceCompatibility JavaVersion.VERSION_11
Expand Down Expand Up @@ -132,8 +138,8 @@ dependencies {
implementation 'com.squareup.okhttp3:logging-interceptor:3.5.0'

// Dependency Injection
implementation 'com.google.dagger:dagger:2.38.1'
kapt 'com.google.dagger:dagger-compiler:2.38.1'
implementation "com.google.dagger:hilt-android:$dagger_version"
kapt "com.google.dagger:hilt-compiler:$dagger_version"

// ViewPager Indicator
// FIXME:: Fetch on JitPack.io, version 1.0.3
Expand Down
2 changes: 1 addition & 1 deletion app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

-keep class tw.com.louis383.coffeefinder.model.entity.** { *; }
-keep class tw.com.louis383.coffeefinder.model.domain.** { *; }
-keep interface tw.com.louis383.coffeefinder.model.CoffeeTripService { *; }
-keep interface tw.com.louis383.coffeefinder.model.data.api.CoffeeTripService { *; }

# Uncomment this to preserve the line number information for
# debugging stack traces.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package tw.com.louis383.coffeefinder

import androidx.fragment.app.Fragment
import tw.com.louis383.coffeefinder.model.entity.Shop
import tw.com.louis383.coffeefinder.model.domain.model.CoffeeShop

/**
* Created by louis383 on 2017/2/26.
*/
abstract class BaseFragment : Fragment() {
abstract fun prepareCoffeeShops(coffeeShops: List<Shop>)
abstract fun prepareCoffeeShops(coffeeShops: List<CoffeeShop>)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@ package tw.com.louis383.coffeefinder

import android.app.Application
import androidx.appcompat.app.AppCompatDelegate
import tw.com.louis383.coffeefinder.di.components.AppComponent
import tw.com.louis383.coffeefinder.di.components.DaggerAppComponent
import tw.com.louis383.coffeefinder.di.module.AppModule
import dagger.hilt.android.HiltAndroidApp

/**
* Created by louis383 on 2017/1/24.
*/
@HiltAndroidApp
class CoffeeTripApplication : Application() {
var appComponent: AppComponent? = null
private set

override fun onCreate() {
super.onCreate()
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
appComponent = DaggerAppComponent.builder().appModule(AppModule(this)).build()
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
package tw.com.louis383.coffeefinder.details

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import android.widget.Button
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.RatingBar
import android.widget.TextView
import androidx.core.widget.NestedScrollView
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import com.google.android.libraries.maps.model.LatLng
import dagger.hilt.android.AndroidEntryPoint
import tw.com.louis383.coffeefinder.R
import tw.com.louis383.coffeefinder.mainpage.MainActivity
import tw.com.louis383.coffeefinder.model.CurrentLocationCarrier
import tw.com.louis383.coffeefinder.model.entity.Shop
import tw.com.louis383.coffeefinder.model.domain.model.CoffeeShop
import tw.com.louis383.coffeefinder.uimodel.CoffeeShopUiModel
import tw.com.louis383.coffeefinder.utils.FragmentArgumentDelegate
import tw.com.louis383.coffeefinder.viewmodel.CoffeeShopViewModel
import javax.inject.Inject

@AndroidEntryPoint
class DetailsFragment: Fragment() {
companion object {
fun newInstance(coffeeShop: Shop) = DetailsFragment().apply {
fun newInstance(coffeeShop: CoffeeShop) = DetailsFragment().apply {
this.coffeeShop = coffeeShop
}
}

private var coffeeShop by FragmentArgumentDelegate<Shop>()
private var coffeeShop by FragmentArgumentDelegate<CoffeeShop>()
var detailsItemClickListener: DetailsItemClickListener? = null
private lateinit var nestedScrollView: NestedScrollView
private lateinit var titleText: TextView
Expand All @@ -49,11 +53,6 @@ class DetailsFragment: Fragment() {
@Inject
lateinit var currentLocationCarrier: CurrentLocationCarrier

override fun onAttach(context: Context) {
super.onAttach(context)
(context as? MainActivity)?.getAppComponent()?.inject(this)
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_detail, container, false)
}
Expand All @@ -62,7 +61,7 @@ class DetailsFragment: Fragment() {
super.onViewCreated(view, savedInstanceState)
retrieveViews(view)
nestedScrollView = view.findViewById(R.id.detail_view_scrollview)
setDetailInfo(coffeeShop.getViewModel())
setDetailInfo(coffeeShop.getUiModel())

view.findViewById<Button>(R.id.detail_view_button_navigate).setOnClickListener {
detailsItemClickListener?.onNavigationButtonClicked()
Expand Down Expand Up @@ -99,8 +98,8 @@ class DetailsFragment: Fragment() {
distanceText = view.findViewById<TextView>(R.id.detail_view_distance)
}

fun setDetailInfo(viewModel: CoffeeShopViewModel) {
with(viewModel) {
fun setDetailInfo(uiModel: CoffeeShopUiModel) {
with(uiModel) {
titleText.text = shopName
cheapRating.rating = cheapPoints

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,56 +1,25 @@
package tw.com.louis383.coffeefinder.di.module

import android.app.Application
import android.content.Context
import android.content.SharedPreferences
import dagger.Module
import dagger.Provides
import tw.com.louis383.coffeefinder.di.ApplicationContext
import tw.com.louis383.coffeefinder.model.*
import javax.inject.Singleton
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent

/**
* Created by louis383 on 2017/2/22.
*/
@InstallIn(SingletonComponent::class)
@Module
class AppModule(private val application: Application) {
object AppModule {

@ApplicationContext
@Provides
fun provideApplicationContext(): Context {
return application
}

@Provides
@Singleton
fun provideCoffeeAPI(): CoffeeTripAPI {
return CoffeeTripAPI()
}

@Provides
@Singleton
fun provideCoffeeShopListManager(coffeeTripAPI: CoffeeTripAPI): CoffeeShopListManager {
return CoffeeShopListManager(coffeeTripAPI)
}

@Provides
@Singleton
fun providePreferenceManager(
@ApplicationContext context: Context
): PreferenceManager {
return PreferenceManager(context)
}

@Provides
fun provideConnectivityChecker(
@ApplicationContext context: Context
): ConnectivityChecker {
return ConnectivityChecker(context)
}

@Provides
fun providePermissionChecker(
fun provideDefaultPreference(
@ApplicationContext context: Context
): PermissionChecker {
return PermissionChecker(context)
): SharedPreferences {
val preferenceName = "coffeeTrip_preference"
return context.getSharedPreferences(preferenceName, Context.MODE_PRIVATE)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import com.google.android.gms.location.SettingsClient
import dagger.Module
import dagger.Provides
import dagger.Reusable
import tw.com.louis383.coffeefinder.di.ApplicationContext
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import tw.com.louis383.coffeefinder.model.CurrentLocationCarrier
import tw.com.louis383.coffeefinder.model.PermissionChecker
import tw.com.louis383.coffeefinder.model.UserLocationListener
import javax.inject.Singleton

@InstallIn(SingletonComponent::class)
@Module
class LocationModule {
object LocationModule {

@Singleton
@Provides
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package tw.com.louis383.coffeefinder.di.module

import android.content.Context
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import dagger.Lazy
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
import okhttp3.MediaType
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import tw.com.louis383.coffeefinder.BuildConfig
import tw.com.louis383.coffeefinder.model.ConnectivityChecker
import tw.com.louis383.coffeefinder.model.data.api.CoffeeTripService
import java.util.concurrent.TimeUnit
import javax.inject.Qualifier


@Retention(AnnotationRetention.BINARY)
@Qualifier
private annotation class InternalNetworkApi

@InstallIn(SingletonComponent::class)
@Module
object NetworkModule {

@Provides
fun provideConnectivityChecker(
@ApplicationContext context: Context
): ConnectivityChecker {
return ConnectivityChecker(context)
}

@Provides
@InternalNetworkApi
fun provideLogInterceptor(): HttpLoggingInterceptor {
return HttpLoggingInterceptor().also {
if (BuildConfig.DEBUG) {
it.level = HttpLoggingInterceptor.Level.HEADERS
it.level = HttpLoggingInterceptor.Level.BODY
} else {
it.level = HttpLoggingInterceptor.Level.NONE
}
}
}

@Provides
@InternalNetworkApi
fun provideHttpClient(
@InternalNetworkApi logInterceptor: HttpLoggingInterceptor
): OkHttpClient {
return OkHttpClient.Builder()
.addInterceptor(logInterceptor)
.connectTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.retryOnConnectionFailure(true)
.build()
}

@OptIn(ExperimentalSerializationApi::class)
@Provides
@InternalNetworkApi
fun provideRetrofit(
@InternalNetworkApi httpClient: Lazy<OkHttpClient>
): Retrofit {
val HOST = BuildConfig.HOST
val contentType = MediaType.parse("application/json")!!
return Retrofit.Builder()
.baseUrl(HOST)
.addConverterFactory(Json.asConverterFactory(contentType))
.callFactory {
httpClient.get().newCall(it)
}
.build()
}

@Provides
fun provideCoffeeTripApi(
@InternalNetworkApi retrofit: Retrofit
): CoffeeTripService {
return retrofit.create(CoffeeTripService::class.java)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package tw.com.louis383.coffeefinder.di.module

import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import tw.com.louis383.coffeefinder.model.domain.repository.CoffeeShopRepository
import tw.com.louis383.coffeefinder.model.domain.repository.CoffeeShopRepositoryImpl

@InstallIn(SingletonComponent::class)
@Module
abstract class RepositoryModule {

@Binds
abstract fun bindCoffeeShopRepository(
coffeeShopRepositoryImpl: CoffeeShopRepositoryImpl
): CoffeeShopRepository
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package tw.com.louis383.coffeefinder.list

import tw.com.louis383.coffeefinder.BaseView
import tw.com.louis383.coffeefinder.model.entity.Shop
import tw.com.louis383.coffeefinder.model.domain.model.CoffeeShop

interface CoffeeShopListView : BaseView {
fun setLoadingProgressBarVisibility(visible: Boolean)
fun setRecyclerViewVisibility(visible: Boolean)
fun setNoCoffeeShopPictureVisibility(visible: Boolean)
fun showNoCoffeeShopMessage()
fun setItems(items: List<Shop>)
fun setItems(items: List<CoffeeShop>)
}
Loading

0 comments on commit 9203d19

Please sign in to comment.