Skip to content

Commit

Permalink
3rd edition
Browse files Browse the repository at this point in the history
  • Loading branch information
javafa committed Nov 10, 2021
1 parent b0b5887 commit 135d794
Show file tree
Hide file tree
Showing 88 changed files with 2,491 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Firebase/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*.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
local.properties
3 changes: 3 additions & 0 deletions Firebase/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Firebase/.idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions Firebase/.idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions Firebase/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Firebase/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Firebase/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
52 changes: 52 additions & 0 deletions Firebase/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'com.google.gms.google-services'
}

android {
compileSdk 31

defaultConfig {
applicationId "net.flow9.thisiskotlin.firebase"
minSdk 21
targetSdk 31
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
viewBinding true
}
}

dependencies {
// 두 줄 추가
implementation platform('com.google.firebase:firebase-bom:29.0.0')
implementation 'com.google.firebase:firebase-analytics-ktx'
// Realtime Database
implementation 'com.google.firebase:firebase-database-ktx'

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
69 changes: 69 additions & 0 deletions Firebase/app/google-services.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"project_info": {
"project_number": "304871030893",
"firebase_url": "https://this-is-android-with-kot-df246-default-rtdb.asia-southeast1.firebasedatabase.app",
"project_id": "this-is-android-with-kot-df246",
"storage_bucket": "this-is-android-with-kot-df246.appspot.com"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:304871030893:android:7934b217728bb20e88bf64",
"android_client_info": {
"package_name": "net.flow9.thisiskotlin.firebase"
}
},
"oauth_client": [
{
"client_id": "304871030893-ab02iq9kro1lb0hsd0lj0k7rfeupjvr8.apps.googleusercontent.com",
"client_type": 3
}
],
"api_key": [
{
"current_key": "AIzaSyDtpT4MZVMAlu7tqzTsqsdiLW8iCIuvINg"
}
],
"services": {
"appinvite_service": {
"other_platform_oauth_client": [
{
"client_id": "304871030893-ab02iq9kro1lb0hsd0lj0k7rfeupjvr8.apps.googleusercontent.com",
"client_type": 3
}
]
}
}
},
{
"client_info": {
"mobilesdk_app_id": "1:304871030893:android:3be7ad65a6cd1aa088bf64",
"android_client_info": {
"package_name": "net.flow9.thisiskotlin.firebasechat"
}
},
"oauth_client": [
{
"client_id": "304871030893-ab02iq9kro1lb0hsd0lj0k7rfeupjvr8.apps.googleusercontent.com",
"client_type": 3
}
],
"api_key": [
{
"current_key": "AIzaSyDtpT4MZVMAlu7tqzTsqsdiLW8iCIuvINg"
}
],
"services": {
"appinvite_service": {
"other_platform_oauth_client": [
{
"client_id": "304871030893-ab02iq9kro1lb0hsd0lj0k7rfeupjvr8.apps.googleusercontent.com",
"client_type": 3
}
]
}
}
}
],
"configuration_version": "1"
}
21 changes: 21 additions & 0 deletions Firebase/app/proguard-rules.pro
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package net.flow9.thisiskotlin.firebase

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("net.flow9.thisiskotlin.firebase", appContext.packageName)
}
}
23 changes: 23 additions & 0 deletions Firebase/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.flow9.thisiskotlin.firebase">

<application
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/Theme.Firebase">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package net.flow9.thisiskotlin.firebase

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.google.firebase.database.*
import com.google.firebase.database.ktx.database
import com.google.firebase.ktx.Firebase
import net.flow9.thisiskotlin.firebase.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

val database = Firebase.database("https://this-is-android-with-kot-df246-default-rtdb.asia-southeast1.firebasedatabase.app")
val myRef = database.getReference("users")

val binding by lazy { ActivityMainBinding.inflate(layoutInflater)}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)

with(binding){
btnPost.setOnClickListener {
val name = editName.text.toString()
val password = editPassword.text.toString()
val user = User(name, password)
addItem(user)
}
}

myRef.addValueEventListener( object: ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
// 먼저 textList를 지운다
with(binding) {
textList.setText("")
for (item in snapshot.children) {
item.getValue(User::class.java)?.let { user ->
textList.append("${user.name} : ${user.password} \n")
}
}
}

}

override fun onCancelled(error: DatabaseError) {
print(error.message)
}
})

// write()
// readOnce()
// readData()
}
// 목록 형태로 추가하기
fun addItem(user:User) {
val id = myRef.push().key!!
user.id = id
myRef.child(id).setValue(user)
}
// 계속 읽기
fun readData() {
myRef.child("name").addValueEventListener( object: ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
Log.d("파이어베이스", "${snapshot.value}")
print( snapshot.value ) // 값이 변경 될때마다 매번 호출
}
override fun onCancelled(error: DatabaseError) {
print( error.message )
}
})
}
// 한번 읽기
fun readOnce() {
myRef.child("name").get().addOnSuccessListener {
Log.d("파이어베이스", "name=${it.value}") // 한 번 호출
}.addOnFailureListener {
Log.d("파이어베이스", "error=${it}")
}
}
// 쓰기
fun write() {
myRef.child("name").setValue("Scott")
myRef.child("age").setValue(19)
}
}


class User {
var id:String = ""
var name:String = ""
var password:String = ""

constructor() // 파이어베이스에서 데이터 변환을 위해서 필요

constructor(name:String, password:String) {
this.name = name
this.password = password
}
}
Loading

0 comments on commit 135d794

Please sign in to comment.