-
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
0 parents
commit 8232b7d
Showing
58 changed files
with
2,339 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
*.iml | ||
.gradle | ||
/local.properties | ||
/.idea/caches | ||
/.idea/libraries | ||
/.idea/modules.xml | ||
/.idea/workspace.xml | ||
/.idea/navEditor.xml | ||
/.idea/assetWizardSettings.xml | ||
.DS_Store | ||
/build | ||
/captures | ||
.externalNativeBuild | ||
.cxx |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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 @@ | ||
/build |
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,57 @@ | ||
apply plugin: 'com.android.application' | ||
|
||
apply plugin: 'kotlin-android' | ||
|
||
apply plugin: 'kotlin-android-extensions' | ||
|
||
apply plugin: 'kotlin-kapt' | ||
|
||
android { | ||
compileSdkVersion 29 | ||
buildToolsVersion "29.0.2" | ||
defaultConfig { | ||
applicationId "com.sample.libraryapplication" | ||
minSdkVersion 21 | ||
targetSdkVersion 29 | ||
versionCode 1 | ||
versionName "1.0.0" | ||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
} | ||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
dataBinding { | ||
enabled true | ||
} | ||
} | ||
|
||
dependencies { | ||
def room_version = "2.2.1" | ||
implementation fileTree(dir: 'libs', include: ['*.jar']) | ||
// kotlin | ||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" | ||
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.2' | ||
// androidx | ||
implementation 'androidx.appcompat:appcompat:1.1.0' | ||
implementation 'androidx.core:core-ktx:1.1.0' | ||
implementation 'androidx.constraintlayout:constraintlayout:1.1.3' | ||
implementation 'androidx.recyclerview:recyclerview:1.0.0' | ||
implementation 'androidx.cardview:cardview:1.0.0' | ||
// room | ||
implementation "androidx.room:room-runtime:$room_version" | ||
implementation "androidx.room:room-ktx:$room_version" | ||
kapt "androidx.room:room-compiler:$room_version" | ||
// design | ||
implementation 'com.google.android.material:material:1.0.0' | ||
// lifecycle | ||
implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0' | ||
implementation "androidx.lifecycle:lifecycle-runtime:2.1.0" | ||
kapt "androidx.lifecycle:lifecycle-compiler:2.1.0" | ||
|
||
testImplementation 'junit:junit:4.12' | ||
androidTestImplementation 'androidx.test:runner:1.2.0' | ||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' | ||
} |
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,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 |
24 changes: 24 additions & 0 deletions
24
app/src/androidTest/java/com/sample/libraryapplication/ExampleInstrumentedTest.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 | ||
|
||
import androidx.test.platform.app.InstrumentationRegistry | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
|
||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
import org.junit.Assert.* | ||
|
||
/** | ||
* Instrumented test, which will execute on an Android device. | ||
* | ||
* See [testing documentation](http://d.android.com/tools/testing). | ||
*/ | ||
@RunWith(AndroidJUnit4::class) | ||
class ExampleInstrumentedTest { | ||
@Test | ||
fun useAppContext() { | ||
// Context of the app under test. | ||
val appContext = InstrumentationRegistry.getInstrumentation().targetContext | ||
assertEquals("com.sample.libraryapplication", appContext.packageName) | ||
} | ||
} |
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,25 @@ | ||
<?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.sample.libraryapplication"> | ||
|
||
<application | ||
android:name=".LibraryApplication" | ||
android:allowBackup="true" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/AppTheme" | ||
tools:ignore="GoogleAppIndexingWarning"> | ||
<activity android:name=".view.BookActivity"/> | ||
<activity android:name=".view.BookListActivity"> | ||
<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
15
app/src/main/java/com/sample/libraryapplication/LibraryApplication.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,15 @@ | ||
package com.sample.libraryapplication | ||
|
||
import android.app.Application | ||
|
||
class LibraryApplication : Application() { | ||
|
||
companion object { | ||
lateinit var instance: LibraryApplication | ||
} | ||
|
||
override fun onCreate() { | ||
super.onCreate() | ||
instance = this | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.sample.libraryapplication.database | ||
|
||
import androidx.room.Database | ||
import androidx.room.Room | ||
import androidx.room.RoomDatabase | ||
import androidx.sqlite.db.SupportSQLiteDatabase | ||
import com.sample.libraryapplication.LibraryApplication | ||
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().getCategoryDAO() | ||
categoryDAO.addCategory(category1) | ||
categoryDAO.addCategory(category2) | ||
categoryDAO.addCategory(category3) | ||
|
||
val bookDAO = getInstance().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 var instance: LibraryDatabase? = null | ||
fun getInstance(): LibraryDatabase { | ||
return if (instance == null) synchronized(this) { | ||
instance = Room.databaseBuilder(LibraryApplication.instance, LibraryDatabase::class.java, "library_database") | ||
.fallbackToDestructiveMigration() | ||
.addCallback(databaseCallback) | ||
.build() | ||
instance!! | ||
} else | ||
instance!! | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
app/src/main/java/com/sample/libraryapplication/database/dao/BookDAO.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,23 @@ | ||
package com.sample.libraryapplication.database.dao | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.room.* | ||
import com.sample.libraryapplication.database.entity.BookEntity | ||
|
||
@Dao | ||
interface BookDAO { | ||
@Insert | ||
fun addBook(book: BookEntity) : Long | ||
|
||
@Update | ||
fun updateBook(book: BookEntity) | ||
|
||
@Delete | ||
fun deleteBook(book: BookEntity?) | ||
|
||
@Query("SELECT * FROM books") | ||
fun getAllBooks() : LiveData<List<BookEntity>> | ||
|
||
@Query("SELECT * FROM books WHERE book_category_id == :categoryID") | ||
fun getCategoryBooks(categoryID: Long) : LiveData<List<BookEntity>> | ||
} |
23 changes: 23 additions & 0 deletions
23
app/src/main/java/com/sample/libraryapplication/database/dao/CategoryDAO.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,23 @@ | ||
package com.sample.libraryapplication.database.dao | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.room.* | ||
import com.sample.libraryapplication.database.entity.CategoryEntity | ||
|
||
@Dao | ||
interface CategoryDAO { | ||
@Insert | ||
fun addCategory(category: CategoryEntity) : Long | ||
|
||
@Update | ||
fun updateCategory(category: CategoryEntity) | ||
|
||
@Delete | ||
fun deleteCategory(category: CategoryEntity?) | ||
|
||
@Query("SELECT * FROM categories") | ||
fun getAllCategories() : LiveData<List<CategoryEntity>> | ||
|
||
@Query("SELECT * FROM categories WHERE category_id == :categoryID") | ||
fun getCategory(categoryID: Long) : CategoryEntity? | ||
} |
Oops, something went wrong.