Skip to content

Instantly share code, notes, and snippets.

@androuino
Created November 25, 2019 02:54
Show Gist options
  • Save androuino/c99090e67b870ceab3a8ed4e3dd85ca1 to your computer and use it in GitHub Desktop.
Save androuino/c99090e67b870ceab3a8ed4e3dd85ca1 to your computer and use it in GitHub Desktop.
package com.example.mvvm.ui.base
import android.annotation.TargetApi
import android.content.Context
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.TextView
import androidx.annotation.NonNull
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.google.android.material.snackbar.Snackbar
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.database.FirebaseDatabase
import com.example.mvvm.R
import com.example.mvvm.Service
import com.example.mvvm.ui.dialogs.ConfirmationRepositoryImpl
import com.example.mvvm.utils.LocalData
import javax.inject.Inject
abstract class BaseFragment<VM> : Fragment() {
@Inject
lateinit var firebaseAuth: FirebaseAuth
@Inject
lateinit var firebaseDatabase: FirebaseDatabase
@Inject
lateinit var confirmationRepositoryImpl: ConfirmationRepositoryImpl
@Inject
lateinit var isMIUI: LocalData
private lateinit var service: Service
lateinit var googleSignInClient: GoogleSignInClient
var title: String = ""
//fun <T : BaseKey> getKey(): T = requireArguments.getParcelable("KEY")!!
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
service = Service()
}
@NonNull
fun <T : BaseKey<*>> getKey(): T {
val args = arguments ?: throw IllegalStateException("Fragment cannot have null arguments.")
return args.getParcelable("KEY") ?: throw IllegalStateException("Fragment cannot have null key")
}
abstract fun bindViewModel(viewModel: VM)
@TargetApi(Build.VERSION_CODES.CUPCAKE)
fun View.hideKeyboard() {
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(windowToken, 0)
}
fun snackbar(message: String) {
val snackbar = Snackbar.make(activity!!.window.decorView.rootView, "", Snackbar.LENGTH_LONG)
val snackbarView = snackbar.view
val tv = snackbarView.findViewById(com.google.android.material.R.id.snackbar_text) as TextView
when (message) {
"reset-success" -> {
snackbar.setText(getString(R.string.reset_success))
tv.setTextColor(Color.WHITE)
snackbarView.setBackgroundColor(ContextCompat.getColor(context!!, R.color.snackbarSuccess))
}
"reset-error" -> {
snackbar.setText(getString(R.string.reset_error))
tv.setTextColor(Color.WHITE)
snackbarView.setBackgroundColor(ContextCompat.getColor(context!!, R.color.snackbarError))
}
"log-in-error" -> {
snackbar.setText(getString(R.string.log_in_error))
tv.setTextColor(Color.WHITE)
snackbarView.setBackgroundColor(ContextCompat.getColor(context!!, R.color.snackbarError))
}
"verify-account" -> {
snackbar.setText(getString(R.string.verify_account))
tv.setTextColor(Color.WHITE)
snackbarView.setBackgroundColor(ContextCompat.getColor(context!!, R.color.snackbarError))
}
"verification-error" -> {
snackbar.setText(getString(R.string.verification_error))
tv.setTextColor(Color.WHITE)
snackbarView.setBackgroundColor(ContextCompat.getColor(context!!, R.color.snackbarError))
}
"register-success" -> {
snackbar.setText(getString(R.string.register_success))
tv.setTextColor(Color.WHITE)
snackbarView.setBackgroundColor(ContextCompat.getColor(context!!, R.color.snackbarSuccess))
}
"register-error" -> {
snackbar.setText(getString(R.string.register_error))
tv.setTextColor(Color.WHITE)
snackbarView.setBackgroundColor(ContextCompat.getColor(context!!, R.color.snackbarError))
}
"register-password-error" -> {
snackbar.setText(getString(R.string.register_password_error))
tv.setTextColor(Color.WHITE)
snackbarView.setBackgroundColor(ContextCompat.getColor(context!!, R.color.snackbarError))
}
"invalid-password" -> {
snackbar.setText(getString(R.string.invalid_password))
tv.setTextColor(Color.WHITE)
snackbarView.setBackgroundColor(ContextCompat.getColor(context!!, R.color.snackbarError))
}
"already-in-use" -> {
snackbar.setText(getString(R.string.already_in_use))
tv.setTextColor(Color.WHITE)
snackbarView.setBackgroundColor(ContextCompat.getColor(context!!, R.color.snackbarError))
}
"empty" -> {
snackbar.setText(getString(R.string.empty))
tv.setTextColor(Color.WHITE)
snackbarView.setBackgroundColor(ContextCompat.getColor(context!!, R.color.snackbarError))
}
"address-error" -> {
snackbar.setText(getString(R.string.address_error))
tv.setTextColor(Color.WHITE)
snackbarView.setBackgroundColor(ContextCompat.getColor(context!!, R.color.snackbarError))
}
"system-error" -> {
snackbar.setText(getString(R.string.system_error))
tv.setTextColor(Color.WHITE)
snackbarView.setBackgroundColor(ContextCompat.getColor(context!!, R.color.snackbarError))
}
else -> {
}
}
snackbar.show()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment