Skip to content

Commit

Permalink
[Feature] Change Fragment Factory logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Nanamare committed Jul 12, 2020
1 parent 2565baa commit 52f173b
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 51 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions app/src/main/AndroidManifest.xml
Expand Up @@ -10,8 +10,7 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="LockedOrientationActivity">
android:theme="@style/AppTheme">
<activity android:name="com.template.nanamare.presentation.activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
18 changes: 3 additions & 15 deletions app/src/main/java/com/template/nanamare/di/FragmentModule.kt
Expand Up @@ -3,7 +3,6 @@ package com.template.nanamare.di
import com.template.nanamare.presentation.dialog.VideoFragment
import com.template.nanamare.presentation.fragment.MovieCategoryFragment
import com.template.nanamare.presentation.fragment.MovieFragment
import com.template.nanamare.presentation.model.GenrePresentation
import org.koin.androidx.fragment.dsl.fragment
import org.koin.core.module.Module
import org.koin.dsl.module
Expand All @@ -13,18 +12,7 @@ import org.koin.dsl.module
* setupKoinFragmentFactory() in activity before super.onCreate(savedInstanceState)
*/
val fragmentModule: Module = module {
fragment(override = true) {
MovieFragment()
}
fragment(override = true) { (movies: List<GenrePresentation>) ->
MovieFragment(movies)
}
fragment(override = true) {
MovieCategoryFragment()
}
fragment(override = true) { (movie: GenrePresentation) ->
MovieCategoryFragment(movie)
}
fragment(override = true) { VideoFragment() }
fragment(override = true) { (liveVideoUrl: String) -> VideoFragment(liveVideoUrl) }
fragment { MovieFragment() }
fragment { MovieCategoryFragment() }
fragment { VideoFragment() }
}
@@ -1,13 +1,18 @@
package com.template.nanamare.ext


import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment

fun AppCompatActivity.replaceFragmentInActivity(fragment: Fragment, frameId: Int) {
fun AppCompatActivity.replaceFragmentInActivity(
fragmentClazz: Class<out Fragment>,
frameId: Int,
args: Bundle
) {
supportFragmentManager.transact {
replace(frameId, fragment)
replace(frameId, fragmentClazz, args, "$fragmentClazz")
}
}

Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/com/template/nanamare/ext/StringExt.kt
@@ -0,0 +1,6 @@
package com.template.nanamare.ext

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

inline fun <reified T> String.fromJson() = Gson().fromJson<T>(this, object : TypeToken<T>() {}.type)
Expand Up @@ -10,6 +10,7 @@ import com.template.nanamare.data.network.NetworkState
import com.template.nanamare.databinding.MainActivityBinding
import com.template.nanamare.domain.model.GenreModel
import com.template.nanamare.ext.replaceFragmentInActivity
import com.template.nanamare.ext.toJsonString
import com.template.nanamare.presentation.base.ui.BaseActivity
import com.template.nanamare.presentation.fragment.MovieFragment
import com.template.nanamare.presentation.mapper.GenrePresentationMapper
Expand Down Expand Up @@ -39,7 +40,9 @@ class MainActivity : BaseActivity<MainActivityBinding>(R.layout.main_activity) {
is NetworkState.Init -> hideLoadingPopup()
is NetworkState.Loading -> showLoadingPopup()
is NetworkState.Success<List<GenreModel>> ->
replaceFragmentInActivity(MovieFragment(it.item.map(GenrePresentationMapper::mapToPresentation)), R.id.flContent)
replaceFragmentInActivity(MovieFragment::class.java, R.id.flContent, Bundle().apply {
putString(KEY_GENE, it.item.map(GenrePresentationMapper::mapToPresentation).toJsonString())
})
is NetworkState.Error -> showToast(it.throwable.toString())
is NetworkState.ServerError -> showToast(it.toString())
}
Expand All @@ -65,4 +68,8 @@ class MainActivity : BaseActivity<MainActivityBinding>(R.layout.main_activity) {
)
}
}

