嗨,这是我的视图模型类
class AuthViewModel(private val repository: UserRepository) : ViewModel() {
/** Variables for custom login */
var email: String? = null
var password: String? = null
var authTask: Task<AuthResult>? = null
var authListener : AuthListener? = null
var onCompleteListener : OnCompleteListener<AuthResult>? = null
/** Method to perform login operation with facebook */
fun onClickLoginWithFacebook(view: View){
}
/** Method to perform login operation with google */
fun onClickLoginWithGoogle(view: View){
}
/** Method to perform login operation with custom */
fun onClickCustomLogin(view: View){
authListener?.onStarted()
authTask = repository.userLogin(email!!,password!!)
authTask!!.addOnCompleteListener(onCompleteListener!!)
}
/** Method to perform sign up operation with custom */
fun onClickSignUp(view: View){
}
}
这是我的authListener
interface AuthListener : OnFailureListener{
fun onStarted()
override fun onFailure(e: Exception)
}
我希望这个authListener
这样握住onCompletListener
interface AuthListener : OnFailureListener, OnCompleteListener<AuthResult>{
fun onStarted()
override fun onFailure(e: Exception)
override fun onComplete(task: Task<AuthResult>)
}
这是我的loginActvitiy
class LoginActivity : AppCompatActivity(),AuthListener, KodeinAware,OnCompleteListener<AuthResult>{
override val kodein by kodein()
private val factory : AuthViewModelFactory by instance()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// setContentView(R.layout.activity_login)
val binding: ActivityLoginBinding = DataBindingUtil.setContentView(this,R.layout.activity_login)
var viewModel = ViewModelProviders.of(this,factory).get(AuthViewModel::class.java)
binding.loginViewModel = viewModel
viewModel?.authListener = this
viewModel?.onCompleteListener = this
}
override fun onStarted() {
loginProgressBar.show()
}
override fun onComplete(task: Task<AuthResult>) {
loginProgressBar.hide()
if(task.isSuccessful){
toast("Login Successfull")
}else{
toast("Invalid email or password")
}
}
override fun onFailure(e: Exception) {
loginProgressBar.hide()
toast("Something went wrong please try again")
}
}
我想将此更改为(从实现中删除onCompletedListener
)
class LoginActivity : AppCompatActivity(),AuthListener, KodeinAware{
override val kodein by kodein()
private val factory : AuthViewModelFactory by instance()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// setContentView(R.layout.activity_login)
val binding: ActivityLoginBinding = DataBindingUtil.setContentView(this,R.layout.activity_login)
var viewModel = ViewModelProviders.of(this,factory).get(AuthViewModel::class.java)
binding.loginViewModel = viewModel
viewModel?.authListener = this
viewModel?.onCompleteListener = this
}
override fun onStarted() {
loginProgressBar.show()
}
override fun onComplete(task: Task<AuthResult>) {
loginProgressBar.hide()
if(task.isSuccessful){
toast("Login Successfull")
}else{
toast("Invalid email or password")
}
}
override fun onFailure(e: Exception) {
loginProgressBar.hide()
toast("Something went wrong please try again")
}
}
我可以这样做