Skip to content

Commit

Permalink
add dialogs, and purchase
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedshasho committed Aug 16, 2023
1 parent caca4b8 commit 3e1fab1
Show file tree
Hide file tree
Showing 25 changed files with 499 additions and 66 deletions.
19 changes: 14 additions & 5 deletions app/src/main/java/com/scholar/center/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
package com.scholar.center

import androidx.appcompat.app.AppCompatActivity
import android.app.Application
import android.content.Context
import android.os.Bundle
import android.util.Log
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import com.scholar.center.databinding.ActivityMainBinding
import com.scholar.center.unit.LocaleHelper
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch


@AndroidEntryPoint
class MainActivity : AppCompatActivity() {

private val viewModel: MainViewModel by viewModels()
private lateinit var binding: ActivityMainBinding
private lateinit var navController: NavController

companion object{

}


override fun onCreate(savedInstanceState: Bundle?) {
installSplashScreen()
Expand All @@ -31,10 +36,12 @@ class MainActivity : AppCompatActivity() {
setContentView(binding.root)


val navHostFragment = supportFragmentManager.findFragmentById(binding.rootNavHostFragment.id) as NavHostFragment
val navHostFragment =
supportFragmentManager.findFragmentById(binding.rootNavHostFragment.id) as NavHostFragment
navController = navHostFragment.navController
val navGraph = navController.navInflater.inflate(R.navigation.root_nav_graph)


lifecycleScope.launch {
viewModel.startDestination.collectLatest { startDestination ->
startDestination?.let {
Expand All @@ -43,6 +50,8 @@ class MainActivity : AppCompatActivity() {
}
}
}


}

}
9 changes: 5 additions & 4 deletions app/src/main/java/com/scholar/center/MainViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.scholar.center

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.scholar.domain.repo.DataStorePreference
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import javax.inject.Inject


@HiltViewModel
class MainViewModel @Inject
constructor(
Expand All @@ -20,9 +19,11 @@ constructor(

var startDestination = MutableStateFlow<Int?>(null)
private set

init {
viewModelScope.launch {
val isFirstOpen = dataStore.readValue(DataStorePreference.isAppFirstOpen).first() ?: true
val isFirstOpen =
dataStore.readValue(DataStorePreference.isAppFirstOpen).first() ?: true
startDestination.value = if (isFirstOpen) {
R.id.loginFragment
} else {
Expand Down
170 changes: 167 additions & 3 deletions app/src/main/java/com/scholar/center/ui/credit/CreditFragment.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,186 @@
package com.scholar.center.ui.credit

import android.Manifest
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.view.animation.AlphaAnimation
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat
import androidx.core.widget.addTextChangedListener
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.fragment.findNavController
import com.scholar.center.R
import com.scholar.center.databinding.FragmentCreditBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import java.io.File

@AndroidEntryPoint
class CreditFragment :Fragment(R.layout.fragment_credit) {
lateinit var binding :FragmentCreditBinding
private val viewModel : CreditVM by viewModels()
class CreditFragment : Fragment(R.layout.fragment_credit) {
lateinit var binding: FragmentCreditBinding
private val viewModel: CreditVM by viewModels()

private lateinit var permissionLauncher: ActivityResultLauncher<String>
private lateinit var imagePickerLauncher: ActivityResultLauncher<String>
private lateinit var methodAdapter: ArrayAdapter<String>

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
methodAdapter =
ArrayAdapter<String>(requireContext(), android.R.layout.simple_spinner_item).apply {
setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
}
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
binding = FragmentCreditBinding.bind(view)

binding.creditAmountEditText.addTextChangedListener {
if (it.toString().isNotEmpty())
viewModel.onAmountInputChanged(it.toString().toInt())
}
binding.creditDescriptionEditText.addTextChangedListener {
viewModel.onDescriptionInputChanged(it.toString())
}

binding.addContactSend.setOnClickListener {
viewModel.onSendClick()
}

binding.creditSpinnerMethod.adapter = methodAdapter
addMethod()
binding.creditSpinnerMethod.onItemSelectedListener =
object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(
parent: AdapterView<*>?,
view: View?,
position: Int,
id: Long,
) {
viewModel.onMethodInputChanged(position)
}

override fun onNothingSelected(parent: AdapterView<*>?) {}
}


binding.addImagePaymentLayout.setOnClickListener {
if (permissionGranted()) {
imagePickerLauncher.launch("image/*")
} else {
permissionLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE)
}

}

permissionLauncher =
registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted: Boolean ->
if (isGranted) {
imagePickerLauncher.launch("image/*")
} else {
Toast.makeText(requireContext(), "Permission denied", Toast.LENGTH_SHORT).show()
}
}

lifecycleScope.launch {
viewModel.message.collectLatest { message ->
message?.let {
Toast.makeText(requireContext(), it, Toast.LENGTH_SHORT).show()
}
}
}


imagePickerLauncher = registerForActivityResult(
ActivityResultContracts.GetContent()
) { uri: Uri? ->
if (uri != null) {
val filePath =
requireContext().contentResolver?.openInputStream(uri)?.use { inputStream ->
val file = File(requireContext().cacheDir, "temp_file")
file.outputStream().use { outputStream ->
inputStream.copyTo(outputStream)
}
file.absolutePath
}
filePath?.let { viewModel.onLinkInputChanged(it) }
}
}

viewLifecycleOwner.lifecycleScope.launch {
repeatOnLifecycle(state = Lifecycle.State.CREATED) {
viewModel.creditState.collect { state ->
binding.creditProgressBar.visibility =
if (state.isLoading) View.VISIBLE else View.GONE

if (state.isInputValid || state.errorMessageInput == null)
hideTextError() else showTextError()
state.errorMessageInput?.let { error ->
binding.creditErrorText.text = getString(error)
}
if (state.linkInput.isNotEmpty()) {
binding.addImagePaymentImageview.setImageResource(R.drawable.check_mark)
}
val hasErrorMessage = state.errorMessageLoginProcess.isNullOrEmpty()
if (!hasErrorMessage) {
binding.creditErrorText.text = state.errorMessageLoginProcess
showTextError()
}

if(state.isSuccessfullySend){
findNavController().popBackStack()
}
}
}
}
}

private fun addMethod(data: List<String> = emptyList()) {
val mutableData = data.toMutableList()
mutableData.add(getString(R.string.select_method))
mutableData.add("شركة الهرم")
mutableData.add("بنك البركة")
mutableData.add("بنك بيبلوس")
mutableData.add("بنك بيمو السعودي الفرنسي")
mutableData.add("MTN cash")
mutableData.add("SYRIATEL cash")
methodAdapter.clear()
methodAdapter.addAll(mutableData)
methodAdapter.notifyDataSetChanged()
}

private fun permissionGranted(): Boolean {
return ContextCompat.checkSelfPermission(
requireContext(), Manifest.permission.READ_EXTERNAL_STORAGE
) == PackageManager.PERMISSION_GRANTED
}

private fun showTextError() {
// Fade in
if (binding.creditErrorText.visibility == View.VISIBLE) return
val alpha = AlphaAnimation(0f, 1f)
alpha.duration = 300
binding.creditErrorText.startAnimation(alpha)
binding.creditErrorText.visibility = View.VISIBLE
}

private fun hideTextError() {
// Fade out
if (binding.creditErrorText.visibility == View.GONE) return
val alpha = AlphaAnimation(1f, 0f)
alpha.duration = 300
binding.creditErrorText.startAnimation(alpha)
binding.creditErrorText.visibility = View.GONE
}
}
Loading

0 comments on commit 3e1fab1

Please sign in to comment.