Skip to content

Commit

Permalink
Merge pull request #54 from horaciocome1/bug-fixes-and-improvements-p…
Browse files Browse the repository at this point in the history
…art-IX

Bug fixes and improvements part ix
  • Loading branch information
horaciocome1 authored Aug 16, 2019
2 parents 1dec3a6 + 7b854f1 commit 5bd38ab
Show file tree
Hide file tree
Showing 16 changed files with 216 additions and 185 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ android {
applicationId "io.github.horaciocome1.reaque"
minSdkVersion 16
targetSdkVersion 29
versionCode 15
versionName "1.4.2-rc-25"
versionCode 16
versionName "1.4.2-rc-35"
vectorDrawables.useSupportLibrary = true
multiDexEnabled true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ data class Post(var id: String) {
field = value
}
var score: Float = 1f - (1f / timestamp.seconds)
var rating = "0"
var rating = 0f

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import io.github.horaciocome1.reaque.data.posts.Post
import io.github.horaciocome1.reaque.databinding.FragmentSetRatingBinding
import io.github.horaciocome1.reaque.util.InjectorUtils
import kotlinx.android.synthetic.main.fragment_set_rating.*
import kotlin.math.roundToInt

class SetRatingFragment : Fragment() {

Expand All @@ -20,29 +21,43 @@ class SetRatingFragment : Fragment() {
ViewModelProviders.of(this, factory)[PostsViewModel::class.java]
}

private var rating = 0

private var defaultRating = 0

private var post = Post("")

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
binding = FragmentSetRatingBinding.inflate(inflater, container, false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.viewModel = viewModel
toolbar?.setNavigationOnClickListener { viewModel.navigateUp(it) }
toolbar.setNavigationOnClickListener {
viewModel.navigateUp(it)
}
rating_bar.setOnRatingBarChangeListener { _, rating, fromUser ->
done_button.isEnabled = if (rating != 0f && rating.roundToInt() != defaultRating
&& post.id.isNotBlank() && fromUser
) {
this.rating = rating.roundToInt()
true
} else
false
}
done_button.setOnClickListener {
viewModel.setRating(it, post, rating)
}
}

override fun onStart() {
super.onStart()
arguments?.let { bundle ->
val args = SetRatingFragmentArgs.fromBundle(bundle)
binding.post = Post(args.postId)
when (args.rating) {
1 -> textView1.isEnabled = false
2 -> textView2.isEnabled = false
3 -> textView3.isEnabled = false
4 -> textView4.isEnabled = false
5 -> textView5.isEnabled = false
}
this.post = Post(args.postId)
defaultRating = args.rating
rating_bar.rating = args.rating.toFloat()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.github.horaciocome1.reaque.ui.posts.create

import android.Manifest
import android.app.Activity
import android.content.Context.MODE_PRIVATE
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
Expand Down Expand Up @@ -48,14 +49,14 @@ class CreatePostFragment : Fragment() {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
loadDraft()
binding.viewmodel = viewModel
select_pic_from_gallery_button.setOnClickListener {

if (ContextCompat.checkSelfPermission(
activity as MainActivity,
Manifest.permission.READ_EXTERNAL_STORAGE
) == PackageManager.PERMISSION_GRANTED
val permission = ContextCompat.checkSelfPermission(
activity as MainActivity,
Manifest.permission.READ_EXTERNAL_STORAGE
)
if (permission == PackageManager.PERMISSION_GRANTED)
pickImageFromGallery()
else
requestStoragePermission()
Expand All @@ -79,28 +80,32 @@ class CreatePostFragment : Fragment() {
viewModel.post.topic = it[position]
binding.viewmodel = viewModel
create_button.isEnabled = viewModel.isPostReady
saveDraft()
}
}
}
select_topic_button.setOnClickListener {
select_topic_button?.setOnClickListener {
selectTopicBehavior.state = BottomSheetBehavior.STATE_HALF_EXPANDED
}
select_pic_button.setOnClickListener {
selectPicBehavior.state = BottomSheetBehavior.STATE_HALF_EXPANDED
}
toolbar?.setNavigationOnClickListener {
viewModel.navigateUp(it)
saveDraft()
}
create_button.setOnClickListener { binding.viewmodel = viewModel.create(it) }
create_button?.setOnClickListener { binding.viewmodel = viewModel.create(it) }
}

override fun onStart() {
super.onStart()
viewModel.title.observe(this, Observer {
saveDraft()
viewModel.post.title = it
create_button.isEnabled = viewModel.isPostReady
})
viewModel.message.observe(this, Observer {
saveDraft()
viewModel.post.message = it
create_button.isEnabled = viewModel.isPostReady
})
Expand All @@ -122,6 +127,17 @@ class CreatePostFragment : Fragment() {
}
}

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == Constants.STORAGE_PERMISSION_CODE) {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)
pickImageFromGallery()
else
Toast.makeText(context, R.string.permission_denied, Toast.LENGTH_LONG).show()
}
}


private fun pickImageFromGallery() {
val intent = Intent(Intent.ACTION_PICK).apply {
type = "image/*"
Expand All @@ -134,12 +150,12 @@ class CreatePostFragment : Fragment() {
}

private fun requestStoragePermission() {
if (activity is MainActivity)
if (ActivityCompat.shouldShowRequestPermissionRationale(
activity as MainActivity,
Manifest.permission.READ_EXTERNAL_STORAGE
)
) {
if (activity is MainActivity) {
val permission = ActivityCompat.shouldShowRequestPermissionRationale(
activity as MainActivity,
Manifest.permission.READ_EXTERNAL_STORAGE
)
if (permission) {
AlertDialog.Builder(activity as MainActivity)
.setTitle(resources.getString(R.string.permission_needed))
.setMessage(resources.getString(R.string.permission_needed_explanation))
Expand All @@ -150,7 +166,9 @@ class CreatePostFragment : Fragment() {
Constants.STORAGE_PERMISSION_CODE
)
}
.setNegativeButton(resources.getString(R.string.reject)) { dialog, _ -> dialog.dismiss() }
.setNegativeButton(resources.getString(R.string.reject)) { dialog, _ ->
dialog.dismiss()
}
.create()
.show()
} else
Expand All @@ -159,15 +177,46 @@ class CreatePostFragment : Fragment() {
arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE),
Constants.STORAGE_PERMISSION_CODE
)
}
}

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == Constants.STORAGE_PERMISSION_CODE) {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)
pickImageFromGallery()
else
Toast.makeText(context, R.string.permission_denied, Toast.LENGTH_LONG).show()
private fun saveDraft() {
activity?.let {
val postTitle = viewModel.post.title
val postMessage = viewModel.post.message
val topicId = viewModel.post.topic.id
val topicTitle = viewModel.post.topic.title
val sharedPreferences = it.getSharedPreferences(Constants.SharedPreferences.NAME, MODE_PRIVATE)
sharedPreferences.edit().apply {
putString(Constants.SharedPreferences.POST_TITLE, postTitle)
putString(Constants.SharedPreferences.POST_MESSAGE, postMessage)
putString(Constants.SharedPreferences.TOPIC_ID, topicId)
putString(Constants.SharedPreferences.TOPIC_TITLE, topicTitle)
apply()
}
}
}

private fun loadDraft() {
activity?.let { activity ->
val sharedPreferences = activity.getSharedPreferences(Constants.SharedPreferences.NAME, MODE_PRIVATE)
sharedPreferences.getString(Constants.SharedPreferences.TOPIC_ID, "")?.let {
if (it.isNotBlank())
viewModel.post.topic.id = it
}
sharedPreferences.getString(Constants.SharedPreferences.TOPIC_TITLE, "")?.let {
if (it.isNotBlank())
viewModel.post.topic.title = it
}
sharedPreferences.getString(Constants.SharedPreferences.POST_TITLE, "")?.let {
if (it.isNotBlank())
viewModel.title.value = it
}
sharedPreferences.getString(Constants.SharedPreferences.POST_MESSAGE, "")?.let {
if (it.isNotBlank())
viewModel.message.value = it
}
}
}

}
10 changes: 10 additions & 0 deletions app/src/main/java/io/github/horaciocome1/reaque/util/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,14 @@ object Constants {
const val USER_ID = "USER_ID"
const val MAIN_ACTIVITY = "MainActivity"

object SharedPreferences {

const val NAME = "SharedPreferencesPostDraft"
const val POST_TITLE = "SharedPreferencesPostDraftPostTitle"
const val POST_MESSAGE = "SharedPreferencesPostDraftPostMessage"
const val TOPIC_ID = "SharedPreferencesPostDraftTopicId"
const val TOPIC_TITLE = "SharedPreferencesPostDraftTopicTitle"

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ val DocumentSnapshot.post: Post
name = this@post["user.name"].toString()
pic = this@post["user.pic"].toString()
}
val score = this@post["score"]
if (score != null)
this.score = score.toString().toFloat()
rating = this@post["rating"].toString()
if (contains("score"))
this.score = this@post["score"].toString().toFloat()
if (contains("rating"))
rating = this@post["rating"].toString().toFloat()
}
38 changes: 19 additions & 19 deletions app/src/main/res/layout-v21/fragment_read_post.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@
<com.google.android.material.button.MaterialButton
android:id="@+id/share_button"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="wrap_content"
android:layout_width="48dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginEnd="32dp"
android:padding="16dp"
android:text="@string/share"
android:textColor="@color/green"
app:icon="@drawable/outline_share_18"
app:iconTint="@color/green"
Expand All @@ -41,7 +40,7 @@
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="48dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginEnd="8dp"
android:onClick="@{(view) -> viewmodel.unBookmark(view, post)}"
android:padding="16dp"
android:visibility="gone"
Expand All @@ -55,9 +54,9 @@
<com.google.android.material.button.MaterialButton
android:id="@+id/bookmark_button"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="46dp"
android:layout_width="48dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginEnd="8dp"
android:onClick="@{(view) -> viewmodel.bookmark(view, post)}"
android:padding="16dp"
android:visibility="gone"
Expand Down Expand Up @@ -95,7 +94,7 @@
android:id="@+id/imageView14"
android:layout_width="46dp"
android:layout_height="46dp"
android:layout_marginStart="16dp"
android:layout_marginStart="32dp"
android:contentDescription="@string/content_description_user_profile_image"
android:onClick="@{(view) -> viewmodel.openUserProfile(view, post.user)}"
app:layout_constraintBottom_toBottomOf="parent"
Expand Down Expand Up @@ -158,7 +157,7 @@
android:textAppearance="@style/TextAppearance.MaterialComponents.Body2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/divider7" />
app:layout_constraintTop_toBottomOf="@+id/textView21" />

<TextView
android:id="@+id/textView21"
Expand All @@ -175,7 +174,7 @@
android:textIsSelectable="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView20" />
app:layout_constraintTop_toBottomOf="@+id/ratingBar" />

<TextView
android:id="@+id/textView22"
Expand All @@ -194,7 +193,7 @@
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView21" />
app:layout_constraintTop_toBottomOf="@+id/textView20" />

<View
android:id="@+id/divider6"
Expand All @@ -218,22 +217,23 @@
android:id="@+id/divider7"
android:layout_width="56dp"
android:layout_height="2dp"
android:layout_marginTop="16dp"
android:background="@color/secondaryColor"
app:layout_constraintBottom_toBottomOf="@+id/ratingBar"
app:layout_constraintStart_toStartOf="@+id/textView19"
app:layout_constraintTop_toBottomOf="@+id/textView19" />
app:layout_constraintTop_toTopOf="@+id/ratingBar" />

<TextView
android:id="@+id/textView18"
<RatingBar
android:id="@+id/ratingBar"
style="@android:style/Widget.Material.RatingBar.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:fontFamily="@font/roboto_medium"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.MaterialComponents.Body1"
app:layout_constraintBottom_toBottomOf="@+id/divider7"
android:layout_marginTop="32dp"
android:max="5"
android:numStars="5"
android:rating="@{post.rating}"
app:layout_constraintStart_toEndOf="@+id/divider7"
app:layout_constraintTop_toTopOf="@+id/divider7" />
app:layout_constraintTop_toBottomOf="@+id/textView19" />

</androidx.constraintlayout.widget.ConstraintLayout>

Expand Down
Loading

0 comments on commit 5bd38ab

Please sign in to comment.