Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
AzharRivaldi authored Mar 23, 2021
1 parent a603900 commit 9900f0b
Show file tree
Hide file tree
Showing 49 changed files with 1,834 additions and 0 deletions.
51 changes: 51 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'

android {
compileSdkVersion 29
buildToolsVersion "30.0.2"

defaultConfig {
applicationId "com.azhar.kodepos"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}

realm {
syncEnabled = true;
}

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

implementation 'com.google.android.material:material:1.3.0'

implementation 'com.amitshekhar.android:android-networking:1.0.2'

implementation 'com.github.ivbaranov:materialfavoritebutton:0.1.5'

}
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
30 changes: 30 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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.azhar.kodepos">

<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/AppTheme"
android:usesCleartextTraffic="true"
tools:targetApi="q">
<activity android:name=".activities.FavoriteActivity" />
<activity
android:name=".activities.MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="stateAlwaysHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

</manifest>
90 changes: 90 additions & 0 deletions app/src/main/java/com/azhar/kodepos/activities/FavoriteActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.azhar.kodepos.activities

import android.app.Activity
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.azhar.kodepos.R
import com.azhar.kodepos.adapter.FavoriteAdapter
import com.azhar.kodepos.model.ModelMain
import com.azhar.kodepos.realm.RealmHelper
import kotlinx.android.synthetic.main.activity_favorite.*
import java.util.*

class FavoriteActivity : AppCompatActivity() {

var modelMainList: List<ModelMain> = ArrayList()
var helper: RealmHelper? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_favorite)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
}

if (Build.VERSION.SDK_INT >= 21) {
setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, false)
window.statusBarColor = Color.TRANSPARENT
}

helper = RealmHelper(this)

setSupportActionBar(toolbar)
assert(supportActionBar != null)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setDisplayShowTitleEnabled(false)

rvListKodePos.setLayoutManager(LinearLayoutManager(this))
rvListKodePos.setHasFixedSize(true)

getFavorite()
}

private fun getFavorite() {
modelMainList = helper!!.showFavorite()
if (modelMainList.size == 0) {
tvNoData.visibility = View.VISIBLE
rvListKodePos.visibility = View.GONE
} else {
tvNoData.visibility = View.GONE
rvListKodePos.visibility = View.VISIBLE
rvListKodePos.adapter = FavoriteAdapter(this, modelMainList as ArrayList<ModelMain>)
}
}

override fun onResume() {
super.onResume()
getFavorite()
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == android.R.id.home) {
finish()
return true
}
return super.onOptionsItemSelected(item)
}

companion object {
fun setWindowFlag(activity: Activity, bits: Int, on: Boolean) {
val window = activity.window
val layoutParams = window.attributes
if (on) {
layoutParams.flags = layoutParams.flags or bits
} else {
layoutParams.flags = layoutParams.flags and bits.inv()
}
window.attributes = layoutParams
}
}

}
112 changes: 112 additions & 0 deletions app/src/main/java/com/azhar/kodepos/activities/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package com.azhar.kodepos.activities

import android.app.ProgressDialog
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.androidnetworking.AndroidNetworking
import com.androidnetworking.common.Priority
import com.androidnetworking.error.ANError
import com.androidnetworking.interfaces.JSONObjectRequestListener
import com.azhar.kodepos.R
import com.azhar.kodepos.adapter.MainAdapter
import com.azhar.kodepos.model.ModelMain
import com.azhar.kodepos.networking.ApiEndpoint
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.toolbar_main.*
import org.json.JSONException
import org.json.JSONObject
import java.util.*

class MainActivity : AppCompatActivity() {

var mainAdapter: MainAdapter? = null
var modelMain: MutableList<ModelMain> = ArrayList()
var strInputTeks: String = ""
var progressDialog: ProgressDialog? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

progressDialog = ProgressDialog(this)
progressDialog?.setTitle("Mohon Tunggu...")
progressDialog?.setCancelable(false)
progressDialog?.setMessage("Sedang menampilkan data")

imageClear.setVisibility(View.GONE)
linearHasil.setVisibility(View.GONE)

rvListKodePos.setLayoutManager(LinearLayoutManager(this))
rvListKodePos.setHasFixedSize(true)

//membuka menu favorite
imageFavorite.setOnClickListener {
val intent = Intent(this@MainActivity, FavoriteActivity::class.java)
startActivity(intent)
}

//menghapus list hasil
imageClear.setOnClickListener {
teksInput.getText().clear()
modelMain.clear()
linearHasil.setVisibility(View.GONE)
imageClear.setVisibility(View.GONE)
}

//action menampilkan data
btnPencarian.setOnClickListener {
strInputTeks = teksInput.getText().toString()
if (strInputTeks.isEmpty()) {
Toast.makeText(this@MainActivity, "Form tidak boleh kosong!", Toast.LENGTH_SHORT).show()
} else {
//menampilkan data
getKodePos(strInputTeks)
}
}
}

