168 lines
6.0 KiB
Kotlin
168 lines
6.0 KiB
Kotlin
package pl.edu.amu.wmi.socialaggregator.activity
|
|
|
|
import android.Manifest
|
|
import android.annotation.SuppressLint
|
|
import android.content.Context
|
|
import android.content.Intent
|
|
import android.content.pm.PackageManager
|
|
import android.graphics.BitmapFactory
|
|
import android.net.Uri
|
|
import android.os.Bundle
|
|
import android.provider.MediaStore
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
import androidx.core.app.ActivityCompat
|
|
import androidx.core.content.ContextCompat
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
|
import com.google.android.material.snackbar.Snackbar
|
|
import com.jakewharton.rxbinding2.view.RxView
|
|
import io.reactivex.Observable
|
|
import io.reactivex.functions.BiFunction
|
|
import io.reactivex.schedulers.Schedulers
|
|
import io.reactivex.subjects.PublishSubject
|
|
import kotlinx.android.synthetic.main.activity_new_post.*
|
|
import kotlinx.android.synthetic.main.content_new_post.*
|
|
import pl.edu.amu.wmi.socialaggregator.R
|
|
import pl.edu.amu.wmi.socialaggregator.socialplatforms.SocialPlatform
|
|
import pl.edu.amu.wmi.socialaggregator.utils.SocialPlatformsManager
|
|
import pl.edu.amu.wmi.socialaggregator.viewholders.SocialWithToggleRecycler
|
|
|
|
|
|
class NewPostActivity : AppCompatActivity() {
|
|
|
|
var file: String? = null
|
|
|
|
val postedSubject = PublishSubject.create<Any>()
|
|
|
|
val resumedSubject = PublishSubject.create<Any>()
|
|
|
|
@SuppressLint("CheckResult")
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
setContentView(R.layout.activity_new_post)
|
|
setSupportActionBar(toolbar)
|
|
|
|
publishPost.setOnClickListener { view ->
|
|
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
|
|
.setAction("Action", null).show()
|
|
}
|
|
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
|
|
|
val loggedInSocials = SocialPlatformsManager.getLoggedIn(this)
|
|
|
|
val availablesRecycler = SocialWithToggleRecycler(loggedInSocials)
|
|
availableSocials.apply {
|
|
layoutManager = LinearLayoutManager(this@NewPostActivity)
|
|
adapter = availablesRecycler
|
|
}
|
|
|
|
RxView.clicks(publishPost)
|
|
.filter { file != null }
|
|
.doOnNext {
|
|
val sdk = android.os.Build.VERSION.SDK_INT
|
|
if (sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
|
|
val clipboard =
|
|
getSystemService(Context.CLIPBOARD_SERVICE) as android.text.ClipboardManager
|
|
clipboard.text = postText.text?.toString() ?: ""
|
|
} else {
|
|
val clipboard =
|
|
getSystemService(Context.CLIPBOARD_SERVICE) as android.content.ClipboardManager
|
|
val clip = android.content.ClipData.newPlainText(
|
|
"text label",
|
|
postText.text?.toString() ?: ""
|
|
)
|
|
clipboard.primaryClip = clip
|
|
}
|
|
}
|
|
.observeOn(Schedulers.io())
|
|
.doOnNext {
|
|
resumedSubject.zipWith(
|
|
Observable.fromIterable(availablesRecycler.chips.entries
|
|
.filter { (_, chip) -> chip.isChecked }
|
|
.map { it.key }),
|
|
BiFunction<Any, SocialPlatform, SocialPlatform> { _, t2 -> t2 }
|
|
)
|
|
.doOnNext {
|
|
val bitmap = BitmapFactory.decodeFile(file!!)
|
|
it.addPost(
|
|
postedSubject, this,
|
|
postText.text?.toString() ?: "",
|
|
listOf(bitmap),
|
|
listOf(file!!)
|
|
)
|
|
}
|
|
.take(availablesRecycler.chips.count { it.value.isChecked }.toLong())
|
|
.toList()
|
|
.subscribe { _ ->
|
|
finish()
|
|
}
|
|
|
|
}
|
|
.subscribe {
|
|
resumedSubject.onNext(Any())
|
|
}
|
|
|
|
RxView.clicks(imageView2)
|
|
.subscribe {
|
|
val getIntent = Intent(Intent.ACTION_GET_CONTENT)
|
|
getIntent.type = "image/*"
|
|
|
|
val pickIntent = Intent(
|
|
Intent.ACTION_PICK,
|
|
MediaStore.Images.Media.EXTERNAL_CONTENT_URI
|
|
)
|
|
pickIntent.type = "image/*"
|
|
|
|
val chooserIntent = Intent.createChooser(getIntent, "Select Image")
|
|
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, arrayOf(pickIntent))
|
|
|
|
startActivityForResult(chooserIntent, PICK_IMAGE_REQUEST_CODE)
|
|
}
|
|
|
|
|
|
if (canAccessGallery()) {
|
|
ActivityCompat.requestPermissions(
|
|
this,
|
|
arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), 0
|
|
)
|
|
}
|
|
}
|
|
|
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
|
SocialPlatformsManager.getAll()
|
|
.forEach { it.onActivityResult(requestCode, resultCode, data) }
|
|
super.onActivityResult(requestCode, resultCode, data)
|
|
|
|
if (requestCode == PICK_IMAGE_REQUEST_CODE) {
|
|
data?.data?.let { uri ->
|
|
file = getPath(uri)
|
|
imageView2.setImageURI(uri)
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
fun getPath(uri: Uri): String? {
|
|
val projection = arrayOf(MediaStore.Images.Media.DATA)
|
|
val cursor = contentResolver.query(uri, projection, null, null, null) ?: return null
|
|
val column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
|
|
cursor.moveToFirst()
|
|
val s = cursor.getString(column_index)
|
|
cursor.close()
|
|
return s
|
|
}
|
|
|
|
override fun onResume() {
|
|
super.onResume()
|
|
resumedSubject.onNext(Any())
|
|
}
|
|
|
|
private fun canAccessGallery() =
|
|
(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
|
|
!= PackageManager.PERMISSION_GRANTED)
|
|
|
|
companion object {
|
|
const val PICK_IMAGE_REQUEST_CODE = 69
|
|
}
|
|
|
|
}
|