-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
153 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
...ion/dagger/components/LibraryComponent.kt → ...tion/dagger/component/LibraryComponent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...gger/builders/BookListViewModelFactory.kt → ...agger/factory/BookListViewModelFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...n/dagger/builders/BookViewModelFactory.kt → ...on/dagger/factory/BookViewModelFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
app/src/main/java/com/sample/libraryapplication/dagger/module/LibraryModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.sample.libraryapplication.dagger.module | ||
|
||
import com.sample.libraryapplication.database.LibraryDatabase | ||
import com.sample.libraryapplication.repository.BookRepository | ||
import com.sample.libraryapplication.repository.CategoryRepository | ||
import dagger.Module | ||
import dagger.Provides | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
class LibraryModule { | ||
|
||
@Singleton | ||
@Provides | ||
fun providesBookRepository(libraryDatabase: LibraryDatabase): BookRepository { | ||
return BookRepository(libraryDatabase) | ||
} | ||
|
||
@Singleton | ||
@Provides | ||
fun providesCategoryRepository(libraryDatabase: LibraryDatabase): CategoryRepository { | ||
return CategoryRepository(libraryDatabase) | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
app/src/main/java/com/sample/libraryapplication/dagger/module/RoomDatabaseModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package com.sample.libraryapplication.dagger.module | ||
|
||
import android.app.Application | ||
import android.util.Log | ||
import androidx.room.Room | ||
import androidx.room.RoomDatabase | ||
import androidx.sqlite.db.SupportSQLiteDatabase | ||
import com.sample.libraryapplication.database.LibraryDatabase | ||
import com.sample.libraryapplication.database.entity.BookEntity | ||
import com.sample.libraryapplication.database.entity.CategoryEntity | ||
import dagger.Module | ||
import dagger.Provides | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
class RoomDatabaseModule(application: Application) { | ||
|
||
private var libraryApplication = application | ||
private lateinit var libraryDatabase: LibraryDatabase | ||
|
||
companion object { | ||
private const val EDUCATIONAL_BOOKS_CATEGORY_ID = 1L | ||
private const val NOVELS_CATEGORY_ID = 2L | ||
private const val OTHER_BOOKS_CATEGORY_ID = 3L | ||
} | ||
|
||
private val databaseCallback = object : RoomDatabase.Callback() { | ||
override fun onCreate(db: SupportSQLiteDatabase) { | ||
super.onCreate(db) | ||
Log.d("RoomDatabaseModule", "onCreate") | ||
CoroutineScope(Dispatchers.IO).launch { | ||
addSampleBooksToDatabase() | ||
} | ||
} | ||
} | ||
|
||
private fun addSampleBooksToDatabase() { | ||
val category1 = CategoryEntity(EDUCATIONAL_BOOKS_CATEGORY_ID, "Educational Books", "Educational Books Desc") | ||
val category2 = CategoryEntity(NOVELS_CATEGORY_ID, "Novels", "Novels Desc") | ||
val category3 = CategoryEntity(OTHER_BOOKS_CATEGORY_ID, "Other Books", "Non Categorized Books") | ||
|
||
val book1 = BookEntity(1, "Java Programming Book", 10.50, EDUCATIONAL_BOOKS_CATEGORY_ID) | ||
val book2 = BookEntity(2, "Mathematics", 19.10, EDUCATIONAL_BOOKS_CATEGORY_ID) | ||
val book3 = BookEntity(3, "Adventures of Joe Finn", 25.30, NOVELS_CATEGORY_ID) | ||
val book4 = BookEntity(4, "The Hound the New York", 5.30, NOVELS_CATEGORY_ID) | ||
val book5 = BookEntity(5, "Astrology", 56.99, OTHER_BOOKS_CATEGORY_ID) | ||
val book6 = BookEntity(6, "Arc of Witches", 34.99, OTHER_BOOKS_CATEGORY_ID) | ||
val book7 = BookEntity(7, "Can I Run?", 99.99, NOVELS_CATEGORY_ID) | ||
val book8 = BookEntity(8, "Basic of Physics", 10.50, EDUCATIONAL_BOOKS_CATEGORY_ID) | ||
|
||
val categoryDAO = libraryDatabase.getCategoryDAO() | ||
categoryDAO.addCategory(category1) | ||
categoryDAO.addCategory(category2) | ||
categoryDAO.addCategory(category3) | ||
|
||
val bookDAO = libraryDatabase.getBookDAO() | ||
bookDAO.addBook(book1) | ||
bookDAO.addBook(book2) | ||
bookDAO.addBook(book3) | ||
bookDAO.addBook(book4) | ||
bookDAO.addBook(book5) | ||
bookDAO.addBook(book6) | ||
bookDAO.addBook(book7) | ||
bookDAO.addBook(book8) | ||
} | ||
|
||
@Singleton | ||
@Provides | ||
fun providesRoomDatabase(): LibraryDatabase { | ||
libraryDatabase = Room.databaseBuilder(libraryApplication, LibraryDatabase::class.java, "library_database") | ||
.fallbackToDestructiveMigration() | ||
.addCallback(databaseCallback) | ||
.build() | ||
return libraryDatabase | ||
} | ||
|
||
@Singleton | ||
@Provides | ||
fun providesCategoryDAO(libraryDatabase: LibraryDatabase) = libraryDatabase.getCategoryDAO() | ||
|
||
@Singleton | ||
@Provides | ||
fun providesBookDAO(libraryDatabase: LibraryDatabase) = libraryDatabase.getBookDAO() | ||
} |
18 changes: 0 additions & 18 deletions
18
app/src/main/java/com/sample/libraryapplication/dagger/modules/AppModule.kt
This file was deleted.
Oops, something went wrong.
24 changes: 0 additions & 24 deletions
24
app/src/main/java/com/sample/libraryapplication/dagger/modules/LibraryModule.kt
This file was deleted.
Oops, something went wrong.
67 changes: 0 additions & 67 deletions
67
app/src/main/java/com/sample/libraryapplication/database/LibraryDatabase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,15 @@ | ||
package com.sample.libraryapplication.database | ||
|
||
import android.app.Application | ||
import androidx.room.Database | ||
import androidx.room.Room | ||
import androidx.room.RoomDatabase | ||
import androidx.sqlite.db.SupportSQLiteDatabase | ||
import com.sample.libraryapplication.database.dao.BookDAO | ||
import com.sample.libraryapplication.database.dao.CategoryDAO | ||
import com.sample.libraryapplication.database.entity.BookEntity | ||
import com.sample.libraryapplication.database.entity.CategoryEntity | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
|
||
@Database(entities = [CategoryEntity::class, BookEntity::class], version = 1) | ||
abstract class LibraryDatabase : RoomDatabase() { | ||
|
||
abstract fun getCategoryDAO(): CategoryDAO | ||
abstract fun getBookDAO(): BookDAO | ||
|
||
companion object { | ||
|
||
private const val EDUCATIONAL_BOOKS_CATEGORY_ID = 1L | ||
private const val NOVELS_CATEGORY_ID = 2L | ||
private const val OTHER_BOOKS_CATEGORY_ID = 3L | ||
|
||
private val databaseCallback = object : RoomDatabase.Callback() { | ||
override fun onCreate(db: SupportSQLiteDatabase) { | ||
super.onCreate(db) | ||
CoroutineScope(Dispatchers.IO).launch { | ||
addSampleBooksToDatabase() | ||
} | ||
} | ||
} | ||
|
||
private fun addSampleBooksToDatabase() { | ||
val category1 = CategoryEntity(EDUCATIONAL_BOOKS_CATEGORY_ID, "Educational Books", "Educational Books Desc") | ||
val category2 = CategoryEntity(NOVELS_CATEGORY_ID, "Novels", "Novels Desc") | ||
val category3 = CategoryEntity(OTHER_BOOKS_CATEGORY_ID, "Other Books", "Non Categorized Books") | ||
|
||
val book1 = BookEntity(1, "Java Programming Book", 10.50, EDUCATIONAL_BOOKS_CATEGORY_ID) | ||
val book2 = BookEntity(2, "Mathematics", 19.10, EDUCATIONAL_BOOKS_CATEGORY_ID) | ||
val book3 = BookEntity(3, "Adventures of Joe Finn", 25.30, NOVELS_CATEGORY_ID) | ||
val book4 = BookEntity(4, "The Hound the New York", 5.30, NOVELS_CATEGORY_ID) | ||
val book5 = BookEntity(5, "Astrology", 56.99, OTHER_BOOKS_CATEGORY_ID) | ||
val book6 = BookEntity(6, "Arc of Witches", 34.99, OTHER_BOOKS_CATEGORY_ID) | ||
val book7 = BookEntity(7, "Can I Run?", 99.99, NOVELS_CATEGORY_ID) | ||
val book8 = BookEntity(8, "Basic of Physics", 10.50, EDUCATIONAL_BOOKS_CATEGORY_ID) | ||
|
||
val categoryDAO = getInstance(libraryApplication).getCategoryDAO() | ||
categoryDAO.addCategory(category1) | ||
categoryDAO.addCategory(category2) | ||
categoryDAO.addCategory(category3) | ||
|
||
val bookDAO = getInstance(libraryApplication).getBookDAO() | ||
bookDAO.addBook(book1) | ||
bookDAO.addBook(book2) | ||
bookDAO.addBook(book3) | ||
bookDAO.addBook(book4) | ||
bookDAO.addBook(book5) | ||
bookDAO.addBook(book6) | ||
bookDAO.addBook(book7) | ||
bookDAO.addBook(book8) | ||
} | ||
|
||
private lateinit var libraryApplication: Application | ||
|
||
private var instance: LibraryDatabase? = null | ||
fun getInstance(application: Application): LibraryDatabase { | ||
libraryApplication = application | ||
return if (instance == null) synchronized(this) { | ||
instance = Room.databaseBuilder(application, LibraryDatabase::class.java, "library_database") | ||
.fallbackToDestructiveMigration() | ||
.addCallback(databaseCallback) | ||
.build() | ||
instance!! | ||
} else | ||
instance!! | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.