Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[충남대 Android_전영주] 1단계 미션 제출합니다. #1

Open
wants to merge 12 commits into
base: aengzu
Choose a base branch
from
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,39 @@
# android-contacts
## 📣 1단계 기능 업로드 (2024.06.25)
---
### 1. 스켈레톤 업로드

- Contact 모델 스켈레톤 코드 제작
- 속성 : name : String , phoneNumber: String, email : String, birth: String, gender: String, memo: String
- ContactRepository 스켈레톤 코드 제작
- 속성 : 전화번호부 리스트 contacts : mutableListOf<Contact>
- 리스트에 전화번호를 추가, 삭제, 조회 기능
- ActivityContact 스켈레톤 코드 제작

### 2. 리소스 업로드

- 더보기 아이콘 업로드
- 디폴트 프로필 이미지 업로드
- 커스텀 버튼 드로어블 업로드

### 3. 뷰 제작

- activity_contact.xml 뷰 제작하기
- name : EditText , phoneNumber: EditText, gender : RadioGroup, birth: EditText, button: Button

### 4. 기능 구현

- 액티비티 뷰 findViewById 하기
- 더보기 클릭 시 선택 요소(생일, 성별, 메모) 표시하기
- 생일 뷰 클릭 시 DatePickerDialog 띄우기
- 성별 뷰는 radio button 으로 제작되어 클릭 시 체크 표시 후 선택된 성별로 세팅하기
- 저장 버튼 클릭 시
- 입력 폼 조건 확인하기
- 이름과 전화번호가 입력되었나요?
- 조건에 부합하지 않을 경우 토스트 메시지(조건 부합X) 띄우기
- 조건에 부합할 경우
- ContactRepository contacts List 에 저장하기
- 토스트 메시지(저장 완료) 띄우기
- 취소 버튼 클릭 시
- 토스트 메시지 토스트 메시지(취소) 띄우기
- 모든 속성 값 초기화하기
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ dependencies {
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.11.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("androidx.activity:activity:1.8.0")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
android:supportsRtl="true"
android:theme="@style/Theme.Contacts"
tools:targetApi="31">
<activity
android:name=".ContactActivity"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
Expand All @@ -23,4 +26,4 @@
</activity>
</application>

</manifest>
</manifest>
10 changes: 10 additions & 0 deletions app/src/main/java/campus/tech/kakao/contacts/Contact.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package campus.tech.kakao.contacts

data class Contact(
val name: String,
val phoneNumber: String,
val email: String,
val birth: String,
val gender: String,
val memo: String
)
138 changes: 138 additions & 0 deletions app/src/main/java/campus/tech/kakao/contacts/ContactActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package campus.tech.kakao.contacts

import android.annotation.SuppressLint
import android.app.DatePickerDialog
import android.os.Bundle
import android.util.Log
import android.view.Gravity

import android.widget.EditText
import android.widget.LinearLayout
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider


class ContactActivity : AppCompatActivity() {

private lateinit var nameEditText: EditText
private lateinit var emailEditText: EditText
private lateinit var phoneNumEditText: EditText
private lateinit var genderRadioGroup: RadioGroup
private lateinit var birthEditText: EditText
private lateinit var memoEditText: EditText
private lateinit var saveButton: TextView
private lateinit var cancelButton: TextView
private lateinit var moreButton: LinearLayout
private lateinit var contactViewModel: ContactViewModel


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

nameEditText = findViewById<EditText>(R.id.nameEditText)
phoneNumEditText = findViewById<EditText>(R.id.phoneNumEditText)
emailEditText = findViewById<EditText>(R.id.emailEditText)
birthEditText = findViewById<EditText>(R.id.birthdayEditText)
moreButton = findViewById<LinearLayout>(R.id.moreButton)
memoEditText = findViewById<EditText>(R.id.memoEditText)
genderRadioGroup = findViewById<RadioGroup>(R.id.genderRadioGroup)
saveButton = findViewById<TextView>(R.id.saveButton)
cancelButton = findViewById<TextView>(R.id.cancelButton)

contactViewModel = ViewModelProvider(this)[ContactViewModel::class.java]


moreButton.setOnClickListener {
memoEditText.visibility = TextView.VISIBLE
birthEditText.visibility = TextView.VISIBLE
genderRadioGroup.visibility = TextView.VISIBLE
moreButton.visibility = TextView.GONE
}

birthEditText.setOnClickListener {
showDatePickerDialog()
}

saveButton.setOnClickListener {
val name = nameEditText.text.toString()
val phoneNum = phoneNumEditText.text.toString()
val email = emailEditText.text.toString()
val birth = birthEditText.text.toString()
val gender = getSelectedGender()
val memo = memoEditText.text.toString()
val contact = Contact(name, phoneNum, email, birth, gender, memo)

if (checkTerms(name, phoneNum)) {
saveContact(contact)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

save이후에도 clearFields를 호출해서 초기화 시켜주는 것이 좋을 것 같습니다.

저장후에도 데이터가 남아있어 취소를 눌렀을때 저장이 취소 될 것 같은 느낌을 줍니다.

}
}

cancelButton.setOnClickListener {
clearFields()
showCancelToast()
}


}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

실무에서는 indent를 중요하게 생각하고 lint라는 기능들을 사용하여 따로 체크를 하기도 합니다.

그 이유는 리뷰시 불필요한 라인 변경을 방지하여 리뷰의 효율을 높이기 위함인데요

커밋전에 Mac OS 기준 option + cmd + l 을 누르면 라인 정리가 되니 이후 과제에 참고 부탁드립니다!

private fun showDatePickerDialog(){
val datePickerDialog = DatePickerDialog(this, DatePickerDialog.OnDateSetListener { view, year, month, dayOfMonth ->
birthEditText.setText("$year-${month + 1}-$dayOfMonth")
}, 2021, 0, 1)
datePickerDialog.show()
}

private fun checkTerms(name: String, phoneNum: String): Boolean {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checkTerms 함수는

  1. 이름과 번호를 체크하는기능

  2. 데이터가 충분하지 않을때 토스트를 띄우는 기능

이렇게 두가지 역할을 하고 있는데요

checkTerms을 사용하는 부분에서 false가 나왔을때 토스트를 띄우도록 하여 함수가 하나의 기능만 하도록 구현하면 더욱 좋을 것 같습니다.

if(name.isEmpty() || phoneNum.isEmpty()){
Toast.makeText(this, "이름과 전화번호는 필수 입력사항입니다.", Toast.LENGTH_SHORT).show()
return false
} else {
return true
}
}

private fun getSelectedGender(): String {
val selectedId = genderRadioGroup.checkedRadioButtonId
return if (selectedId != -1) {
val selectedRadioButton = findViewById<RadioButton>(selectedId)
selectedRadioButton.text.toString()
} else {
"미정"
}
}

private fun saveContact(contact: Contact) {
contactViewModel.addContact(contact)
showSaveToast()
Log.d("testt",contactViewModel.getContact(contact.name).toString() )
}

private fun clearFields(){
nameEditText.text.clear()
phoneNumEditText.text.clear()
emailEditText.text.clear()
birthEditText.text.clear()
memoEditText.text.clear()
genderRadioGroup.clearCheck()
memoEditText.visibility = TextView.GONE
birthEditText.visibility = TextView.GONE
genderRadioGroup.visibility = TextView.GONE
moreButton.visibility = TextView.VISIBLE
}

private fun showCancelToast(){
val toast = Toast.makeText(this, "취소 되었습니다", Toast.LENGTH_SHORT)
toast.setGravity(Gravity.TOP, 0, 100)
toast.show()
}

private fun showSaveToast(){
val toast = Toast.makeText(this, "저장이 완료 되었습니다", Toast.LENGTH_SHORT)
toast.setGravity(Gravity.TOP, 0, 100)
toast.show()
}
}
21 changes: 21 additions & 0 deletions app/src/main/java/campus/tech/kakao/contacts/ContactRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package campus.tech.kakao.contacts

