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

refactor: BaseActivity, BaseFragment #220

Merged
merged 8 commits into from
Aug 2, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.woowacourse.ody.presentation.common.binding

import android.os.Bundle
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.google.android.material.snackbar.Snackbar
import com.woowacourse.ody.OdyApplication

abstract class BindingActivity<T : ViewDataBinding>(
Copy link
Contributor

Choose a reason for hiding this comment

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

BaseActivity ๋Œ€์‹  BindingActivity๋ผ๋Š” ์ด๋ฆ„์„ ์ฑ„ํƒํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ๋‚˜์šฉ

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Binding์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ์•กํ‹ฐ๋น„ํ‹ฐ๋“ค์ด ์žˆ์–ด์„œ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ BindingActivity๋กœ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.

Copy link
Contributor

Choose a reason for hiding this comment

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

ํ˜น์‹œ BaseActivity, BaseFragment ๋ผ๋Š” ๋„ค์ด๋ฐ์€ ์–ด๋– ์‹ ๊ฐ€์š”? BindingActivity์€ ๋ญ”๊ฐ€ ์ต์ˆ™์น˜ ์•Š์€๋ฐ.. ์ €๋งŒ ๊ทธ๋Ÿฐ ๊ฑธ ์ˆ˜๋„ ์žˆ์Œ.

๋ฐ์ดํ„ฐ ๋ฐ”์ธ๋”ฉ/๋ทฐ ๋ฐ”์ธ๋”ฉ์„ ์“ฐ๋Š” Activity์™€ ๊ทธ๋ ‡์ง€ ์•Š์€ Activity์™€ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•œ ๋„ค์ด๋ฐ์ธ๊ฐ€์š”?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

๋„ต ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•œ ๋„ค์ด๋ฐ์ธ๋ฐ ์–ด๋–ค๊ฐ€์š”?

@LayoutRes private val layoutRes: Int,
) : AppCompatActivity() {
protected lateinit var binding: T
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
protected lateinit var binding: T
protected val binding: T by lazy { DataBindingUtil.setContentView(this, layoutRes) }

์ด ๋ถ€๋ถ„ by lazy๋กœ ์ดˆ๊ธฐํ™”ํ•ด์ฃผ๋ฉด ์•ˆ ๋˜๋‚˜์š”?? ๊ถ๊ธˆ..

Copy link
Contributor Author

Choose a reason for hiding this comment

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

์ด ๋ฐฉ๋ฒ•์ด ๋” ๊น”๋”ํ•˜๋„ค์š” ๋ณ€๊ฒฝํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

private var snackBar: Snackbar? = null
Copy link
Contributor

Choose a reason for hiding this comment

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

activity์— showSnackBar๋ฅผ ๋„ฃ๋Š” ๊ฑด ์–ด๋–จ๊นŒ์šฉ

Copy link
Contributor Author

Choose a reason for hiding this comment

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

์ถ”๊ฐ€ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!

private val application by lazy { applicationContext as OdyApplication }

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, layoutRes)
binding.lifecycleOwner = this
}

override fun onDestroy() {
super.onDestroy()
snackBar = null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.woowacourse.ody.presentation.common.binding

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
import com.google.android.material.snackbar.Snackbar

abstract class BindingFragment<T : ViewDataBinding>(
@LayoutRes private val layoutRes: Int,
) : Fragment() {
private var _binding: T? = null
protected val binding: T
get() = requireNotNull(_binding)
private var snackBar: Snackbar? = null

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

fun showSnackBar(
message: String,
Copy link
Contributor

Choose a reason for hiding this comment

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

@StringRes messageId: Int๋Š” ์–ด๋– ์‹ ๊ฐ€์š”?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

๋ณ€๊ฒฝํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!

action: Snackbar.() -> Unit = {},
) {
snackBar?.dismiss()
Copy link
Contributor

Choose a reason for hiding this comment

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

show ์ „์— dismissํ•ด์ฃผ๋Š” ๊ฑฐ ๊ผผ๊ผผํ•˜๋„ค์š” ๐Ÿ‘

Copy link
Contributor Author

Choose a reason for hiding this comment

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

๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!

snackBar = Snackbar.make(binding.root, message, Snackbar.LENGTH_SHORT).apply { action() }
snackBar?.show()
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
Copy link
Contributor

Choose a reason for hiding this comment

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

๊ผผ๊ผผํ•œ null์ฒ˜๋ฆฌ ์ข‹์Šต๋‹ˆ๋‹ค!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

์šฐํ•˜ํ•˜

snackBar = null
}
}