Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
aamfahrur committed Jan 7, 2021
0 parents commit b196c62
Show file tree
Hide file tree
Showing 46 changed files with 1,202 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .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 .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 .idea/compiler.xml

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

21 changes: 21 additions & 0 deletions .idea/gradle.xml

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

25 changes: 25 additions & 0 deletions .idea/jarRepositories.xml

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

9 changes: 9 additions & 0 deletions .idea/misc.xml

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

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

android {
compileSdkVersion 30
buildToolsVersion "30.0.3"

defaultConfig {
applicationId "com.aamfahrur.crudfirebase"
minSdkVersion 19
targetSdkVersion 30
versionCode 1
versionName "1.0"
multiDexEnabled true

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'
}
}

dependencies {

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.13.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

//firebase
// Import the Firebase BoM
implementation platform('com.google.firebase:firebase-bom:25.12.0')
implementation 'com.google.firebase:firebase-firestore:22.0.1'
implementation 'com.google.firebase:firebase-core:18.0.0'
implementation 'com.google.firebase:firebase-analytics-ktx'
implementation 'com.firebaseui:firebase-ui-firestore:5.0.0'
//material design
implementation 'com.google.android.material:material:1.2.1'
}
39 changes: 39 additions & 0 deletions app/google-services.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"project_info": {
"project_number": "1621856111",
"project_id": "crudfirebase-8316a",
"storage_bucket": "crudfirebase-8316a.appspot.com"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:1621856111:android:c07a5f7b831bdbccb0e29a",
"android_client_info": {
"package_name": "com.aamfahrur.crudfirebase"
}
},
"oauth_client": [
{
"client_id": "1621856111-8ag8epoak1fm1jl7sqmgn9k6of2u91sp.apps.googleusercontent.com",
"client_type": 3
}
],
"api_key": [
{
"current_key": "AIzaSyABPVGchVX5S4EElwYd4X3BtZ4768bEj2A"
}
],
"services": {
"appinvite_service": {
"other_platform_oauth_client": [
{
"client_id": "1621856111-8ag8epoak1fm1jl7sqmgn9k6of2u91sp.apps.googleusercontent.com",
"client_type": 3
}
]
}
}
}
],
"configuration_version": "1"
}
21 changes: 21 additions & 0 deletions 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 com.aamfahrur.crudfirebase

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

<uses-permission android:name="android.permission.INTERNET" />

<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.CRUDFirebase">
<activity android:name=".EditActivity"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

</manifest>
69 changes: 69 additions & 0 deletions app/src/main/java/com/aamfahrur/crudfirebase/EditActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.aamfahrur.crudfirebase

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.Toast
import com.google.android.material.textfield.TextInputEditText
import com.google.firebase.firestore.FirebaseFirestore

class EditActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_edit)
val db = FirebaseFirestore.getInstance()
val collection = db.collection("pengguna")
val pserial = intent.getSerializableExtra("pengguna")

val submit = findViewById<Button>(R.id.submitEdit)
val delete = findViewById<Button>(R.id.deleteEdit)
val username = findViewById<TextInputEditText>(R.id.usernameEdit)
val fullname = findViewById<TextInputEditText>(R.id.fullnameEdit)
val email = findViewById<TextInputEditText>(R.id.emailEdit)
val address = findViewById<TextInputEditText>(R.id.addressEdit)

val pengguna = if(pserial != null){
delete.visibility = View.VISIBLE
pserial as HashMap<*,*>
}else{
null
}

if(pengguna != null){
username.setText(pengguna["username"].toString())
fullname.setText(pengguna["name"].toString())
email.setText(pengguna["email"].toString())
address.setText(pengguna["address"].toString())
}

delete.setOnClickListener {
collection.document(pengguna!!["id"].toString()).delete().addOnSuccessListener {
Toast.makeText(this, "Success delete data with id ${pengguna["id"]}", Toast.LENGTH_SHORT).show()
onBackPressed()
}
}

submit.setOnClickListener {
val data = hashMapOf(
"username" to username.text.toString(),
"name" to fullname.text.toString(),
"email" to email.text.toString(),
"address" to address.text.toString()
)
if(pengguna == null){
collection.add(data).addOnSuccessListener {documentReference ->
Toast.makeText(this, "Success add new data with id ${documentReference.id}", Toast.LENGTH_SHORT).show()
onBackPressed()
}
}else{
@Suppress("UNCHECKED_CAST")
collection.document(pengguna["id"].toString()).update(data as Map<String, Any>).addOnSuccessListener {
Toast.makeText(this, "Success update data with id ${pengguna["id"]}", Toast.LENGTH_SHORT).show()
onBackPressed()
}
}
}
}
}
52 changes: 52 additions & 0 deletions app/src/main/java/com/aamfahrur/crudfirebase/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.aamfahrur.crudfirebase

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.firebase.firestore.FirebaseFirestore

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val db = FirebaseFirestore.getInstance()
val rv = findViewById<RecyclerView>(R.id.recView)
var pengguna: ArrayList<HashMap<String, String>>
val layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
rv.layoutManager = layoutManager
val collection = db.collection("pengguna")
val fab = findViewById<FloatingActionButton>(R.id.fab)
fab.setOnClickListener{
startActivity(Intent(this, EditActivity::class.java))
}

collection.addSnapshotListener { snapshot, e ->
if (e != null) {
Log.w("errorsnapshot", "Listen failed.", e)
return@addSnapshotListener
}

if (snapshot != null) {
pengguna = ArrayList()
for(document in snapshot){
val hm: HashMap<String, String> = hashMapOf(
"id" to document.id,
"name" to document.get("name").toString(),
"username" to document.get("username").toString(),
"email" to document.get("email").toString(),
"address" to document.get("address").toString()
)
pengguna.add(hm)
}
val adapter = RVAdapter(pengguna, this)
rv.adapter = adapter
} else {
Log.d("snapdataerr", "Current data: null")
}
}
}
}
Loading

0 comments on commit b196c62

Please sign in to comment.