Skip to content

Commit 3cf010d

Browse files
Room Example added
1 parent 3d105b5 commit 3cf010d

44 files changed

Lines changed: 1019 additions & 0 deletions

Some content is hidden

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

RoomAndroidExample/.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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

RoomAndroidExample/app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
apply plugin: 'com.android.application'
2+
3+
apply plugin: 'kotlin-android'
4+
5+
apply plugin: 'kotlin-android-extensions'
6+
apply plugin: 'kotlin-kapt'
7+
8+
9+
10+
android {
11+
compileSdkVersion 28
12+
defaultConfig {
13+
applicationId "com.example.roomandroidexample"
14+
minSdkVersion 21
15+
targetSdkVersion 28
16+
versionCode 1
17+
versionName "1.0"
18+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
19+
}
20+
buildTypes {
21+
release {
22+
minifyEnabled false
23+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
24+
}
25+
}
26+
}
27+
28+
dependencies {
29+
implementation fileTree(dir: 'libs', include: ['*.jar'])
30+
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
31+
implementation 'androidx.appcompat:appcompat:1.0.2'
32+
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
33+
testImplementation 'junit:junit:4.12'
34+
androidTestImplementation 'androidx.test:runner:1.1.1'
35+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
36+
37+
kapt 'androidx.room:room-compiler:2.0.0'
38+
implementation "android.arch.persistence.room:runtime:1.0.0"
39+
implementation 'androidx.recyclerview:recyclerview:1.0.0'
40+
implementation 'androidx.cardview:cardview:1.0.0'
41+
implementation 'com.google.code.gson:gson:2.8.5'
42+
implementation 'com.google.android.material:material:1.0.0'
43+
44+
}
Lines changed: 21 additions & 0 deletions
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
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.example.roomandroidexample
2+
3+
import androidx.test.InstrumentationRegistry
4+
import androidx.test.runner.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.getTargetContext()
22+
assertEquals("com.example.roomandroidexample", appContext.packageName)
23+
}
24+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
package="com.example.roomandroidexample">
5+
6+
<application
7+
android:allowBackup="true"
8+
android:icon="@mipmap/ic_launcher"
9+
android:label="@string/app_name"
10+
android:roundIcon="@mipmap/ic_launcher_round"
11+
android:supportsRtl="true"
12+
android:theme="@style/AppTheme">
13+
<activity android:name=".MainActivity">
14+
<intent-filter>
15+
<action android:name="android.intent.action.MAIN"/>
16+
17+
<category android:name="android.intent.category.LAUNCHER"/>
18+
</intent-filter>
19+
</activity>
20+
<activity android:name=".AddUserActivity"/>
21+
</application>
22+
23+
</manifest>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.example.roomandroidexample
2+
3+
import android.os.Bundle
4+
import androidx.appcompat.app.AppCompatActivity
5+
import android.widget.Toast
6+
import kotlinx.android.synthetic.main.activity_add_user.*
7+
8+
class AddUserActivity : AppCompatActivity() {
9+
10+
override fun onCreate(savedInstanceState: Bundle?) {
11+
super.onCreate(savedInstanceState)
12+
setContentView(R.layout.activity_add_user)
13+
14+
15+
val repo = UserRepository(this)
16+
17+
button_save_user.setOnClickListener {
18+
if (ed_username.text.isNotEmpty() && ed_email.text.isNotEmpty() && ed_location.text.isNotEmpty()) {
19+
val user = Users(
20+
userName = ed_username.text.toString(),
21+
location = ed_location.text.toString(),
22+
email = ed_email.text.toString()
23+
)
24+
repo.insertUser(user)
25+
} else {
26+
Toast.makeText(this, "Invalid Input", Toast.LENGTH_SHORT).show()
27+
}
28+
finish()
29+
}
30+
}
31+
32+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.example.roomandroidexample
2+
3+
import androidx.room.Database
4+
import androidx.room.Room
5+
import androidx.room.RoomDatabase
6+
import androidx.room.TypeConverters
7+
import android.content.Context
8+
9+
@Database(entities = [Users::class], version = 1, exportSchema = false)
10+
@TypeConverters(Converters::class)
11+
abstract class AppDatabase : RoomDatabase() {
12+
13+
abstract fun userDao() : UserDao
14+
15+
companion object {
16+
private var INSTANCE: AppDatabase? = null
17+
18+
fun getInstance(context: Context): AppDatabase? {
19+
if (INSTANCE == null) {
20+
synchronized(AppDatabase::class) {
21+
INSTANCE = Room.databaseBuilder(context.applicationContext,
22+
AppDatabase::class.java, "user.db").allowMainThreadQueries()
23+
.build()
24+
}
25+
}
26+
return INSTANCE
27+
}
28+
29+
fun destroyInstance() {
30+
INSTANCE = null
31+
}
32+
}
33+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.example.roomandroidexample
2+
3+
import android.os.Handler
4+
import android.os.Looper
5+
6+
import java.util.concurrent.Executor
7+
import java.util.concurrent.Executors
8+
9+
/**
10+
* Global executor pools for the whole application.
11+
*
12+
*
13+
* Grouping tasks like this avoids the effects of task starvation (e.g. disk reads don't wait behind
14+
* webservice requests).
15+
*/
16+
class AppExecutors private constructor(
17+
private val diskIO: Executor,
18+
private val networkIO: Executor,
19+
private val mainThread: Executor
20+
) {
21+
22+
fun diskIO(): Executor {
23+
return diskIO
24+
}
25+
26+
fun mainThread(): Executor {
27+
return mainThread
28+
}
29+
30+
fun networkIO(): Executor {
31+
return networkIO
32+
}
33+
34+
private class MainThreadExecutor : Executor {
35+
private val mainThreadHandler = Handler(Looper.getMainLooper())
36+
37+
override fun execute(command: Runnable) {
38+
mainThreadHandler.post(command)
39+
}
40+
}
41+
42+
companion object {
43+
44+
// For Singleton instantiation
45+
private val LOCK = Any()
46+
private var sInstance: AppExecutors? = null
47+
48+
val instance: AppExecutors
49+
get() {
50+
if (sInstance == null) {
51+
synchronized(LOCK) {
52+
sInstance = AppExecutors(
53+
Executors.newSingleThreadExecutor(),
54+
Executors.newFixedThreadPool(3),
55+
MainThreadExecutor()
56+
)
57+
}
58+
}
59+
return sInstance!!
60+
}
61+
}
62+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.example.roomandroidexample
2+
3+
import androidx.room.TypeConverter
4+
import com.google.gson.Gson
5+
import com.google.gson.reflect.TypeToken
6+
7+
class Converters {
8+
9+
@TypeConverter
10+
fun fromString(value: String): List<String> {
11+
val listType = object : TypeToken<List<String>>() {
12+
13+
}.type
14+
return Gson().fromJson(value, listType)
15+
}
16+
17+
@TypeConverter
18+
fun fromArrayList(list: List<String>): String {
19+
val gson = Gson()
20+
return gson.toJson(list)
21+
}
22+
}

0 commit comments

Comments
 (0)