private fun getKodePos(strInputTeks: String) {
progressDialog?.show()
modelMain.clear()
AndroidNetworking.get(ApiEndpoint.BASEURL)
.addPathParameter("query", strInputTeks)
.setPriority(Priority.MEDIUM)
.build()
.getAsJSONObject(object : JSONObjectRequestListener {
override fun onResponse(response: JSONObject) {
progressDialog?.dismiss()
try {
val jsonArray = response.getJSONArray("data")
for (i in 0 until jsonArray.length()) {
val jsonObjectKata = jsonArray.getJSONObject(i)
val dataModel = ModelMain()
dataModel.strProvinsi = jsonObjectKata.getString("province")
dataModel.strKabupaten = jsonObjectKata.getString("city")
dataModel.strKecamatan = jsonObjectKata.getString("subdistrict")
dataModel.strKelurahan = jsonObjectKata.getString("urban")
dataModel.strKodePos = jsonObjectKata.getString("postalcode")
modelMain.add(dataModel)
}
mainAdapter = MainAdapter(this@MainActivity, modelMain)
rvListKodePos.adapter = mainAdapter
mainAdapter?.notifyDataSetChanged()
imageClear.visibility = View.VISIBLE
linearHasil.visibility = View.VISIBLE
} catch (e: JSONException) {
Toast.makeText(this@MainActivity, "Oops, gagal menampilkan jenis dokumen.",
Toast.LENGTH_SHORT).show()
}
}

override fun onError(anError: ANError) {
progressDialog?.dismiss()
Toast.makeText(this@MainActivity, "Oops! Sepertinya ada masalah dengan koneksi internet kamu.",
Toast.LENGTH_SHORT).show()
}
})
}
}
73 changes: 73 additions & 0 deletions app/src/main/java/com/azhar/kodepos/adapter/FavoriteAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.azhar.kodepos.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.azhar.kodepos.R
import com.azhar.kodepos.model.ModelMain
import com.azhar.kodepos.realm.RealmHelper
import com.google.android.material.snackbar.Snackbar
import io.realm.Realm
import kotlinx.android.synthetic.main.list_item_favorite.view.*

/**
* Created by Azhar Rivaldi on 20-03-2021
*/

class FavoriteAdapter(private val context: Context, private val modelMain: List<ModelMain>) :
RecyclerView.Adapter<FavoriteAdapter.ViewHolder>() {

var realm: Realm? = null
var helper: RealmHelper? = null

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.list_item_favorite, parent, false)
return ViewHolder(view)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val data = modelMain[position]
helper = RealmHelper(context)
realm = Realm.getDefaultInstance()
holder.tvProvinsi.text = data.strProvinsi
holder.tvKabupaten.text = data.strKabupaten
holder.tvKecamatan.text = data.strKecamatan
holder.tvKelurahan.text = data.strKelurahan
holder.tvKodePos.text = data.strKodePos
holder.imageDelete.setOnClickListener { view ->
helper!!.deleteKodePos(data.strKelurahan!!)
modelMain.removeAt(position)
notifyItemRemoved(position)
notifyItemRangeChanged(0, modelMain.size)
notifyDataSetChanged()
Snackbar.make(view, data.strKelurahan + " Removed from Favorite", Snackbar.LENGTH_SHORT).show()
}
}

override fun getItemCount(): Int {
return modelMain.size
}

internal class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var imageDelete: ImageView
var tvProvinsi: TextView
var tvKabupaten: TextView
var tvKecamatan: TextView
var tvKelurahan: TextView
var tvKodePos: TextView

init {
imageDelete = itemView.imageDelete
tvProvinsi = itemView.tvProvinsi
tvKabupaten = itemView.tvKabupaten
tvKecamatan = itemView.tvKecamatan
tvKelurahan = itemView.tvKelurahan
tvKodePos = itemView.tvKodePos
}
}

}
Loading

0 comments on commit 9900f0b

Please sign in to comment.