companion object {
const val KEY_GENE = "KEY_GENE"
}
}
Expand Up @@ -6,18 +6,18 @@ import androidx.lifecycle.Observer
import com.template.nanamare.BR
import com.template.nanamare.BuildConfig.VIDEO_URL
import com.template.nanamare.R
import com.template.nanamare.presentation.base.ui.BaseActivity
import com.template.nanamare.presentation.base.ui.SimpleRecyclerView
import com.template.nanamare.presentation.model.ActorPresentation
import com.template.nanamare.data.network.NetworkState
import com.template.nanamare.databinding.ItemActorBinding
import com.template.nanamare.databinding.MovieInfoActivityBinding
import com.template.nanamare.data.network.NetworkState
import com.template.nanamare.domain.model.CreditModel
import com.template.nanamare.domain.model.VideoModel
import com.template.nanamare.presentation.anim.SimpleAnimation
import com.template.nanamare.presentation.base.ui.BaseActivity
import com.template.nanamare.presentation.base.ui.SimpleRecyclerView
import com.template.nanamare.presentation.dialog.VideoFragment
import com.template.nanamare.presentation.mapper.ActorPresentationMapper
import com.template.nanamare.presentation.mapper.ResultPresentationMapper
import com.template.nanamare.presentation.model.ActorPresentation
import com.template.nanamare.presentation.vm.MovieInfoViewModel
import org.koin.androidx.viewmodel.ext.android.viewModel
import java.util.*
Expand All @@ -28,7 +28,10 @@ class MovieInfoActivity : BaseActivity<MovieInfoActivityBinding>(R.layout.movie_

private val actorSimpleAdapter by lazy {
object :
SimpleRecyclerView.Adapter<ActorPresentation, ItemActorBinding>(R.layout.item_actor, BR.actor) {
SimpleRecyclerView.Adapter<ActorPresentation, ItemActorBinding>(
R.layout.item_actor,
BR.actor
) {
}
}

Expand Down Expand Up @@ -71,15 +74,28 @@ class MovieInfoActivity : BaseActivity<MovieInfoActivityBinding>(R.layout.movie_
}

private fun initActorAdapter(creditModel: CreditModel) {
movieInfoViewModel.liveActor.value = creditModel.cast.map(ActorPresentationMapper::mapToPresentation)
movieInfoViewModel.liveActor.value =
creditModel.cast.map(ActorPresentationMapper::mapToPresentation)
}

private fun getUrl(it: NetworkState.Success<VideoModel>) {
it.item.results.map(ResultPresentationMapper::mapToPresentation)
.asSequence()
.filter { "YouTube".toLowerCase(Locale.getDefault()) == it.site.toLowerCase(Locale.getDefault()) }
.reduce { acc, result -> if (acc.size > result.size) acc else result }.let {
VideoFragment(Uri.parse(VIDEO_URL).buildUpon().appendQueryParameter("v", it.key).build().toString()).show(supportFragmentManager, VideoFragment::class.simpleName)
VideoFragment().show(
supportFragmentManager,
VideoFragment::class.java,
Bundle().apply {
putString(
VideoFragment.KEY_VIDEO_URL,
Uri.parse(VIDEO_URL).buildUpon().appendQueryParameter("v", it.key)
.build()
.toString()
)
},
VideoFragment::class.simpleName
)
}
}

Expand Down
Expand Up @@ -10,16 +10,22 @@ import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.template.nanamare.ext.transact
import com.template.nanamare.presentation.base.navigator.BaseNavigator


abstract class BaseDialogFragment<B : ViewDataBinding>(@LayoutRes private val layoutId: Int)
: DialogFragment(), BaseNavigator {
abstract class BaseDialogFragment<B : ViewDataBinding>(@LayoutRes private val layoutId: Int) :
DialogFragment(), BaseNavigator {

lateinit var binding: B

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = DataBindingUtil.inflate(inflater, layoutId, container, false)
binding.lifecycleOwner = this
return binding.root
Expand Down Expand Up @@ -63,9 +69,14 @@ abstract class BaseDialogFragment<B : ViewDataBinding>(@LayoutRes private val la
}

override fun show(manager: FragmentManager, tag: String?) {
manager.beginTransaction().let {
it.add(this, tag)
it.commitAllowingStateLoss()
manager.transact {
add(this@BaseDialogFragment, tag)
}
}

fun show(manager: FragmentManager, clazz: Class<out Fragment>, args: Bundle, tag: String?) {
manager.transact {
add(clazz, args, tag)
}
}

Expand All @@ -80,7 +91,7 @@ abstract class BaseDialogFragment<B : ViewDataBinding>(@LayoutRes private val la
}

override fun errorHandling(errorCode: String) {
if(isAdded) {
if (isAdded) {
(activity as? BaseActivity<*>)?.errorHandling(errorCode)
}
}
Expand Down
Expand Up @@ -25,5 +25,4 @@ class BaseViewPager(fm: FragmentManager) : FragmentPagerAdapter(fm, BEHAVIOR_RES
return fragmentList[position].second
}


}
Expand Up @@ -15,18 +15,20 @@ import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory
import com.google.android.exoplayer2.util.Util
import com.template.nanamare.AppApplication
import com.template.nanamare.R
import com.template.nanamare.presentation.base.ui.BaseDialogFragment
import com.template.nanamare.databinding.VideoDialogBinding
import com.template.nanamare.presentation.base.ui.BaseDialogFragment
import com.template.nanamare.presentation.vm.VideoViewModel
import org.koin.androidx.viewmodel.ext.android.viewModel
import org.koin.core.parameter.parametersOf

@SuppressLint("StaticFieldLeak")
class VideoFragment(private val liveVideoUrl: String? = null) :
class VideoFragment :
BaseDialogFragment<VideoDialogBinding>(R.layout.video_dialog) {

private val videoUrl by lazy { arguments?.getString(KEY_VIDEO_URL) }

private val videoViewModel by viewModel<VideoViewModel> {
parametersOf(liveVideoUrl)
parametersOf(videoUrl)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Expand All @@ -45,11 +47,12 @@ class VideoFragment(private val liveVideoUrl: String? = null) :
val iTag = 22 // quality
val videoSource: MediaSource = ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(it[iTag].url))
binding.videoView.player = SimpleExoPlayer.Builder(requireContext()).build().apply {
playWhenReady = playWhenReady;
seekTo(currentWindowIndex, 0)
prepare(videoSource, false, false)
}
binding.videoView.player =
SimpleExoPlayer.Builder(requireContext()).build().apply {
playWhenReady = playWhenReady;
seekTo(currentWindowIndex, 0)
prepare(videoSource, false, false)
}
} ?: run {
showToast(R.string.empty_movie_url)
dismiss()
Expand All @@ -68,5 +71,8 @@ class VideoFragment(private val liveVideoUrl: String? = null) :
binding.videoView.player?.release()
}

companion object {
const val KEY_VIDEO_URL = "KEY_VIDEO_URL"
}

}
Expand Up @@ -22,12 +22,15 @@ import com.template.nanamare.utils.decoration.GridSpacingItemDecoration
import org.koin.androidx.viewmodel.ext.android.viewModel
import org.koin.core.parameter.parametersOf

class MovieCategoryFragment(private val genre: GenrePresentation? = null) :
class MovieCategoryFragment :
BaseFragment<MovieCategoryFragmentBinding>(R.layout.movie_category_fragment), BaseNavigator {

private val movieCategoryViewModel by viewModel<MovieCategoryViewModel> {
parametersOf(this)
}

private val genre by lazy { arguments?.getParcelable<GenrePresentation>(KEY_GENRE_PRESENTATION) }

private val column by lazy { resources.getInteger(R.integer.grid_column) }
private val space by lazy { resources.getDimension(R.dimen.grid_space).toInt() }

Expand Down Expand Up @@ -84,4 +87,13 @@ class MovieCategoryFragment(private val genre: GenrePresentation? = null) :
}
}

companion object {
const val KEY_GENRE_PRESENTATION = "KEY_GENRE_PRESENTATION"
fun getInstance(genre: GenrePresentation) = MovieCategoryFragment().apply {
arguments = Bundle().apply {
putParcelable(KEY_GENRE_PRESENTATION, genre)
}
}
}

}
Expand Up @@ -4,22 +4,23 @@ import android.os.Bundle
import android.view.View
import com.template.nanamare.R
import com.template.nanamare.databinding.MovieFragmentBinding
import com.template.nanamare.ext.fromJson
import com.template.nanamare.presentation.activity.MainActivity
import com.template.nanamare.presentation.base.ui.BaseFragment
import com.template.nanamare.presentation.base.ui.BaseViewPager
import com.template.nanamare.presentation.model.GenrePresentation

class MovieFragment(private val genres: List<GenrePresentation>? = null) :
class MovieFragment :
BaseFragment<MovieFragmentBinding>(R.layout.movie_fragment) {

private val baseViewPager by lazy { BaseViewPager(childFragmentManager) }

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.run {
genres?.let {
arguments?.getString(MainActivity.KEY_GENE)?.fromJson<List<GenrePresentation>>()?.let {
for (genre in it) {
baseViewPager.addFragment(MovieCategoryFragment(genre), genre.name)
baseViewPager.addFragment(MovieCategoryFragment.getInstance(genre), genre.name)
}
}
viewPager.adapter = baseViewPager
Expand Down
@@ -1,6 +1,34 @@
package com.template.nanamare.presentation.model

import android.os.Parcel
import android.os.Parcelable

data class GenrePresentation(
val id: Int,
val name: String
): BasePresentation
) : BasePresentation, Parcelable {
constructor(parcel: Parcel) : this(
parcel.readInt(),
parcel.readString() ?: ""
) {
}

override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeInt(id)
parcel.writeString(name)
}

override fun describeContents(): Int {
return 0
}

companion object CREATOR : Parcelable.Creator<GenrePresentation> {
override fun createFromParcel(parcel: Parcel): GenrePresentation {
return GenrePresentation(parcel)
}

override fun newArray(size: Int): Array<GenrePresentation?> {
return arrayOfNulls(size)
}
}
}
Expand Up @@ -3,8 +3,8 @@ package com.template.nanamare.presentation.vm
import androidx.lifecycle.MutableLiveData
import com.template.nanamare.presentation.base.ui.BaseViewModel

class VideoViewModel(private val liveVideoUrl: String) : BaseViewModel() {
class VideoViewModel(private val videoUrl: String) : BaseViewModel() {
var liveVideoPath = MutableLiveData<String>().apply {
value = liveVideoUrl
value = videoUrl
}
}

0 comments on commit 52f173b

Please sign in to comment.