Skip to content

Commit

Permalink
add ui
Browse files Browse the repository at this point in the history
  • Loading branch information
dzakdzaks committed Feb 16, 2023
1 parent 492a3eb commit 4c28102
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 23 deletions.
107 changes: 89 additions & 18 deletions app/src/main/java/com/example/nikparser/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
package com.example.nikparser

import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.text.ClickableText
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.dzakdzaks.nikparser.NIKParser
import com.dzakdzaks.nikparser.NIKParserImpl
import com.dzakdzaks.nikparser.model.NIKParserResponse
Expand All @@ -20,15 +36,6 @@ import com.squareup.moshi.Moshi

class MainActivity : ComponentActivity() {

private val nikParser: NIKParser = NIKParserImpl(this) { isDataReady ->
Log.e("walwaw", isDataReady.toString())
}

private val nikParserAdapter by lazy {
Moshi.Builder().build().adapter(NIKParserResponse::class.java)
}


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Expand All @@ -38,25 +45,89 @@ class MainActivity : ComponentActivity() {
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Greeting(nikParserAdapter.toJson(nikParser.parseNik("3275091507010001")))
Home()
}
}
}
}
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun Greeting(name: String, onClickAction: (Int) -> Unit = {}) {
ClickableText(
text = AnnotatedString(name),
onClick = onClickAction
fun Home() {
Scaffold(
topBar = { TopAppBar(title = { Text("NIK Parser") }) },
content = { paddingValues ->
MyContent(paddingValues)
}
)
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MyContent(paddingValues: PaddingValues) {
val context = LocalContext.current

var text by rememberSaveable(stateSaver = TextFieldValue.Saver) {
mutableStateOf(TextFieldValue())
}
val maxChar = 16

val digitPattern = remember { Regex("^\\d+\$") }

var isError by remember { mutableStateOf(false) }

var result by remember { mutableStateOf("") }

val nikParser: NIKParser = NIKParserImpl(context)

val nikParserAdapter by lazy {
Moshi.Builder().build().adapter(NIKParserResponse::class.java)
}

Column(
modifier = Modifier.padding(paddingValues)
) {
OutlinedTextField(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp),
label = { Text("NIK") },
value = text,
onValueChange = {
if (it.annotatedString.length <= 16
&& (it.annotatedString.isEmpty() || it.annotatedString.matches(digitPattern))
) {
text = it
isError = if (text.annotatedString.length == maxChar) {
val nikParseResult = nikParser.parseNik(it.annotatedString.text)
result = nikParserAdapter.toJson(nikParseResult)
!nikParseResult.isValid
} else {
it.annotatedString.isNotEmpty()
}
}
},
isError = isError,
singleLine = true,
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
supportingText = {
Text(
text = if (isError) "NIK not valid" else ""
)
}
)
Text(
modifier = Modifier.padding(16.dp),
text = result
)
}
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
NIKParserTheme {
Greeting("Android")
Home()
}
}
10 changes: 5 additions & 5 deletions nikparser/src/main/java/com/dzakdzaks/nikparser/NIKParserImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext


/**
* @param context for access json assets file
* */
class NIKParserImpl(
private val context: Context,
private val isDataReadyToAction: (Boolean) -> Unit,
private val context: Context
) : NIKParser {

private var provinceList: List<NIKProvince> = emptyList()
Expand Down Expand Up @@ -50,7 +53,6 @@ class NIKParserImpl(

init {
coroutineScope.launch {
isDataReadyToAction(false)
parseJsonData()
}
}
Expand Down Expand Up @@ -292,8 +294,6 @@ class NIKParserImpl(
val zipCode = it.value.split(" -- ")[1].replace("-", "")
NIKDistrict(it.key, name, zipCode)
}

isDataReadyToAction(true)
}
}

Expand Down

0 comments on commit 4c28102

Please sign in to comment.