Skip to content

Commit

Permalink
refactor: Apply Hilt for dependency injection in HomeMapActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
yb0x00 committed Jul 29, 2024
1 parent 518a06c commit b70ebac
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ package campus.tech.kakao.map

import android.content.Context
import android.content.SharedPreferences
import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject
import javax.inject.Singleton

class PreferenceRepository(context: Context) {
@Singleton
class PreferenceRepository @Inject constructor(
@ApplicationContext context: Context
) {

private var sharedPrefs: SharedPreferences =
context.getSharedPreferences("location_data", Context.MODE_PRIVATE)
Expand Down
43 changes: 22 additions & 21 deletions app/src/main/java/campus/tech/kakao/map/activity/HomeMapActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,57 @@ import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.widget.EditText
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.lifecycle.ViewModelProvider
import campus.tech.kakao.map.MapViewModelFactory
import campus.tech.kakao.map.PreferenceRepository
import campus.tech.kakao.map.R
import campus.tech.kakao.map.dataContract.LocationDataContract
import campus.tech.kakao.map.databinding.ActivityHomeMapBinding
import campus.tech.kakao.map.viewModel.MapViewModel
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.kakao.vectormap.KakaoMap
import com.kakao.vectormap.KakaoMapReadyCallback
import com.kakao.vectormap.LatLng
import com.kakao.vectormap.MapLifeCycleCallback
import com.kakao.vectormap.MapView
import com.kakao.vectormap.label.LabelOptions
import com.kakao.vectormap.label.LabelStyle
import com.kakao.vectormap.label.LabelStyles
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class HomeMapActivity : AppCompatActivity() {
private lateinit var mapView: MapView
private lateinit var searchBar: EditText
private val bottomSheet: ConstraintLayout by lazy { findViewById(R.id.bottomSheet) }
private lateinit var bottomBehavior: BottomSheetBehavior<ConstraintLayout>
private lateinit var binding: ActivityHomeMapBinding

private lateinit var placeNameTextView: TextView
private lateinit var placeAddressTextView: TextView
private lateinit var mapViewModel: MapViewModel
private lateinit var prefersRepo: PreferenceRepository

private val mapViewModel: MapViewModel by viewModels()

private val bottomSheet: ConstraintLayout by lazy { findViewById(R.id.bottomSheet) }
private lateinit var bottomBehavior: BottomSheetBehavior<ConstraintLayout>

@SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_home_map)

prefersRepo = PreferenceRepository(applicationContext)
mapViewModel =
ViewModelProvider(this, MapViewModelFactory(prefersRepo))[MapViewModel::class.java]
setBinding()

val name = intent.getStringExtra(LocationDataContract.LOCATION_NAME)
val address = intent.getStringExtra(LocationDataContract.LOCATION_ADDRESS)
val latitude = intent.getStringExtra(LocationDataContract.LOCATION_LATITUDE)?.toDouble()
val longitude = intent.getStringExtra(LocationDataContract.LOCATION_LONGITUDE)?.toDouble()

mapView = findViewById(R.id.mapView)
placeNameTextView = findViewById(R.id.placeName)
placeAddressTextView = findViewById(R.id.placeAddress)

bottomBehavior = BottomSheetBehavior.from(bottomSheet)
val intentError = Intent(this, MapErrorActivity::class.java)

//KaKao Map UI에 띄우기
mapView.start(object : MapLifeCycleCallback() {
binding.mapView.start(object : MapLifeCycleCallback() {
override fun onMapDestroy() {
}

Expand Down Expand Up @@ -117,21 +113,20 @@ class HomeMapActivity : AppCompatActivity() {
bottomBehavior.state = BottomSheetBehavior.STATE_HIDDEN
}

searchBar = findViewById(R.id.search_home)
searchBar.setOnClickListener {
binding.searchHome.setOnClickListener {
val intent = Intent(this, DataSearchActivity::class.java)
startActivity(intent)
}
}

override fun onResume() {
super.onResume()
mapView.resume()
binding.mapView.resume()
}

override fun onPause() {
super.onPause()
mapView.pause()
binding.mapView.pause()
}

override fun onDestroy() {
Expand All @@ -141,4 +136,10 @@ class HomeMapActivity : AppCompatActivity() {
intent.getStringExtra(LocationDataContract.LOCATION_LONGITUDE)
?.let { mapViewModel.saveLocation(LocationDataContract.LOCATION_LONGITUDE, it) }
}

private fun setBinding(){
binding = ActivityHomeMapBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package campus.tech.kakao.map.viewModel

import androidx.lifecycle.ViewModel
import campus.tech.kakao.map.PreferenceRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject


class MapViewModel(repository: PreferenceRepository) : ViewModel() {
private val prefersRepo = repository
@HiltViewModel
class MapViewModel @Inject constructor(
private val prefersRepo: PreferenceRepository
) : ViewModel() {

fun saveLocation(locationKey: String, data: String) {
prefersRepo.setString(locationKey, data)
Expand Down
102 changes: 53 additions & 49 deletions app/src/main/res/layout/activity_home_map.xml
Original file line number Diff line number Diff line change
@@ -1,55 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/homeMap"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.HomeMapActivity">

<com.kakao.vectormap.MapView
android:id="@+id/mapView"
xmlns:tools="http://schemas.android.com/tools">

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/homeMap"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="-20dp"
tools:layout_editor_absoluteY="16dp" />

<EditText
android:id="@+id/search_home"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_marginTop="35dp"
android:background="@color/white"
android:elevation="20dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="@string/searchBar_text"
android:paddingStart="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.9" />

<ImageButton
android:id="@+id/clickForSearch_home"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginEnd="15dp"
android:background="@color/transparent"
android:elevation="21dp"
android:src="@drawable/search_icon"
app:layout_constraintBottom_toBottomOf="@id/search_home"
app:layout_constraintEnd_toEndOf="@id/search_home"
app:layout_constraintTop_toTopOf="@id/search_home" />

<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
tools:context=".activity.HomeMapActivity">

<com.kakao.vectormap.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="-20dp"
tools:layout_editor_absoluteY="16dp" />

<EditText
android:id="@+id/search_home"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_marginTop="35dp"
android:background="@color/white"
android:elevation="20dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="@string/searchBar_text"
android:paddingStart="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.9" />

<ImageButton
android:id="@+id/clickForSearch_home"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginEnd="15dp"
android:background="@color/transparent"
android:elevation="21dp"
android:src="@drawable/search_icon"
app:layout_constraintBottom_toBottomOf="@id/search_home"
app:layout_constraintEnd_toEndOf="@id/search_home"
app:layout_constraintTop_toTopOf="@id/search_home" />

<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">

<include layout="@layout/map_detail_bottom_sheet" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

<include layout="@layout/map_detail_bottom_sheet" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

0 comments on commit b70ebac

Please sign in to comment.