Updated on 2026-08-14
This commit is contained in:
parent
c56705f4cb
commit
c13348a018
11 changed files with 150 additions and 170 deletions
|
|
@ -0,0 +1,8 @@
|
|||
package com.tangem.ui.activity
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import com.tangem.ui.navigation.NavigationResult
|
||||
|
||||
class GlobalViewModel : ViewModel() {
|
||||
var navigationResult: NavigationResult? = null
|
||||
}
|
||||
|
|
@ -13,31 +13,29 @@ import android.os.Bundle
|
|||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.app.ActivityCompat
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.navigation.NavController
|
||||
import androidx.navigation.Navigation
|
||||
import androidx.lifecycle.ViewModelProviders
|
||||
import com.scottyab.rootbeer.RootBeer
|
||||
import com.tangem.App
|
||||
import com.tangem.Constant
|
||||
import com.tangem.card_android.android.nfc.NfcLifecycleObserver
|
||||
import com.tangem.card_android.android.reader.NfcManager
|
||||
import com.tangem.di.Navigator
|
||||
import com.tangem.di.ToastHelper
|
||||
import com.tangem.ui.dialog.RootFoundDialog
|
||||
import com.tangem.wallet.BuildConfig
|
||||
import com.tangem.wallet.R
|
||||
import javax.inject.Inject
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
class MainActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
|
||||
|
||||
companion object {
|
||||
val TAG: String = MainActivity::class.java.simpleName
|
||||
fun callingIntent(context: Context) = Intent(context, MainActivity::class.java)
|
||||
}
|
||||
|
||||
lateinit var navController: NavController
|
||||
@Inject
|
||||
internal lateinit var navigator: Navigator
|
||||
@Inject
|
||||
internal lateinit var toastHelper: ToastHelper
|
||||
lateinit var viewModel: GlobalViewModel
|
||||
lateinit var nfcManager: NfcManager
|
||||
|
||||
// private var onNfcReaderCallback: NfcAdapter.ReaderCallback? = null
|
||||
|
||||
|
|
@ -54,15 +52,17 @@ class MainActivity : AppCompatActivity() {
|
|||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_main)
|
||||
|
||||
navController = Navigation.findNavController(this, R.id.nav_host_fragment)
|
||||
viewModel = ViewModelProviders.of(this).get(GlobalViewModel::class.java)
|
||||
|
||||
App.navigatorComponent.inject(this)
|
||||
App.toastHelperComponent.inject(this)
|
||||
|
||||
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
|
||||
|
||||
verifyPermissions()
|
||||
|
||||
nfcManager = NfcManager(this, this)
|
||||
lifecycle.addObserver(NfcLifecycleObserver(nfcManager))
|
||||
|
||||
// // NFC
|
||||
// val intent = intent
|
||||
// if (intent != null && (NfcAdapter.ACTION_TECH_DISCOVERED == intent.action || NfcAdapter.ACTION_NDEF_DISCOVERED == intent.action)) {
|
||||
|
|
@ -85,4 +85,14 @@ class MainActivity : AppCompatActivity() {
|
|||
}
|
||||
}
|
||||
|
||||
override fun onTagDiscovered(tag: Tag) {
|
||||
val activeFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment)
|
||||
?.childFragmentManager?.primaryNavigationFragment
|
||||
if (activeFragment is NfcAdapter.ReaderCallback) {
|
||||
activeFragment.onTagDiscovered(tag)
|
||||
} else {
|
||||
nfcManager.ignoreTag(tag)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
96
app/src/main/java/com/tangem/ui/fragment/BaseFragment.kt
Normal file
96
app/src/main/java/com/tangem/ui/fragment/BaseFragment.kt
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
package com.tangem.ui.fragment
|
||||
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.annotation.IdRes
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.navigation.fragment.NavHostFragment.findNavController
|
||||
import com.tangem.ui.activity.MainActivity
|
||||
import com.tangem.ui.navigation.NavigationResult
|
||||
import com.tangem.ui.navigation.NavigationResultListener
|
||||
|
||||
abstract class BaseFragment : Fragment() {
|
||||
|
||||
protected val requestCode: String
|
||||
get() = arguments?.getString(ARGUMENT_NAVIGATION_REQUEST_CODE, REQUEST_CODE_NOT_SET)
|
||||
?: REQUEST_CODE_NOT_SET
|
||||
protected val navigatedBack: Boolean
|
||||
get() = (activity as? MainActivity)?.viewModel?.navigationResult != null
|
||||
|
||||
protected abstract val layoutId: Int
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
Log.d("LIFECYCLE", "onCreate: ${this::class.java.simpleName}")
|
||||
}
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
||||
return inflater.inflate(layoutId, container, false)
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
Log.d("LIFECYCLE", "onStart: ${this::class.java.simpleName}")
|
||||
if (this is NavigationResultListener) {
|
||||
val navigationResult = (activity as? MainActivity)?.viewModel?.navigationResult
|
||||
navigationResult?.run {
|
||||
onNavigationResult(this.requestCode, this.resultCode, this.data)
|
||||
(activity as? MainActivity)?.viewModel?.navigationResult = null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
super.onPause()
|
||||
Log.d("LIFECYCLE", "onPause: ${this::class.java.simpleName}")
|
||||
}
|
||||
|
||||
override fun onStop() {
|
||||
super.onStop()
|
||||
Log.d("LIFECYCLE", "onStop: ${this::class.java.simpleName}")
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
Log.d("LIFECYCLE", "onDestroy: ${this::class.java.simpleName}")
|
||||
}
|
||||
|
||||
protected fun navigateUp(@IdRes destination: Int = DESTINATION_NOT_SET): Boolean {
|
||||
return if (destination == DESTINATION_NOT_SET) {
|
||||
findNavController(this).popBackStack()
|
||||
} else {
|
||||
findNavController(this).popBackStack(destination, true)
|
||||
}
|
||||
}
|
||||
|
||||
protected fun navigateForResult(requestCode: String, @IdRes destination: Int, data: Bundle? = null) {
|
||||
val argumentsWithRequestCode = (data ?: Bundle()).apply {
|
||||
putString(ARGUMENT_NAVIGATION_REQUEST_CODE, requestCode)
|
||||
}
|
||||
navigateToDestination(destination, argumentsWithRequestCode)
|
||||
}
|
||||
|
||||
protected fun navigateToDestination(@IdRes destination: Int, data: Bundle? = null) {
|
||||
try {
|
||||
findNavController(this).navigate(destination, data)
|
||||
} catch (e: IllegalArgumentException) {
|
||||
Log.w(this::class.java.simpleName, e.message)
|
||||
}
|
||||
}
|
||||
|
||||
protected fun navigateBackWithResult(resultCode: Int, data: Bundle? = null,
|
||||
@IdRes destination: Int = DESTINATION_NOT_SET): Boolean {
|
||||
(requireActivity() as MainActivity).viewModel.navigationResult =
|
||||
NavigationResult(requestCode, resultCode, data)
|
||||
return navigateUp(destination)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val ARGUMENT_NAVIGATION_REQUEST_CODE = "NAVIGATION_REQUEST_CODE"
|
||||
private const val DESTINATION_NOT_SET = -1
|
||||
private const val REQUEST_CODE_NOT_SET = "REQUEST_CODE_NOT_SET"
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
package com.tangem.ui.navigation
|
||||
|
||||
import android.os.Bundle
|
||||
|
||||
data class NavigationResult(val requestCode: String, val resultCode: Int, val data: Bundle? = null)
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
package com.tangem.ui.navigation
|
||||
|
||||
import android.os.Bundle
|
||||
|
||||
interface NavigationResultListener {
|
||||
fun onNavigationResult(requestCode: String, resultCode: Int, data: Bundle? = null)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue