-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ce3311
commit 320472f
Showing
5 changed files
with
316 additions
and
0 deletions.
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
app/src/main/java/com/joosung/pickme/ui/image/ImagePagerFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package com.joosung.pickme.ui.image | ||
|
||
import android.content.Intent | ||
import android.databinding.DataBindingUtil | ||
import android.os.Bundle | ||
import android.support.v4.view.ViewPager | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import com.jakewharton.rxbinding2.support.v4.view.RxViewPager | ||
import com.jakewharton.rxbinding2.view.RxView | ||
import com.joosung.pickme.common.BaseFragment | ||
import com.joosung.pickme.common.ErrorCatchable | ||
import com.joosung.pickme.extensions.observe | ||
import com.joosung.pickme.extensions.withViewModel | ||
import com.joosung.pickme.ui.home.HomeViewModel | ||
import com.joosung.library.rx.RxUtils | ||
import com.joosung.pickme.R | ||
import com.joosung.pickme.common.adapter.ViewPagerAdapter | ||
import com.joosung.pickme.databinding.FragmentImagePagerBinding | ||
import io.reactivex.android.schedulers.AndroidSchedulers | ||
import io.reactivex.rxkotlin.addTo | ||
import org.koin.android.viewmodel.ext.android.sharedViewModel | ||
import org.koin.android.viewmodel.ext.android.viewModel | ||
import org.koin.core.parameter.parametersOf | ||
|
||
class ImagePagerFragment() : BaseFragment(), ErrorCatchable { | ||
private val homeViewModel: HomeViewModel by sharedViewModel() | ||
private val index: Int by lazy { arguments?.getInt(kIndex) ?: 0 } | ||
// private val viewModel: ImagePagerViewModel by viewModel { parametersOf(index, homeViewModel.getMediaSource()) } | ||
private var binding: FragmentImagePagerBinding? = null | ||
|
||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { | ||
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_image_pager, container, false) | ||
return binding?.root | ||
} | ||
|
||
override fun onActivityCreated(savedInstanceState: Bundle?) { | ||
super.onActivityCreated(savedInstanceState) | ||
|
||
// binding?.viewModel = withViewModel({ viewModel }) { | ||
// observe(getApiErrorEvent()) { error -> error?.also { handleError(activity, it) } } | ||
// } | ||
|
||
// homeViewModel.monitor() | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
|
||
// val adapter = ImagePagerAdapter(childFragmentManager) | ||
// binding?.also { binding -> | ||
// binding.viewPager.adapter = adapter | ||
// | ||
// viewModel.dataSource | ||
// .observeOn(AndroidSchedulers.mainThread()) | ||
// .doAfterNext { binding.viewPager.setCurrentItem(viewModel.expectedPosition.value, false) } | ||
// .subscribe(adapter.rx()) | ||
// .addTo(disposables) | ||
// } | ||
// | ||
// initialize(adapter) | ||
// | ||
// viewModel.onViewCreated() | ||
} | ||
|
||
private fun initialize(adapter: ViewPagerAdapter) { | ||
// binding?.also { binding -> | ||
// | ||
// RxUtils.combineLatest(viewModel.viewCreated.asObservable(), viewModel.expectedPosition.asObservable()) { c, p -> Pair(c, p) } | ||
// .filter { it.first } | ||
// .map { it.second } | ||
// .observeOn(AndroidSchedulers.mainThread()) | ||
// .subscribe { binding.viewPager.currentItem = it } | ||
// .addTo(disposables) | ||
// | ||
// var currentIndex = viewModel.expectedPosition.value | ||
// var isDraggingAtaLast = false | ||
// | ||
// RxViewPager.pageSelections(binding.viewPager) | ||
// .observeOn(AndroidSchedulers.mainThread()) | ||
// .subscribe { currentIndex = it } | ||
// .addTo(disposables) | ||
// | ||
// RxViewPager.pageScrollStateChanges(binding.viewPager) | ||
// .observeOn(AndroidSchedulers.mainThread()) | ||
// .subscribe { | ||
// val lastIndex = adapter.count - 1 | ||
// viewModel.isDragging.value = it == ViewPager.SCROLL_STATE_DRAGGING | ||
// | ||
// if (currentIndex == lastIndex && it == ViewPager.SCROLL_STATE_DRAGGING) { | ||
// isDraggingAtaLast = true | ||
// } else if (isDraggingAtaLast && it == ViewPager.SCROLL_STATE_IDLE) { | ||
// viewModel.loadNext() | ||
// } else { | ||
// isDraggingAtaLast = false | ||
// } | ||
// } | ||
// .addTo(disposables) | ||
// | ||
// binding.close.bringToFront() | ||
// RxView.clicks(binding.close) | ||
// .observeOn(AndroidSchedulers.mainThread()) | ||
// .subscribe { close(currentIndex) } | ||
// .addTo(disposables) | ||
// } | ||
} | ||
|
||
private fun close(index: Int) { | ||
baseFragment.setFragmentResult(Intent().putExtra(kLatestIndex, index)) | ||
baseFragment.popFragment() | ||
} | ||
|
||
override fun onDestroyView() { | ||
super.onDestroyView() | ||
disposables.clear() | ||
} | ||
|
||
companion object { | ||
const val kIndex = "index" | ||
const val kLatestIndex = "kLatestIndex" | ||
const val codeLatestIndex = 1004 | ||
|
||
fun newInstance(index: Int): ImagePagerFragment { | ||
val fragment = ImagePagerFragment() | ||
val arg = Bundle() | ||
arg.putInt(kIndex, index) | ||
fragment.arguments = arg | ||
|
||
return fragment | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
app/src/main/java/com/joosung/pickme/ui/image/ImagePagerViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.joosung.pickme.ui.image | ||
|
||
import com.joosung.pickme.common.FragmentBundle | ||
import com.joosung.pickme.http.api.ImageId | ||
import com.joosung.library.rx.RxViewModel | ||
import com.joosung.library.rx.Variable | ||
import com.joosung.library.vm.SingleLiveEvent | ||
import com.joosung.pickme.ui.search.MediaServerInterface | ||
import io.reactivex.android.schedulers.AndroidSchedulers | ||
import io.reactivex.rxkotlin.subscribeBy | ||
|
||
class ImagePagerViewModel( | ||
index: Int, | ||
private val imageSource: Variable<ArrayList<ImageId>>, | ||
private val service: MediaServerInterface | ||
) : RxViewModel() { | ||
|
||
val dataSource = imageSource.asObservable().map { list -> list.map { FragmentBundle.Image(it) } } | ||
val viewCreated = Variable(false) | ||
val expectedPosition = Variable(index) | ||
val isDragging = Variable(false) | ||
val isLoading = Variable(false) | ||
|
||
private val apiErrorEvent = SingleLiveEvent<String>() | ||
fun getApiErrorEvent() = apiErrorEvent | ||
|
||
fun onViewCreated() { | ||
viewCreated.value = true | ||
} | ||
|
||
fun loadNext() { | ||
// launch { | ||
// isLoading.value = true | ||
// service.loadImage() | ||
// .observeOn(AndroidSchedulers.mainThread()) | ||
// .subscribeBy( | ||
// onSuccess = { imageIdList -> | ||
// if (imageIdList.isNotEmpty()) { | ||
// expectedPosition.value = imageSource.value.size | ||
// } | ||
// val list = arrayListOf<ImageId>() | ||
// list.addAll(imageSource.value) | ||
// list.addAll(imageIdList) | ||
// imageSource.value = list | ||
// isLoading.value = false | ||
// }, | ||
// onError = { error -> | ||
// isLoading.value = false | ||
// apiErrorEvent.value = error.message | ||
// } | ||
// ) | ||
// } | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
app/src/main/java/com/joosung/pickme/ui/image/item/ImageFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.joosung.pickme.ui.image.item | ||
|
||
import android.databinding.DataBindingUtil | ||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import com.joosung.pickme.common.BaseFragment | ||
import com.joosung.pickme.http.api.ImageId | ||
import com.joosung.library.rx.RxViewModel | ||
import com.joosung.pickme.R | ||
import com.joosung.pickme.common.MediaRepository | ||
import com.joosung.pickme.databinding.FragmentImageBinding | ||
import com.joosung.pickme.http.model.AppSharedMedia | ||
import org.koin.android.viewmodel.ext.android.viewModel | ||
import org.koin.core.parameter.parametersOf | ||
|
||
class ImageFragment : BaseFragment() { | ||
private var binding: FragmentImageBinding? = null | ||
private val id: ImageId by lazy { arguments?.getString(kImageId) ?: "" } | ||
private val viewModel: ImageViewModel by viewModel { parametersOf(id) } | ||
|
||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { | ||
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_image, container, false) | ||
return binding?.root | ||
} | ||
|
||
override fun onActivityCreated(savedInstanceState: Bundle?) { | ||
super.onActivityCreated(savedInstanceState) | ||
|
||
binding?.image = viewModel.image | ||
} | ||
|
||
companion object { | ||
const val kImageId = "imageId" | ||
|
||
fun newInstance(id: ImageId): ImageFragment { | ||
val fragment = ImageFragment() | ||
|
||
val argument = Bundle() | ||
argument.putString(kImageId, id) | ||
fragment.arguments = argument | ||
|
||
return fragment | ||
} | ||
} | ||
} | ||
|
||
class ImageViewModel(id: ImageId, repo: MediaRepository) : RxViewModel() { | ||
var image: AppSharedMedia? = repo.getMedia(id) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<layout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto"> | ||
|
||
<data> | ||
|
||
<variable | ||
name="image" | ||
type="com.joosung.pickme.http.model.AppSharedMedia"/> | ||
</data> | ||
|
||
<android.support.constraint.ConstraintLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:background="@android:color/background_dark"> | ||
|
||
<com.github.chrisbanes.photoview.PhotoView | ||
android:id="@+id/photoView" | ||
android:layout_width="0dp" | ||
android:layout_height="0dp" | ||
android:layout_centerVertical="true" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintLeft_toLeftOf="parent" | ||
app:layout_constraintRight_toRightOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" | ||
app:fullImageUrl="@{image.url}" | ||
/> | ||
</android.support.constraint.ConstraintLayout> | ||
</layout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<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"> | ||
|
||
<data> | ||
<import type="android.view.View" /> | ||
<variable | ||
name="viewModel" | ||
type="com.joosung.pickme.ui.image.ImagePagerViewModel"/> | ||
</data> | ||
|
||
<android.support.constraint.ConstraintLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" > | ||
|
||
<android.support.v7.widget.AppCompatButton | ||
android:id="@+id/close" | ||
style="?android:attr/borderlessButtonStyle" | ||
android:layout_width="35dp" | ||
android:layout_height="42dp" | ||
android:layout_margin="5dp" | ||
android:background="@drawable/icon_close" | ||
app:layout_constraintRight_toRightOf="parent" | ||
android:visibility='@{safeUnbox(viewModel.isDragging) ? View.GONE : View.VISIBLE , default="visible"}' | ||
tools:visibility="visible"/> | ||
|
||
<com.joosung.pickme.common.view.HackyViewPager | ||
android:id="@+id/view_pager" | ||
android:layout_width="0dp" | ||
android:layout_height="0dp" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintLeft_toLeftOf="parent" | ||
app:layout_constraintRight_toRightOf="parent" | ||
app:layout_constraintTop_toTopOf="parent"/> | ||
|
||
<ProgressBar | ||
android:id="@+id/progress_bar" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
android:visibility='@{safeUnbox(viewModel.isLoading) ? View.VISIBLE : View.GONE , default="gone"}' | ||
app:layout_constraintLeft_toLeftOf="parent" | ||
app:layout_constraintRight_toRightOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" | ||
tools:visibility="visible"/> | ||
|
||
</android.support.constraint.ConstraintLayout> | ||
</layout> |