Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: toast to snackbar for login work #788

Merged
merged 10 commits into from
Dec 30, 2018
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.fragment_login.*
import kotlinx.android.synthetic.main.fragment_login.view.*
import org.fossasia.openevent.general.MainActivity
Expand Down Expand Up @@ -67,11 +67,13 @@ class LoginFragment : Fragment() {
})

loginViewModel.error.observe(this, Observer {
Toast.makeText(context, it, Toast.LENGTH_LONG).show()
Snackbar.make(getActivity()!!.findViewById(android.R.id.content),
it, Snackbar.LENGTH_LONG).show()
})

loginViewModel.loggedIn.observe(this, Observer {
Toast.makeText(context, getString(R.string.welcome_back), Toast.LENGTH_LONG).show()
Snackbar.make(getActivity()!!.findViewById(android.R.id.content),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use safe calls instead of !!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but then it is showing type mismatch as snackbar requires view of type View and we are giving View? Are non-null asserted calls allowed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First of all, don't use findViewById. Secondly, that'll fix it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

android.R.id.content This isn't to be used. Use coordinator layout of the current fragment/activity

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I open an issue to change layout to coordinator layout or should do in this PR only. Also if we use coordinator layout of the current fragment as a view then snackbar will not be coming from bottom of the screen. Pls correct me

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, OK. Use CoordinatorLayout of the root element

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but how to get CoordinatorLayout of the root element. when I try to use CoordinatorLayout of root element i.e rootLayout(activity_main.xml) It is becoming null
I'm facing
java.lang.IllegalStateException: rootView.findViewById(R.id.rootLayout) must not be null at org.fossasia.openevent.general.order.OrdersUnderUserFragment.onCreateView(OrdersUnderUserFragment.kt:48)
Need help

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call findViewById on activity

getString(R.string.welcome_back), Snackbar.LENGTH_LONG).show()
loginViewModel.fetchProfile()
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import com.google.android.material.snackbar.Snackbar
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.fragment_profile.view.avatar
import kotlinx.android.synthetic.main.fragment_profile.view.email
Expand Down Expand Up @@ -49,8 +50,11 @@ class ProfileFragment : Fragment() {
override fun onAttach(context: Context) {
super.onAttach(context)
if (!profileViewModel.isLoggedIn()) {
Toast.makeText(context, "You need to Login!", Toast.LENGTH_LONG).show()
redirectToLogin()
Snackbar.make(getActivity()!!.findViewById(android.R.id.content),
"You need to log in first!", Snackbar.LENGTH_SHORT).show();
Handler().postDelayed({
redirectToLogin()
}, 1000)
}
}

Expand All @@ -68,7 +72,8 @@ class ProfileFragment : Fragment() {
})

profileViewModel.error.observe(this, Observer {
Toast.makeText(context, it, Toast.LENGTH_LONG).show()
Snackbar.make(getActivity()!!.findViewById(android.R.id.content),
it, Snackbar.LENGTH_SHORT).show()
})

profileViewModel.user.observe(this, Observer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package org.fossasia.openevent.general.order

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.content_no_tickets.findMyTickets
import kotlinx.android.synthetic.main.fragment_orders_under_user.noTicketsScreen
import kotlinx.android.synthetic.main.fragment_orders_under_user.view.ordersRecycler
Expand Down Expand Up @@ -73,7 +74,8 @@ class OrdersUnderUserFragment : Fragment() {
})

ordersUnderUserVM.message.observe(this, Observer {
Toast.makeText(context, it, Toast.LENGTH_LONG).show()
Snackbar.make(getActivity()!!.findViewById(android.R.id.content),
it, Snackbar.LENGTH_LONG).show()
})

ordersUnderUserVM.noTickets.observe(this, Observer {
Expand All @@ -94,8 +96,11 @@ class OrdersUnderUserFragment : Fragment() {
Timber.d("Fetched events of size %s", ordersRecyclerAdapter.itemCount)
})
} else {
redirectToLogin()
Toast.makeText(context, "You need to log in first!", Toast.LENGTH_LONG).show()
Snackbar.make(getActivity()!!.findViewById(android.R.id.content),
"You need to log in first!", Snackbar.LENGTH_SHORT).show();
Handler().postDelayed({
redirectToLogin()
}, 500)
}

return rootView
Expand Down