Skip to content

Commit

Permalink
added dagger hilt with module
Browse files Browse the repository at this point in the history
  • Loading branch information
binayshaw7777 committed Jan 3, 2025
1 parent 1dfa049 commit 2f8a6d6
Show file tree
Hide file tree
Showing 19 changed files with 234 additions and 97 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ plugins {
id ("com.google.firebase.crashlytics")
id ("kotlin-kapt")
id("com.google.dagger.hilt.android")
// id("dagger.hilt.android.plugin")
id ("com.google.firebase.firebase-perf")
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
android:name=".JusTap"
android:allowBackup="false"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="false"
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/com/binay/shaw/justap/JusTap.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.binay.shaw.justap

import android.app.Application
import com.binay.shaw.justap.utils.Logger
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class JusTap: Application() {
override fun onCreate() {
super.onCreate()
Logger.debugLog("JusTap Application Created")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ class ViewModelFactory : ViewModelProvider.Factory {
isAssignableFrom(EditProfileViewModel::class.java) -> {
EditProfileViewModel()
}
isAssignableFrom(AccountsViewModel::class.java) -> {
AccountsViewModel(application)
}
isAssignableFrom(LocalUserViewModel::class.java) -> {
LocalUserViewModel(application)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
* Created by binay on 02,January,2023
*/

@Database(entities = [LocalUser::class, Accounts::class, LocalHistory::class], version = 4)
@TypeConverters(Converters::class)
abstract class LocalUserDatabase : RoomDatabase() {
Expand All @@ -32,24 +28,17 @@ abstract class LocalUserDatabase : RoomDatabase() {

companion object {
@Volatile
var INSTANCE: LocalUserDatabase? = null
private var INSTANCE: LocalUserDatabase? = null

@Synchronized
fun getDatabase(context: Context): LocalUserDatabase {

val tempInstance = INSTANCE

if (tempInstance != null) {
return tempInstance
}
synchronized(this) {
return INSTANCE ?: synchronized(this) {
val instance = Room.databaseBuilder(
context.applicationContext,
LocalUserDatabase::class.java,
"account_database"
).build()
INSTANCE = instance
return instance
instance
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
package com.binay.shaw.justap.data.local.db

import androidx.lifecycle.LiveData
import androidx.room.*
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import com.binay.shaw.justap.domain.model.Accounts
import kotlinx.coroutines.flow.Flow


@Dao
interface AccountsDAO {

@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insertAccount(accounts: Accounts)
suspend fun insertAccount(accounts: Accounts): Long

@Delete
suspend fun deleteAccount(accounts: Accounts)
suspend fun deleteAccount(accounts: Accounts): Int

@Update
suspend fun updateAccount(accounts: Accounts)
suspend fun updateAccount(accounts: Accounts): Int

@Query(value = "SELECT * FROM accountsDB")
fun getAccountsList() : LiveData<List<Accounts>>
fun getAccountsList(): Flow<List<Accounts>>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.binay.shaw.justap.data.local.repository

import com.binay.shaw.justap.data.local.db.AccountsDAO
import com.binay.shaw.justap.domain.model.Accounts
import com.binay.shaw.justap.domain.repository.AccountRepository
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

class AccountRepositoryImpl @Inject constructor(
private val accountsDao: AccountsDAO
) : AccountRepository {

override suspend fun getAccounts(): Flow<List<Accounts>> {
return accountsDao.getAccountsList()
}

override suspend fun insertAccount(accounts: Accounts): Long {
return accountsDao.insertAccount(accounts)
}

override suspend fun deleteAccount(accounts: Accounts): Int {
return accountsDao.deleteAccount(accounts)
}

override suspend fun updateAccount(accounts: Accounts): Int {
return accountsDao.updateAccount(accounts)
}
}
26 changes: 26 additions & 0 deletions app/src/main/java/com/binay/shaw/justap/di/modules/AppModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.binay.shaw.justap.di.modules

import android.app.Application
import android.content.Context
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

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

@Provides
@Singleton
fun provideAppContext(app: Application): Context {
return app.applicationContext
}

// @Provides
// @Singleton
// fun provideSharedPreferences(app: Application): SharedPreferences {
// return app.getSharedPreferences("Settings", Context.MODE_PRIVATE)
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.binay.shaw.justap.di.modules

import android.content.Context
import androidx.room.Room
import com.binay.shaw.justap.data.local.database.LocalUserDatabase
import com.binay.shaw.justap.data.local.db.AccountsDAO
import com.binay.shaw.justap.data.local.db.LocalHistoryDAO
import com.binay.shaw.justap.data.local.db.LocalUserDAO
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

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

// Provides the LocalUserDatabase instance
@Provides
@Singleton
fun provideLocalUserDatabase(context: Context): LocalUserDatabase {
return Room.databaseBuilder(
context.applicationContext,
LocalUserDatabase::class.java,
"account_database"
).fallbackToDestructiveMigration()
.build()
}

// Provides LocalUserDAO from the database
@Provides
fun provideLocalUserDao(database: LocalUserDatabase): LocalUserDAO {
return database.localUserDao()
}

// Provides AccountsDAO from the database
@Provides
fun provideAccountsDao(database: LocalUserDatabase): AccountsDAO {
return database.accountsDao()
}

// Provides LocalHistoryDAO from the database
@Provides
fun provideLocalHistoryDao(database: LocalUserDatabase): LocalHistoryDAO {
return database.localUserHistoryDao()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.binay.shaw.justap.di.modules

import com.binay.shaw.justap.data.local.db.AccountsDAO
import com.binay.shaw.justap.data.local.repository.AccountRepositoryImpl
import com.binay.shaw.justap.domain.repository.AccountRepository
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent

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

@Provides
fun provideAccountRepository(accountsDAO: AccountsDAO): AccountRepository {
return AccountRepositoryImpl(accountsDAO)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.binay.shaw.justap.domain.repository

import com.binay.shaw.justap.domain.model.Accounts
import kotlinx.coroutines.flow.Flow

interface AccountRepository {

suspend fun getAccounts(): Flow<List<Accounts>>

suspend fun insertAccount(accounts: Accounts): Long

suspend fun deleteAccount(accounts: Accounts): Int

suspend fun updateAccount(accounts: Accounts): Int
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import com.binay.shaw.justap.viewModel.LocalUserViewModel
class SignInScreen : BaseActivity() {

private lateinit var binding: ActivitySignInScreenBinding
private val accountsViewModel by viewModels<AccountsViewModel> { ViewModelFactory() }
private val accountsViewModel: AccountsViewModel by viewModels()
private val localUserViewModel by viewModels<LocalUserViewModel> { ViewModelFactory() }
private val firebaseViewModel by viewModels<FirebaseViewModel> { ViewModelFactory() }
private var isPasswordVisible = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import com.binay.shaw.justap.R
import com.binay.shaw.justap.base.BaseActivity
import com.binay.shaw.justap.databinding.ActivityMainBinding
import com.google.android.material.bottomnavigation.BottomNavigationView
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
class MainActivity : BaseActivity() {

private var timer = 0L
Expand Down
Loading

0 comments on commit 2f8a6d6

Please sign in to comment.