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

# [Fix] Fragment에서 lifecycleOwner 문제 해결, 리팩터링 #216

Open
wants to merge 19 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ plugins {

Properties properties = new Properties()
properties.load(project.rootProject.file("local.properties").newDataInputStream())
def googleMapApiKey = properties.getProperty('newGoogleMapKey')

android {
compileSdk 31
Expand All @@ -25,7 +24,7 @@ android {
buildConfigField "String", "GOOGLE_MAP_KEY", properties["google_map_key"]
buildConfigField "String", "TOUR_API_KEY", properties["tour_api_key"]
buildConfigField "String", "NEW_TOUR_API_KEY", properties["new_tour_api_key"]
manifestPlaceholders = [googleMapKey:googleMapApiKey]
manifestPlaceholders["google_map_key"] = properties["google_map_key"]
}

buildTypes {
Expand All @@ -51,7 +50,6 @@ android {
}

dependencies {

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.0'
implementation 'com.google.android.material:material:1.4.0'
Expand Down Expand Up @@ -80,7 +78,7 @@ dependencies {

// Glide
implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
kapt 'com.github.bumptech.glide:compiler:4.12.0'

// Navigation
implementation 'androidx.navigation:navigation-fragment-ktx:2.4.0-beta01'
Expand Down
25 changes: 11 additions & 14 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@
android:theme="@style/Theme.Travelog"
android:usesCleartextTraffic="true"
android:usesNonSdkApi="true">
<activity
android:name=".TutorialActivity"
android:exported="true" >

</activity>
<activity
android:name=".SplashActivity"
android:exported="true">
Expand All @@ -34,18 +29,20 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="${googleMapKey}" />

<activity
android:name=".TutorialActivity"
android:exported="true" />
<activity
android:name=".MainActivity"
android:exported="true"
android:windowSoftInputMode="adjustPan"></activity>
android:windowSoftInputMode="adjustPan" />

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="${google_map_key}" />

<receiver
android:name=".menu.alarm.AlarmReceiver"
android:name="com.thequietz.travelog.ui.menu.alarm.AlarmReceiver"
android:exported="false" />

<provider
Expand All @@ -57,6 +54,6 @@
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider" />
</provider>
</application>

</manifest>
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.thequietz.travelog.api

import com.thequietz.travelog.guide.RecommendResponse
import com.thequietz.travelog.ui.guide.RecommendResponse
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.GET
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.thequietz.travelog.api

import com.thequietz.travelog.place.model.PlaceRecommendListResponse
import com.thequietz.travelog.ui.place.model.PlaceRecommendListResponse
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.thequietz.travelog.api

import com.thequietz.travelog.place.model.PlaceRecommendResponse
import com.thequietz.travelog.place.model.detail.RecommendImageResponse
import com.thequietz.travelog.place.model.detail.RecommendResponse
import com.thequietz.travelog.ui.place.model.PlaceRecommendResponse
import com.thequietz.travelog.ui.place.model.detail.RecommendImageResponse
import com.thequietz.travelog.ui.place.model.detail.RecommendResponse
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.thequietz.travelog.api

import com.thequietz.travelog.place.model.PlaceDetailModelResponse
import com.thequietz.travelog.place.model.PlaceSearchModelResponse
import com.thequietz.travelog.ui.place.model.PlaceDetailModelResponse
import com.thequietz.travelog.ui.place.model.PlaceSearchModelResponse
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/thequietz/travelog/api/PlaceService.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.thequietz.travelog.api

import com.thequietz.travelog.guide.AllPlaceResponse
import com.thequietz.travelog.schedule.model.PlaceResponse
import com.thequietz.travelog.ui.guide.AllPlaceResponse
import com.thequietz.travelog.ui.schedule.model.PlaceResponse
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
Expand Down
32 changes: 32 additions & 0 deletions app/src/main/java/com/thequietz/travelog/common/BaseFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.thequietz.travelog.common

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment

abstract class BaseFragment<VDB : ViewDataBinding>(
@LayoutRes private val layoutId: Int
) : Fragment() {
private var _binding: VDB? = null
val binding get() = _binding!!

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = DataBindingUtil.inflate(inflater, layoutId, container, false)
binding.lifecycleOwner = viewLifecycleOwner
return binding.root
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
25 changes: 25 additions & 0 deletions app/src/main/java/com/thequietz/travelog/common/BaseListAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.thequietz.travelog.common

import android.view.ViewGroup
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter

abstract class BaseListAdapter<T, VDB : ViewDataBinding>(
diffCallback: DiffUtil.ItemCallback<T>
) : ListAdapter<T, BaseViewHolder<VDB>>(diffCallback) {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<VDB> {
val binding = createBinding(parent, viewType)
return BaseViewHolder(binding)
}

override fun onBindViewHolder(holder: BaseViewHolder<VDB>, position: Int) {
bind(holder.binding, position)
holder.binding.executePendingBindings()
}

abstract fun createBinding(parent: ViewGroup, viewType: Int): VDB

abstract fun bind(binding: VDB, position: Int)
}
17 changes: 17 additions & 0 deletions app/src/main/java/com/thequietz/travelog/common/BaseViewHolder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.thequietz.travelog.common

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView

class BaseViewHolder<VDB : ViewDataBinding>(val binding: VDB) :
RecyclerView.ViewHolder(binding.root) {
constructor(parent: ViewGroup, @LayoutRes layoutId: Int) : this(
DataBindingUtil.inflate(
LayoutInflater.from(parent.context), layoutId, parent, false
)
)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.thequietz.travelog
package com.thequietz.travelog.common

import android.util.Log
import androidx.lifecycle.ViewModel
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.thequietz.travelog.map
package com.thequietz.travelog.common

import android.Manifest
import android.content.pm.PackageManager
Expand All @@ -15,15 +15,13 @@ import android.view.View
import android.view.ViewGroup
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.LayoutRes
import androidx.core.app.ActivityCompat
import androidx.core.content.res.ResourcesCompat
import androidx.core.graphics.BlendModeColorFilterCompat
import androidx.core.graphics.BlendModeCompat
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
Expand All @@ -39,15 +37,11 @@ import com.google.android.gms.maps.model.Polyline
import com.google.android.gms.maps.model.PolylineOptions
import com.google.android.material.snackbar.Snackbar
import com.thequietz.travelog.R
import com.thequietz.travelog.schedule.data.ColorRGB

abstract class GoogleMapFragment<B : ViewDataBinding, VM : ViewModel> :
Fragment(), OnMapReadyCallback, GoogleMap.OnMarkerClickListener {
protected lateinit var binding: B
abstract val viewModel: VM

abstract val layoutId: Int
import com.thequietz.travelog.ui.schedule.data.ColorRGB

abstract class GoogleMapFragment<VDB : ViewDataBinding>(
@LayoutRes layoutId: Int
) : BaseFragment<VDB>(layoutId), OnMapReadyCallback, GoogleMap.OnMarkerClickListener {
private lateinit var requestPermissionLauncher: ActivityResultLauncher<String>
private val locationPermission = Manifest.permission.ACCESS_COARSE_LOCATION

Expand Down Expand Up @@ -94,15 +88,14 @@ abstract class GoogleMapFragment<B : ViewDataBinding, VM : ViewModel> :
container: ViewGroup?,
savedInstanceState: Bundle?,
): View {
binding = DataBindingUtil.inflate(inflater, layoutId, container, false)
val view = super.onCreateView(inflater, container, savedInstanceState)
(childFragmentManager.findFragmentById(R.id.fragment_map) as SupportMapFragment)
.getMapAsync(this)
return binding.root
return view
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.lifecycleOwner = viewLifecycleOwner
initViewModel()
initTargetList()
updateMapViewBound()
Expand All @@ -116,20 +109,20 @@ abstract class GoogleMapFragment<B : ViewDataBinding, VM : ViewModel> :
setMinZoomPreference(6f)

// viewLifecycleOwnerLiveData 추가
viewLifecycleOwnerLiveData.observe(viewLifecycleOwner, {
viewLifecycleOwnerLiveData.observe(viewLifecycleOwner) {
if (it == null)
return@observe
setOnMapLoadedCallback {
targetList.observe(it, {
targetList.observe(it) {
updateMapViewBound()
moveCameraByTargetCount(targetCount)
drawMarker()
})
markerColorList.observe(it, {
}
markerColorList.observe(it) {
drawMarker()
})
}
}
})
}
uiSettings.apply {
isZoomControlsEnabled = true
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.thequietz.travelog
package com.thequietz.travelog.common

import android.app.Dialog
import android.content.Context
import android.graphics.drawable.AnimationDrawable
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.widget.ImageView
import com.thequietz.travelog.R

class LoadingDialog(context: Context) : Dialog(context) {
override fun onCreate(savedInstanceState: Bundle?) {
Expand Down

This file was deleted.

27 changes: 21 additions & 6 deletions app/src/main/java/com/thequietz/travelog/data/Repository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import com.thequietz.travelog.data.db.dao.NewRecordImageDao
import com.thequietz.travelog.data.db.dao.PlaceDao
import com.thequietz.travelog.data.db.dao.RecommendPlaceDao
import com.thequietz.travelog.data.db.dao.RecordImageDao
import com.thequietz.travelog.getTodayDate
import com.thequietz.travelog.guide.Place
import com.thequietz.travelog.guide.RecommendPlace
import com.thequietz.travelog.record.model.RecordImage
import com.thequietz.travelog.ui.guide.Place
import com.thequietz.travelog.ui.guide.RecommendPlace
import com.thequietz.travelog.ui.record.model.RecordImage
import com.thequietz.travelog.util.getTodayDate
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -204,7 +204,11 @@ class GuideRepository @Inject constructor(
}
}

suspend fun loadFestivalData(areaCode: String, contentTypeId: Int, pageNo: Int): List<RecommendPlace> {
suspend fun loadFestivalData(
areaCode: String,
contentTypeId: Int,
pageNo: Int
): List<RecommendPlace> {
val loadData =
recommendPlaceDao.loadRecommendFestivalByAreaCode(areaCode, contentTypeId)
if (loadData.size < pageNo * 10) {
Expand Down Expand Up @@ -258,7 +262,14 @@ class RecordRepository @Inject constructor(
private val coroutineScope: CoroutineScope
) {
fun loadAnyJoinedRecordByTravelIdAndPlaceAndDay(travelId: Int, day: String, place: String) =
joinRecordDao.loadStartJoinedRecordByTravelIdAndDayAndPlace(travelId, day, place, true, 1, 0)
joinRecordDao.loadStartJoinedRecordByTravelIdAndDayAndPlace(
travelId,
day,
place,
true,
1,
0
)

fun loadDefaultJoinedRecordByTravelId(travelId: Int, place: String) =
joinRecordDao.loadDefaultJoinedRecordByTravelId(travelId, place)
Expand Down Expand Up @@ -305,12 +316,15 @@ class RecordRepository @Inject constructor(
fun insertEachNewRecordImages(image: NewRecordImage) {
coroutineScope.launch { newRecordImageDao.insert(image) }
}

fun insertNewRecordImages(images: List<NewRecordImage>) {
coroutineScope.launch { newRecordImageDao.insert(*images.toTypedArray()) }
}

fun insertRecordImages(images: List<RecordImage>) {
coroutineScope.launch { recordImageDao.insert(*images.toTypedArray()) }
}

fun deleteNewRecordImages(images: List<NewRecordImage>) {
coroutineScope.launch { newRecordImageDao.delete(*images.toTypedArray()) }
}
Expand All @@ -323,6 +337,7 @@ class RecordRepository @Inject constructor(
}
}
}

fun deleteNewRecordImage(id: Int) {
coroutineScope.launch {
val data = newRecordImageDao.loadRecordImageById(id)
Expand Down
Loading