class ContactRepository {
private val contacts = mutableListOf<Contact>()

fun addContact(contact: Contact) {
contacts.add(contact)
}

fun getContacts(): List<Contact> {
return contacts
}

fun getContact(name: String): Contact? {
return contacts.find { it.name == name }
}

fun removeContact(name: String) {
contacts.removeIf { it.name == name }
}
}
18 changes: 18 additions & 0 deletions app/src/main/java/campus/tech/kakao/contacts/ContactViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package campus.tech.kakao.contacts
import androidx.lifecycle.ViewModel

class ContactViewModel : ViewModel() {
private val repository: ContactRepository = ContactRepository()

fun addContact(contact: Contact) {
repository.addContact(contact)
}

fun getContacts(): List<Contact> {
return repository.getContacts()
}

fun getContact(name: String): Contact? {
return repository.getContact(name)
}
}
5 changes: 4 additions & 1 deletion app/src/main/java/campus/tech/kakao/contacts/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package campus.tech.kakao.contacts

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val intent = Intent(this, ContactActivity::class.java)
startActivity(intent)
finish()
}
}
Binary file added app/src/main/res/drawable/down.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions app/src/main/res/drawable/edit_box.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:thickness="0dp">
<stroke android:width="1dp" android:color="@color/grey"/>
<solid android:color="#FFFFFF"/>
</shape>
Binary file added app/src/main/res/drawable/plus.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/user.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading