-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from 2 commits
ad24410
5979085
92ebd6e
7409a56
64a4e52
a1665b5
280998b
b2d5b05
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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>( | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ํน์ BaseActivity, BaseFragment ๋ผ๋ ๋ค์ด๋ฐ์ ์ด๋ ์ ๊ฐ์? BindingActivity์ ๋ญ๊ฐ ์ต์์น ์์๋ฐ.. ์ ๋ง ๊ทธ๋ฐ ๊ฑธ ์๋ ์์. ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ/๋ทฐ ๋ฐ์ธ๋ฉ์ ์ฐ๋ Activity์ ๊ทธ๋ ์ง ์์ Activity์ ๊ตฌ๋ถํ๊ธฐ ์ํ ๋ค์ด๋ฐ์ธ๊ฐ์? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๋ต ๊ตฌ๋ถํ๊ธฐ ์ํ ๋ค์ด๋ฐ์ธ๋ฐ ์ด๋ค๊ฐ์? |
||||||
@LayoutRes private val layoutRes: Int, | ||||||
) : AppCompatActivity() { | ||||||
protected lateinit var binding: T | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
์ด ๋ถ๋ถ by lazy๋ก ์ด๊ธฐํํด์ฃผ๋ฉด ์ ๋๋์?? ๊ถ๊ธ.. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์ด ๋ฐฉ๋ฒ์ด ๋ ๊น๋ํ๋ค์ ๋ณ๊ฒฝํ๊ฒ ์ต๋๋ค. |
||||||
private var snackBar: Snackbar? = null | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. activity์ showSnackBar๋ฅผ ๋ฃ๋ ๊ฑด ์ด๋จ๊น์ฉ There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๋ณ๊ฒฝํ๊ฒ ์ต๋๋ค! |
||
action: Snackbar.() -> Unit = {}, | ||
) { | ||
snackBar?.dismiss() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. show ์ ์ dismissํด์ฃผ๋ ๊ฑฐ ๊ผผ๊ผผํ๋ค์ ๐ There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๊ผผ๊ผผํ null์ฒ๋ฆฌ ์ข์ต๋๋ค! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์ฐํํ |
||
snackBar = null | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BaseActivity ๋์ BindingActivity๋ผ๋ ์ด๋ฆ์ ์ฑํํ์ ์ด์ ๊ฐ ์๋์ฉ
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Binding์ ์ฌ์ฉํ์ง ์๋ ์กํฐ๋นํฐ๋ค์ด ์์ด์ ๊ณ ๋ฏผํ๋ค๊ฐ BindingActivity๋ก ์์ฑํ์ต๋๋ค.