Skip to content

Commit

Permalink
Optimise code
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitjakhar committed Jun 30, 2021
1 parent d8205c0 commit 07a4f6b
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 110 deletions.
9 changes: 9 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ plugins {
}

android {
signingConfigs {
release {
storeFile file('/home/rohit/Documents/Quizzon/quizzonkey.jks')
storePassword storePassword
keyAlias keyAlias
keyPassword keyPassword
}
}
compileSdkVersion 30
buildToolsVersion "30.0.3"

Expand All @@ -22,6 +30,7 @@ android {
buildConfigField("String", "USERNAME", username)
buildConfigField("String", "PASSWORD", password)
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
signingConfig signingConfigs.release
}

buildFeatures {
Expand Down
11 changes: 3 additions & 8 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.VIBRATE" />

<application
android:name=".MyApp"
android:allowBackup="true"
android:fullBackupContent="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
Expand All @@ -29,13 +30,7 @@
</activity>
<activity
android:name=".MainActivity"
android:windowSoftInputMode="adjustResize">
<!-- <intent-filter>-->
<!-- <action android:name="android.intent.action.MAIN" />-->

<!-- <category android:name="android.intent.category.LAUNCHER" />-->
<!-- </intent-filter>-->
</activity>
android:windowSoftInputMode="adjustResize" />

<meta-data
android:name="preloaded_fonts"
Expand Down
5 changes: 1 addition & 4 deletions app/src/main/java/com/rohit/quizzon/data/RemoteRepository.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.rohit.quizzon.data

import android.util.Log
import com.google.firebase.auth.FirebaseAuth
import com.rohit.quizzon.BuildConfig
import com.rohit.quizzon.data.model.body.DataGetBody
Expand Down Expand Up @@ -79,13 +78,11 @@ class RemoteRepository @Inject constructor(
suspend fun registerUser(
email: String,
username: String,
password: String,
gender: String
password: String
) = flow<NetworkResponse<DataInsertResponse>> {
emit(NetworkResponse.Loading())
val response = firebaseAuth.createUserWithEmailAndPassword(email, password).await()
response.user?.let {
Log.d("userImage", it.photoUrl.toString())
it.sendEmailVerification()
val userProfileBody = UserProfileBody(
user_id = it.uid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class PlayQuizFragment : Fragment() {
private var rightAnswer = 0
private var wrongAnswer = 0
private lateinit var quizData: QuizResponse
private var selectedOptionText = ""
private var isCheckedAnswer = false

@SuppressLint("SetTextI18n")
Expand Down Expand Up @@ -113,42 +112,13 @@ class PlayQuizFragment : Fragment() {
private fun updateView() {
loadQuestionAnimation()
binding.apply {
textQuizTitle.text = quizData.quizTitle
textQuestionStatement.text = quizData.questionList[currentPosition].questionStatement
radioOption1.text = quizData.questionList[currentPosition].option1
radioOption2.text = quizData.questionList[currentPosition].option2
radioOption3.text = quizData.questionList[currentPosition].option3
radioOption4.text = quizData.questionList[currentPosition].option4
}
// binding.apply {
// optionChipGroup.clearCheck()
// textQuestionStatement.text = quizData.questionList[currentPosition].questionStatement
// chipOption1.text = quizData.questionList[currentPosition].option1
// chipOption2.text = quizData.questionList[currentPosition].option2
// chipOption3.text = quizData.questionList[currentPosition].option3
// chipOption4.text = quizData.questionList[currentPosition].option4
// optionChipGroup.clearCheck()
// }
}

private fun finishQuestionAnimation() = binding.apply {
radioOption1.animate()
.alpha(0f)
.duration = 400L
radioOption2.animate()
.alpha(0f)
.duration = 400L
radioOption3.animate()
.alpha(0f)
.duration = 400L
radioOption4.animate()
.alpha(0f)
.duration = 400L
btnCheckAnswer.animate()
.alpha(0f)
.duration = 400L
textQuestionStatement.animate()
.alpha(0f)
.duration = 400L
}

private fun loadQuestionAnimation() = binding.apply {
Expand Down
79 changes: 52 additions & 27 deletions app/src/main/java/com/rohit/quizzon/ui/fragment/SignupFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,56 +42,80 @@ class SignupFragment : Fragment() {
savedInstanceState: Bundle?
): View {
binding = FragmentSignupBinding.inflate(inflater, container, false)
initSpannableString()
initClickListener()
initView()
return binding.root
}

val signupText = resources.getString(R.string.login_line)
val spanableString = SpannableStringBuilder(signupText)
private fun initView() = binding.apply {
btnUserSignup.setDisableViews(
listOf(
userNameInputLayout,
userConfirmPasswordInputLayout,
userPasswordInputLayout,
userEmailInputLayout,
textLogin,
fabBackButton
)
)
}

private fun initClickListener() {
binding.btnUserSignup.setOnClickListener {
val userName = binding.userNameInputLayout.editText?.text.toString().trim()
val userEmail = binding.userEmailInputLayout.editText?.text.toString().trim()
val password = binding.userPasswordInputLayout.editText?.text.toString().trim()
val confirmPassword =
binding.userConfirmPasswordInputLayout.editText?.text.toString().trim()
if (validateUserInput(
userName,
userEmail,
password,
confirmPassword
)
) {
binding.btnUserSignup.activate()
signupViewModel.registerUser(
username = userName,
userEmail = userEmail,
userPassword = password
)
checkStatus()
} else return@setOnClickListener
}
}

private fun initSpannableString() {
val signupText = resources.getString(R.string.login_line)
val spannableStringBuilder = SpannableStringBuilder(signupText)
val signUpClick = object : ClickableSpan() {
override fun onClick(widget: View) {
findNavController().navigate(SignupFragmentDirections.actionSignupFragmentToLoginFragment())
}
}
spanableString.setSpan(
spannableStringBuilder.setSpan(
signUpClick,
25,
(signupText.length),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
spanableString.setSpan(
spannableStringBuilder.setSpan(
object : ForegroundColorSpan(Color.parseColor("#00AB5C")) {},
25,
(signupText.length),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
binding.textLogin.movementMethod = LinkMovementMethod.getInstance()
binding.textLogin.setText(spanableString, TextView.BufferType.SPANNABLE)
binding.btnUserSignup.setOnClickListener {
val userName = binding.userNameInputLayout.editText?.text.toString().trim()
val userEmail = binding.userEmailInputLayout.editText?.text.toString().trim()
val password = binding.userPasswordInputLayout.editText?.text.toString().trim()
val confirmPassword =
binding.userConfirmPasswordInputLayout.editText?.text.toString().trim()
if (validateUserInput(
userName,
userEmail,
password,
confirmPassword
)
) signupViewModel.registerUser(
username = userName,
userEmail = userEmail,
userPassword = password
)
checkStatus()
}
return binding.root
binding.textLogin.setText(spannableStringBuilder, TextView.BufferType.SPANNABLE)
}

private fun checkStatus() {
viewLifecycleOwner.lifecycleScope.launchWhenStarted {
signupViewModel.registerState.collectLatest { value ->
when (value) {
is NetworkResponse.Success -> {
binding.btnUserSignup.finished()
binding.root.snack("We sent verification link to your email") {
action("Ok") {
findNavController().navigate(SignupFragmentDirections.actionSignupFragmentToLoginFragment())
Expand All @@ -100,13 +124,14 @@ class SignupFragment : Fragment() {
findNavController().navigate(SignupFragmentDirections.actionSignupFragmentToLoginFragment())
}
is NetworkResponse.Failure -> {
binding.root.snack("We sent verification link to your email") {
binding.btnUserSignup.reset()
binding.root.snack("${value.message}") {
action("Ok") {
}
}
}
is NetworkResponse.Loading -> {
// TODO show loading
binding.btnUserSignup.activate()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.rohit.quizzon.ui.viewmodels
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.rohit.quizzon.data.RemoteRepository
import com.rohit.quizzon.data.model.body.User
import com.rohit.quizzon.data.model.response.DataInsertResponse
import com.rohit.quizzon.utils.NetworkResponse
import dagger.hilt.android.lifecycle.HiltViewModel
Expand All @@ -29,8 +28,7 @@ class SignUpViewModel @Inject constructor(
remoteRepository.registerUser(
username = username,
email = userEmail,
password = userPassword,
gender = "Male"
password = userPassword
).collect { status ->
_registerState.value = status
}
Expand Down
90 changes: 54 additions & 36 deletions app/src/main/res/layout/fragment_play_quiz.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,60 @@
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/gradient"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.35"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="80dp"
android:alpha="0.5"
android:background="@drawable/bottom_round_background"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

<com.google.android.material.card.MaterialCardView
android:id="@+id/cvQuizTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="60dp"
app:cardCornerRadius="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/cyan_a400"
android:padding="16dp">

<TextView
android:id="@+id/textQuizTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/blue_grey_900"
android:textSize="24sp"
tools:text="Quiz Title" />
</FrameLayout>
</com.google.android.material.card.MaterialCardView>

<com.google.android.material.card.MaterialCardView
android:id="@+id/cvQuestion"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginHorizontal="24dp"
android:layout_marginTop="16dp"
app:cardCornerRadius="16dp"
app:cardElevation="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline4">
app:layout_constraintTop_toBottomOf="@id/cvQuizTitle">

<LinearLayout
android:layout_width="match_parent"
Expand Down Expand Up @@ -112,41 +139,32 @@
android:alpha="0"
android:text="@string/check_answer"
tools:alpha="1" />

<TextView
android:id="@+id/textAnswerStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/wrong"
android:textColor="@color/red_700"
android:textSize="24sp"
android:textStyle="bold"
android:visibility="gone"
tools:visibility="visible" />

<TextView
android:id="@+id/textRightAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:textColor="@color/blue_grey_900"
android:textSize="20sp"
android:visibility="gone"
tools:text="Right Answer: Option A"
tools:visibility="visible" />
</LinearLayout>

</com.google.android.material.card.MaterialCardView>

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.25" />

<TextView
android:id="@+id/textAnswerStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/wrong"
android:textColor="@color/red_700"
android:textSize="24sp"
android:textStyle="bold"
android:visibility="gone"
app:layout_constraintBottom_toTopOf="@+id/textRightAnswer"
app:layout_constraintStart_toStartOf="@+id/textRightAnswer"
tools:visibility="visible" />

<TextView
android:id="@+id/textRightAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:textColor="@color/blue_grey_900"
android:textSize="20sp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:text="Right Answer: Option A"
tools:visibility="visible" />
</androidx.constraintlayout.widget.ConstraintLayout>
Loading

0 comments on commit 07a4f6b

Please sign in to comment.