Pixelcat-App-Android/app/src/main/kotlin/at/connyduck/pixelcat/components/compose/ComposeActivity.kt

161 lines
5.7 KiB
Kotlin
Raw Normal View History

2020-06-17 19:07:47 +02:00
/*
* Copyright (C) 2020 Conny Duck
*
* This file is part of Pixelcat.
*
* Pixelcat is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pixelcat is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2020-06-12 15:44:45 +02:00
package at.connyduck.pixelcat.components.compose
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.lifecycle.Observer
2020-06-17 14:48:18 +02:00
import androidx.lifecycle.lifecycleScope
2020-06-12 15:44:45 +02:00
import androidx.recyclerview.widget.LinearLayoutManager
import at.connyduck.pixelcat.R
import at.connyduck.pixelcat.components.general.BaseActivity
import at.connyduck.pixelcat.dagger.ViewModelFactory
import at.connyduck.pixelcat.databinding.ActivityComposeBinding
import at.connyduck.pixelcat.util.viewBinding
2020-06-17 14:48:18 +02:00
import com.fxn.pix.Options
2020-06-12 15:44:45 +02:00
import com.fxn.pix.Pix
2020-06-17 14:48:18 +02:00
import com.fxn.utility.ImageQuality
2020-06-12 15:44:45 +02:00
import com.google.android.material.bottomsheet.BottomSheetBehavior
2020-06-17 14:48:18 +02:00
import kotlinx.coroutines.launch
2020-06-12 15:44:45 +02:00
import javax.inject.Inject
2020-06-17 14:48:18 +02:00
class ComposeActivity : BaseActivity(), OnImageActionClickListener {
2020-06-12 15:44:45 +02:00
@Inject
lateinit var viewModelFactory: ViewModelFactory
private val viewModel: ComposeViewModel by viewModels { viewModelFactory }
private val binding by viewBinding(ActivityComposeBinding::inflate)
2020-06-17 14:48:18 +02:00
private val adapter = ComposeImageAdapter(this)
2020-06-12 15:44:45 +02:00
private lateinit var visibilityBottomSheet: BottomSheetBehavior<*>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
binding.root.setOnApplyWindowInsetsListener { _, insets ->
val top = insets.systemWindowInsetTop
val toolbarParams = binding.composeAppBar.layoutParams as CoordinatorLayout.LayoutParams
toolbarParams.topMargin = top
insets.consumeSystemWindowInsets()
}
2020-06-17 14:48:18 +02:00
if (viewModel.imageLiveData.value.isNullOrEmpty()) {
2020-06-12 15:44:45 +02:00
viewModel.addImage(intent.getStringExtra(EXTRA_MEDIA_URI)!!)
}
2020-06-17 14:48:18 +02:00
binding.composeToolBar.setNavigationOnClickListener {
onBackPressed()
}
2020-06-12 15:44:45 +02:00
binding.composeImages.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
binding.composeImages.adapter = adapter
visibilityBottomSheet = BottomSheetBehavior.from(binding.composeVisibilityBottomSheet)
2020-06-17 14:48:18 +02:00
visibilityBottomSheet.state = BottomSheetBehavior.STATE_HIDDEN
2020-06-12 15:44:45 +02:00
binding.composeShareButton.setOnClickListener {
2020-06-17 14:48:18 +02:00
lifecycleScope.launch {
viewModel.sendStatus(
caption = binding.composeCaptionInput.text?.toString().orEmpty(),
sensitive = binding.composeNsfwSwitch.isChecked
)
finish()
}
2020-06-12 15:44:45 +02:00
}
binding.composeVisibilityButton.setOnClickListener {
visibilityBottomSheet.state = BottomSheetBehavior.STATE_EXPANDED
}
binding.composeVisibilityPublic.setOnClickListener {
changeVisibility(VISIBILITY.PUBLIC)
}
binding.composeVisibilityUnlisted.setOnClickListener {
changeVisibility(VISIBILITY.UNLISTED)
}
binding.composeVisibilityFollowers.setOnClickListener {
changeVisibility(VISIBILITY.FOLLOWERS_ONLY)
}
2020-06-17 14:48:18 +02:00
viewModel.imageLiveData.observe(
2020-06-12 19:58:15 +02:00
this,
Observer {
adapter.submitList(it)
2020-06-12 15:44:45 +02:00
}
2020-06-12 19:58:15 +02:00
)
viewModel.visibility.observe(
this,
Observer {
val visibilityString = when (it) {
VISIBILITY.PUBLIC -> R.string.compose_visibility_public
VISIBILITY.UNLISTED -> R.string.compose_visibility_unlisted
VISIBILITY.FOLLOWERS_ONLY -> R.string.compose_visibility_followers_only
}
binding.composeVisibilityButton.text = getString(R.string.compose_visibility, getString(visibilityString))
}
)
2020-06-12 15:44:45 +02:00
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CODE_PICK_MEDIA) {
val returnValue =
data?.getStringArrayListExtra(Pix.IMAGE_RESULTS)
viewModel.addImage(returnValue?.first()!!)
}
}
private fun changeVisibility(visibility: VISIBILITY) {
viewModel.setVisibility(visibility)
2020-06-17 14:48:18 +02:00
visibilityBottomSheet.state = BottomSheetBehavior.STATE_HIDDEN
}
override fun onAddImage() {
val options = Options.init()
.setRequestCode(REQUEST_CODE_PICK_MEDIA)
.setImageQuality(ImageQuality.HIGH)
.setScreenOrientation(Options.SCREEN_ORIENTATION_PORTRAIT)
Pix.start(this, options)
2020-06-12 15:44:45 +02:00
}
companion object {
private const val REQUEST_CODE_PICK_MEDIA = 123
private const val EXTRA_MEDIA_URI = "MEDIA_URI"
fun newIntent(context: Context, mediaUri: String): Intent {
return Intent(context, ComposeActivity::class.java).apply {
putExtra(EXTRA_MEDIA_URI, mediaUri)
}
}
}
2020-06-12 19:58:15 +02:00
}