I need to transform this and some other code made in kotlin to java, but it is not working. Here is one of them.
BackgroundActivity.kt (this I need to transform to java):
#RequiresApi(Build.VERSION_CODES.LOLLIPOP)
override fun surfaceChanged(holder: SurfaceHolder, p1: Int, p2: Int, p3: Int) {
RtpService.setView(surfaceView)
RtpService.startPreview()
}
This function is inside a kotlin class, that I will keep
RtpService.kt
package com.wifi.service.backgroundexample
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.IBinder
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import com.wifi.rtplibrary.base.Camera2Base
import com.wifi.rtplibrary.rtmp.RtmpCamera2
import com.wifi.rtplibrary.rtsp.RtspCamera2
import com.wifi.rtplibrary.view.OpenGlView
import com.wifi.service.R
/**
* Basic RTMP/RTSP service streaming implementation with camera2
*/
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
class RtpService : Service() {
private var endpoint: String? = null
override fun onCreate() {
super.onCreate()
Log.e(TAG, "RTP service create")
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(channelId, channelId, NotificationManager.IMPORTANCE_HIGH)
notificationManager?.createNotificationChannel(channel)
}
keepAliveTrick()
}
private fun keepAliveTrick() {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
val notification = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_baseline_settings_24_branco)
.setSilent(true)
.setOngoing(false)
//.setContentTitle("")
.build()
startForeground(1, notification)
} else {
startForeground(1, Notification())
}
}
override fun onBind(p0: Intent?): IBinder? {
return null
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.e(TAG, "RTP service started")
endpoint = intent?.extras?.getString("endpoint")
if (endpoint != null) {
prepareStreamRtp()
startStreamRtp(endpoint!!)
}
return START_STICKY
}
companion object {
private const val TAG = "RtpService"
//private const val channelId = "rtpStreamChannel"
private const val channelId = "Android"
private const val notifyId = 123456
private var notificationManager: NotificationManager? = null
private var camera2Base: Camera2Base? = null
private var openGlView: OpenGlView? = null
private var contextApp: Context? = null
fun setView(openGlView: OpenGlView) {
this.openGlView = openGlView
camera2Base?.replaceView(openGlView)
}
fun setView(context: Context) {
contextApp = context
this.openGlView = null
camera2Base?.replaceView(context)
}
fun startPreview() {
camera2Base?.startPreview()
}
fun init(context: Context) {
contextApp = context
if (camera2Base == null) camera2Base = RtmpCamera2(context, true, connectCheckerRtp)
}
fun stopStream() {
if (camera2Base != null) {
if (camera2Base!!.isStreaming) camera2Base!!.stopStream()
}
}
fun stopPreview() {
if (camera2Base != null) {
if (camera2Base!!.isOnPreview) camera2Base!!.stopPreview()
}
}
private val connectCheckerRtp = object : ConnectCheckerRtp {
override fun onConnectionStartedRtp(rtpUrl: String) {
//showNotification("Stream connection started")
}
override fun onConnectionSuccessRtp() {
//showNotification("Stream started")
Log.e(TAG, "RTP service destroy")
}
override fun onNewBitrateRtp(bitrate: Long) {
}
override fun onConnectionFailedRtp(reason: String) {
//showNotification("Stream connection failed")
Log.e(TAG, "RTP service destroy")
}
override fun onDisconnectRtp() {
//showNotification("Stream stopped")
}
override fun onAuthErrorRtp() {
//showNotification("Stream auth error")
}
override fun onAuthSuccessRtp() {
//showNotification("Stream auth success")
}
}
private fun showNotification(text: String) {
contextApp?.let {
val notification = NotificationCompat.Builder(it, channelId)
//.setSmallIcon(R.mipmap.ic_launcher)
.setSmallIcon(R.drawable.ic_baseline_settings_24_branco)
.setContentTitle("RTP Stream")
.setSilent(true)
.setOngoing(false)
.setContentText(text).build()
notificationManager?.notify(notifyId, notification)
}
}
}
override fun onDestroy() {
super.onDestroy()
Log.e(TAG, "RTP service destroy")
stopStream()
}
private fun prepareStreamRtp() {
stopStream()
stopPreview()
if (endpoint!!.startsWith("rtmp")) {
camera2Base = if (openGlView == null) {
RtmpCamera2(baseContext, true, connectCheckerRtp)
} else {
RtmpCamera2(openGlView, connectCheckerRtp)
}
} else {
camera2Base = if (openGlView == null) {
RtspCamera2(baseContext, true, connectCheckerRtp)
} else {
RtspCamera2(openGlView, connectCheckerRtp)
}
}
}
private fun startStreamRtp(endpoint: String) {
if (!camera2Base!!.isStreaming) {
if (camera2Base!!.prepareVideo() && camera2Base!!.prepareAudio()) {
camera2Base!!.startStream(endpoint)
}
} else {
//showNotification("You are already streaming :(")
}
}
}
I tried this with java:
JavaVersion.java
RtpService rtpService = new RtpService();
rtpService.init(this);
init is not recognized, I don't work well with kotlin yet
can you try this
#RequiresApi(Build.VERSION_CODES.LOLLIPOP)
public void surfaceChanged(SurfaceHolder holder, int p1, int p2, int p3) {
Intrinsics.checkNotNullParameter(holder, "holder");
RtpService.setView(surfaceView);
RtpService.startPreview();
}
public void setView(OpenGlView openGlView) {
this.openGlView = openGlView;
camera2Base.replaceView(openGlView);
}
Related
I have a chat app. I used RecyclerView and I set stackFromEnd true and reverseLayout false to LinearlayoutManager. when a new message is added at the bottom I mean at the end of the list the recyclerView starts auto scroll downward instead of upward. the adapter is notified by notifyItemInserted().
Expected Behaviour: When a new message is added to the list it should scroll to the bottom or upward. Any help is appreciated. Thanks,
Here is adapter:
class ChatMessagesListAdapter(
var chatMessages: ChatMessagesDataModel
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
companion object {
// we set the header as 0 so we can add more types to the ConversationItem enum
private const val ITEM_TYPE_HEADER = 0
}
override fun getItemViewType(position: Int): Int {
return when {
chatMessages.hasMoreItems -> chatMessages.messages[position].getItemType()
position == 0 -> ITEM_TYPE_HEADER
else -> chatMessages.messages[position - 1].getItemType()
}
}
// return +1 to draw the header if there are no items to load
override fun getItemCount() = chatMessages.messages.size + getContentIndex()
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val inflater = LayoutInflater.from(parent.context)
return when (viewType) {
ITEM_TYPE_HEADER -> {
MessagesHeaderViewHolder(inflater.inflate(R.layout.item_chat_messages_header,
parent, false))
}
ChatItemDataModel.TYPE_ADDED_USER,
ChatItemDataModel.TYPE_VIDEO_NOTIFICATION -> {
MessagesInfoViewHolder(inflater.inflate(R.layout.item_chat_messages_info,
parent, false))
}
else -> {
MessagesMessageViewHolder(inflater.inflate(R.layout.item_chat_messages_message,
parent, false))
}
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
(holder as? MessagesHeaderViewHolder)?.bind()
(holder as? MessagesMessageViewHolder)?.bind(position, chatMessages.messages[getRealPosition(position)])
(holder as? MessagesInfoViewHolder)?.bind(chatMessages.messages[getRealPosition(position)])
}
private fun getContentIndex() = if (chatMessages.hasMoreItems) {
0
} else {
1
}
private fun getRealPosition(position: Int) = if (chatMessages.hasMoreItems) {
position
} else {
position - 1
}
private fun notifyChanges() {
if (chatMessages.numberOfItemsInserted == chatMessages.messages.size || isPreview ||
chatMessages.hadPendingMessages) {
notifyDataSetChanged()
} else {
// +1 because of the header
notifyItemRangeInserted(chatMessages.insertionIndex + getContentIndex(),
chatMessages.numberOfItemsInserted)
}
}
fun updateConversation(newChatMessages: ChatMessagesDataModel) {
chatMessages = newChatMessages
notifyChanges()
}
fun updateMessage(newMessage: ChatItemDataModel, isRemote: Boolean) {
if (newMessage.message.hashIdentifier.isNullOrBlank()) {
addNewMessage(newMessage)
return
}
val messageIndex = chatMessages.messages.indexOfFirst { it.message.hashIdentifier == newMessage.message.hashIdentifier }
if (messageIndex != -1) {
val localMessage = chatMessages.messages[messageIndex]
chatMessages.messages[messageIndex] = newMessage
if (failedMessages.contains(localMessage.message.sid)) {
if (isRemote) {
failedMessages.remove(localMessage.message.sid)
}
notifyItemChanged(messageIndex + getContentIndex())
}
}
else {
addNewMessage(newMessage)
}
}
private fun addNewMessage(newMessage: ChatItemDataModel) {
val oldCount = chatMessages.messages.size
chatMessages.messages.add(newMessage)
notifyItemInserted(oldCount + getContentIndex())
}
fun addLocalMessage(
sharedPrefsStorage: SharedPrefsStorage,
message: String, imageUri: Uri?, hashIdentifier: String
) {
val userMessage = UserMessage(messageBody = message, firstName = sharedPrefsStorage.firstName,
lastName = sharedPrefsStorage.lastName, isFromLoggedUser = true, imageUri = imageUri,
hashIdentifier = hashIdentifier, files = null, reactions = null)
val newMessage = ChatItemDataModel(userMessage, sharedPrefsStorage.profileImageUrl, sharedPrefsStorage.userId.toString())
val oldCount = chatMessages.messages.size
chatMessages.messages.add(newMessage)
notifyItemRangeInserted(oldCount + getContentIndex(), 1)
}
....
}
Here is Fragment:
class ChatRoomMessagesFragment : Fragment() {
#Inject
lateinit var sharedPrefsStorage: SharedPrefsStorage
private var adapter: ChatMessagesListAdapter? = null
......
override fun onMessagesReceived(chatMessages: ChatMessagesDataModel) {
if (adapter == null) {
adapter = ChatMessagesListAdapter(chatMessages)
adapter?.setHasStableIds(true)
binding.recyclerview.adapter = adapter
} else {
adapter?.updateConversation(chatMessages)
}
}
override fun onUserMessageRetrieved(newMessage: ChatItemDataModel, isRemote: Boolean) {
adapter?.updateMessage(newMessage, isRemote)
}
private fun setupUI() {
binding.apply {
recyclerview.apply {
layoutManager = LinearLayoutManager(requireContext()).apply {
reverseLayout = false
stackFromEnd = true
addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
showScrollToBottomButtonIfNeeded(findFirstCompletelyVisibleItemPosition())
}
})
}
addOnScrollListener(object : PaginationScrollListener(
layoutManager as LinearLayoutManager,
ScrollDirection.BOTTOM
) {
override fun loadMoreItems() {
presenter.loadMoreMessages()
}
override fun isLastPage() = !presenter.hasMoreItems()
override fun isLoading() = presenter.isLoadingInProgress()
})
// prevent the list from scrolling when the keyboard opens
addOnLayoutChangeListener { _, _, _, _, bottom, _, _, _, oldBottom ->
if (bottom < oldBottom) {
scrollChatToBottom()
}
}
}
....
adapter?.addLocalMessage(sharedPrefsStorage, message, imageUri, hashIdentifier)
presenter.sendMessage("", message, imageUri, hashIdentifier)
}
private fun scrollChatToBottom() {
binding.apply {
recyclerview.post {
recyclerview.smoothScrollToPosition(recyclerview.adapter?.itemCount ?: 0)
}
}
}
}
Let's suppose your adapter is populated by objects called messages and you have a source of data called messageList (could be a Collection, etc) that is passed to your adapter. You could do something like this:
int position = messageList.size() > 0 ? (messageList.size()-1) : 0;
mRecyclerView.smoothScrollToPosition(position);
Just ensure your adapter is not null and actually gets data from messageList since this is in your Activity/Fragment class and not in the adapter class.
You can scrolldown progrmatically using code below
recyclerView.scrollToPosition(chatList.size() - 1);
You can also try another solution if above one doesn't works
setStackFromEnd(true) or setReverseLayout(true)
yourAdapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
super.onItemRangeInserted(positionStart, itemCount)
this#EventDetails.binding.rvData.scrollToPosition(positionStart)
}
override fun onItemRangeChanged(positionStart: Int, itemCount: Int) {
super.onItemRangeChanged(positionStart, itemCount)
this#EventDetails.binding.rvData.scrollToPosition(positionStart)
}
})
one of these method will work i think 1st one, try
and
try to avoid notifyDataSetChange() or intemChange() call by your self use DiffUtils or ListAdapter(Extension of recycler view)
This question already has answers here:
Android: Internet connectivity change listener
(12 answers)
How to set timer in android?
(24 answers)
Closed last year.
How can I make something in my Java constantly check for connectivity of internet I already have something that checks for connectivity of internet but do not know how to make it constant
Current connectivity checker
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork());
return capabilities != null && (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET));
} else {
NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
return activeNetwork != null && activeNetwork.isConnected();
}
}
return false;
}]
I have done it in Kotlin. If you face problem in converting Kotlin into Java you can ask me for help.
So, I am using Broadcast Receiver and detect whenever the network state changed and when there is no Internet, a dialogBox will show.
MainActivity.kt
class MainActivity : AppCompatActivity() {
private var br: BroadcastReceiver? = null
private var filter: IntentFilter? = null
private var mContext: MainActivity? = null
private var isPaused = false
private var toConfirm = true
private var noInternetBuilder: AlertDialog.Builder? = null
private var noInternetDialog: AlertDialog? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
br = MyBroadCastReceiver()
filter = IntentFilter()
filter?.addAction("android.net.conn.CONNECTIVITY_CHANGE")
mContext = this#MainActivity
mContext?.registerReceiver(br, filter)
}
/**
* Broadcast for Internet Checking
*
*/
inner class MyBroadCastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (!isNetworkStatusAvailable(this#MainActivity)) {
if (!isPaused) {
toConfirm = true
if (noInternetDialog != null) {
noInternetDialog?.dismiss()
}
showDialogNoInternet(this#MainActivity)
}
toConfirm = false
} else {
toConfirm = true
if (noInternetDialog != null) {
if (noInternetDialog!!.isShowing) {
noInternetDialog?.dismiss()
}
}
}
}
}
fun isNetworkStatusAvailable(context: Context): Boolean {
var result = false
val connectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val networkCapabilities = connectivityManager.activeNetwork ?: return false
val actNw =
connectivityManager.getNetworkCapabilities(networkCapabilities) ?: return false
result = when {
actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
actNw.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
actNw.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true
else -> false
}
} else {
#Suppress("DEPRECATION")
connectivityManager.run {
connectivityManager.activeNetworkInfo?.run {
result = when (type) {
ConnectivityManager.TYPE_WIFI -> true
ConnectivityManager.TYPE_MOBILE -> true
ConnectivityManager.TYPE_ETHERNET -> true
else -> false
}
}
}
}
return result
}
fun showDialogNoInternet(activity: Activity) {
noInternetBuilder = AlertDialog.Builder(activity)
val viewGroup = findViewById<ViewGroup>(android.R.id.content)
val dialogView: View = LayoutInflater.from(viewGroup.context)
.inflate(R.layout.dialog_nointernet, viewGroup, false)
noInternetBuilder!!.setView(dialogView)
noInternetDialog = noInternetBuilder!!.create()
noInternetDialog!!.setCancelable(false)
noInternetDialog!!.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
val btn = dialogView.findViewById<View>(R.id.btnOkInternet) as Button
btn.setOnClickListener {
resultInternetLauncher.launch(Intent(Settings.ACTION_WIFI_SETTINGS))
noInternetDialog?.dismiss()
}
noInternetDialog!!.show()
}
override fun onPause() {
super.onPause()
isPaused = true
}
override fun onResume() {
super.onResume()
isPaused = false
if (!toConfirm) {
if (noInternetDialog != null) {
noInternetDialog?.dismiss()
}
showDialogNoInternet(mContext!!)
}
}
}
Must implement override fun onPause() and override fun onResume() along with it because it will control the crashes of dialogBox.
I am new in Kotlin MVVM also, I tried to achieved Pagination with legacy approach and stucked in a issue with my RecyclerView, whenever I scroll it the data duplicated, I tried DiffUtils but no help.
I Logged the data in VIEWMODEL class the data is not repeating
but, when I logged in Activity where I am observing it is showing duplicate values
SEARCHRESULTACTIVITY.KT
class SearchResultActivity : AppCompatActivity() {
private lateinit var layoutManager: LinearLayoutManager
private lateinit var recyclerView: RecyclerView
private lateinit var pullAdapter: CustomAdapter
private var pageNumber = 1
private var totalItemsCount = 0
private var firstVisibleItemsCount = 0
private var visibleItemsCount = 0
private var previousTotal = 0
private var loading = true
private var fillPullList: ArrayList<RepoPull> = ArrayList()
private var userName: String = ""
private var repoName: String = ""
private var isEnd = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
initialize()
getDataPull(userName, repoName)
loadNextData()
}
private fun initialize() {
setContentView(R.layout.activity_search_result)
recyclerView = findViewById(R.id.repoRecView)
layoutManager = LinearLayoutManager(this)
getSearchQuery()
}
private fun getSearchQuery() {
userName = intent.getStringExtra("owner").toString()
repoName = intent.getStringExtra("repo").toString()
populatePullRv()
}
private fun populatePullRv() {
recyclerView.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
recyclerView.layoutManager = layoutManager
pullAdapter = CustomAdapter(this, fillPullList)
recyclerView.adapter = pullAdapter
progressBar.visibility = View.VISIBLE
}
private fun loadNextData() {
recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
val mLayoutManger = recyclerView.layoutManager as LinearLayoutManager
visibleItemsCount = mLayoutManger.childCount
totalItemsCount = mLayoutManger.itemCount
firstVisibleItemsCount = mLayoutManger.findFirstVisibleItemPosition()
if (loading) {
if (totalItemsCount > previousTotal) {
previousTotal = totalItemsCount
pageNumber++
loading = false
progressBar.visibility = View.GONE
}
}
if (!loading && (firstVisibleItemsCount + visibleItemsCount) >= totalItemsCount) {
getDataPull(userName, repoName)
loading = true
Log.d("PAGE", pageNumber.toString())
}
}
})
}
private fun getDataPull(username: String?, reponame: String?) {
val myViewModel = ViewModelProviders.of(this).get(PullVM::class.java)
myViewModel.endofList.observe(this, {
if (it == true) {
isEnd = true
progressBar.visibility = View.GONE
Toast.makeText(this#SearchResultActivity, "All PR Fetched", Toast.LENGTH_SHORT)
.show()
}
})
myViewModel.status.observe(this, {
if (it == false) {
showError(getString(R.string.no_net))
}
})
myViewModel.getPullDataFromVM().observe(this, {
if (it != null) {
listRepos(it) **//DUPLICATE VALUE COMING**
} else {
showError(getString(R.string.nothing_found))
}
})
myViewModel.getPullList(username.toString(), reponame.toString(), pageNumber)
}
private fun showError(s: String) {
progressBar.visibility = View.GONE
val theView =
this#SearchResultActivity.findViewById<View>(android.R.id.content)
Snackbar.make(
theView,
s,
Snackbar.LENGTH_LONG
).show()
}
#SuppressLint("NotifyDataSetChanged")
fun listRepos(repos: List<RepoPull>) {
if (!isEnd) {
progressBar.visibility = View.GONE
fillPullList.addAll(repos)
pullAdapter.notifyDataSetChanged()
}
}}
PULLVM(View Model).kt
class PullVM : ViewModel() {
var pullList: MutableLiveData<List<RepoPull>>
var status = MutableLiveData<Boolean?>()
var endofList = MutableLiveData<Boolean?>()
init {
pullList = MutableLiveData()
}
fun getPullDataFromVM(): MutableLiveData<List<RepoPull>> {
return pullList
}
fun getPullList(ownerName: String, repoName: String, pgNo: Int) {
val retriever = GitHubRetriever
val callback = object : Callback<List<RepoPull>> {
override fun onFailure(call: Call<List<RepoPull>>, t: Throwable) {
status.value = false
}
override fun onResponse(
call: Call<List<RepoPull>>,
response: Response<List<RepoPull>>
) {
if (response.body()?.size == 0) {
endofList.value = true
}
if (response.code() == 404) {
pullList.postValue(null)
} else {
status.value = true
val repos = response.body()
if (repos != null) {
pullList.postValue(repos)
}
}
}
}
retriever.userRepos(
callback,
ownerName,
repoName,
pgNo
)
}
Try moving your viewModel instantiation and observer settings to onCreate so you don't have to create a new viewModel instance and set a new observable to your LiveDatas.
Declare myViewModel as a lateinit property of your Activity and move this part to onCreate
myViewModel = ViewModelProviders.of(this).get(PullVM::class.java)
myViewModel.endofList.observe(this, {
if (it == true) {
isEnd = true
progressBar.visibility = View.GONE
Toast.makeText(this#SearchResultActivity, "All PR Fetched", Toast.LENGTH_SHORT)
.show()
}
})
myViewModel.status.observe(this, {
if (it == false) {
showError(getString(R.string.no_net))
}
})
myViewModel.getPullDataFromVM().observe(this, {
if (it != null) {
listRepos(it) **//DUPLICATE VALUE COMING**
} else {
showError(getString(R.string.nothing_found))
}
})
And
private fun getDataPull(username: String?, reponame: String?)
should only contain
myViewModel.getPullList(username.toString(), reponame.toString(), pageNumber)
I have been having this issue for a long time and have tried different suggestion on similar posts but all proved futile.
Currently, I have a recyclerview that implements double click for likes. Whenever I do that the recyclerview jumps to the top of the page.
Kindly advise on what to do to stop this.
Code
private fun setDataIntoAdapter(list: List<MovieEntity?>?) {
movieAdapter = MovieAdapter(list, object : MovieAdapter.OnMovieListener {
override fun onMovieDoubleClick(movieEntity: MovieEntity, view: View) {
val fav = view.findViewById<ImageView>(R.id.redFav)
// var favMovie = convertToFavourityEntity(movieEntity)
movieEntity.favourite = true
if (fav.visibility == View.GONE) {
fav.visibility = View.VISIBLE
CoroutineScope(Main).launch {
try {
insertAndUpdate(movieEntity)
FancyToast.makeText(
context,
"${movieEntity.title} is added to favourite",
FancyToast.LENGTH_LONG,
FancyToast.SUCCESS,
true
).show()
} catch (e: Exception) {
FancyToast.makeText(
context,
"Movie has been previously added \nto favorite",
FancyToast.LENGTH_LONG,
FancyToast.ERROR,
true
).show()
}
}
} else {
fav.visibility = View.GONE
movieEntity.favourite = false
CoroutineScope(Main).launch {
try {
deleteAndUpdate(movieEntity)
FancyToast.makeText(
context,
"${movieEntity.title} is removed from favourite",
FancyToast.LENGTH_LONG,
FancyToast.INFO,
true
).show()
} catch (e: Exception) {
// Toast.makeText(context, "Movie has been previously removed \nto favorite", Toast.LENGTH_SHORT).show()
val snackbar = Snackbar
.make(
view, "Movie has been previously removed \n" +
"to favorite", Snackbar.LENGTH_LONG
)
snackbar.show()
}
}
}
}
override fun onSingleClick(movieEntity: MovieEntity, view: View) {
gotoDetails(movieEntity)
// view.deleteIcon.setOnClickListener {
// singleDeletion(movieEntity)
// }
}
})
}
private suspend fun insertAndUpdate(movieEntity: MovieEntity) {
ServiceLocator.createLocalDataSource(context!!).movieDao?.update(movieEntity)
}
Adapter
class MovieAdapter<T>(private var movies:List<T?>?, private var listener:OnMovieListener):RecyclerView.Adapter<MovieAdapter.MovieHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MovieHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.movie_recycler_items, parent, false)
return MovieAdapter.MovieHolder(itemView)
}
override fun getItemCount(): Int {
return movies!!.size
}
fun setMovie(movies: List<T?>?){
this.movies = movies
notifyDataSetChanged()
}
fun getMovieAt(position: Int):T?{
return movies?.get(position)
}
override fun onBindViewHolder(holder: MovieHolder, position: Int) {
// covert.drawCornerFlag(holder)
movies?.let{
val currentMovies = it[position]
holder.bind(it[position]!!, listener)
}
}
class MovieHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
var title = itemView.findViewById<TextView>(R.id.title)
var releaseDate = itemView.findViewById<TextView>(R.id.releaseDate)
var ratingBar = itemView.findViewById<RatingBar>(R.id.ratingBar)
var imageThmbnail = itemView.findViewById<ImageView>(R.id.thumbnail)
var fav = itemView.findViewById<ImageView>(R.id.favourite)
var redFav = itemView.findViewById<ImageView>(R.id.redFav)
var rating = itemView.findViewById<TextView>(R.id.rating)
fun <T> bind(movieEntity: T, listener: OnMovieListener){
var i = 0
if(movieEntity is MovieEntity){
if(movieEntity.favourite){
redFav.visibility = View.VISIBLE
}
else{
redFav.visibility = View.GONE
}
val calendar = Calendar.getInstance()
val dateReleased = movieEntity.releaseDate?.split("-")
val year = dateReleased?.get(0)?.toInt()
val month = dateReleased?.get(1)?.toInt()
val day = dateReleased?.get(2)?.toInt()
var newDate:Date?= null
if (year != null) {
if (month != null) {
if (day != null) {
calendar.set(year, month,day)
}
}
newDate = calendar.time
}
val displayDate = newDate.toString().substring(4..7) + year.toString()
title.setText(movieEntity.title)
releaseDate.setText(displayDate)
ratingBar.numStars = 5
val ratingNum = movieEntity.rating?.toFloat()?.div(2)
rating.setText("${ratingNum!!}")
ratingBar.rating = ratingNum
Picasso.get().load(movieEntity.movieImage).into(imageThmbnail)
itemView.setOnClickListener(DoubleClick(object :DoubleClickListener{
override fun onDoubleClick(view: View?) {
Log.i("Dob", "Double clicked")
listener.onMovieDoubleClick(movieEntity, itemView)
}
override fun onSingleClick(view: View?) {
Log.i("click", "Single click")
listener.onSingleClick(movieEntity, itemView)
}
}))
}
}
}
interface OnMovieListener{
fun onMovieDoubleClick(movieEntity: MovieEntity, view:View)
fun onSingleClick(movieEntity: MovieEntity, view: View)
}
}
I want to pick first phone number automatically from HintRequest android.
Is there any possible solution for it?
is there any solution, user cannot dismiss the HintRequest Dialog without selecting at least one mobile number?
// Example From Kotlin
implementation 'com.google.android.gms:play-services-ads-identifier:17.0.0'
implementation 'com.google.android.gms:play-services-auth-api-phone:17.3.0'
implementation 'com.google.android.gms:play-services-base:17.1.0'
implementation 'com.google.android.gms:play-services-identity:17.0.0'
implementation 'com.google.android.gms:play-services-auth:17.0.0'
// Class : AutoDetectMobileNo
class AutoDetectMobileNo internal constructor(context: Context) {
private var googleApiClient: GoogleApiClient? = null
private val context: Context
private val appCompatActivity: AppCompatActivity = context as AppCompatActivity
fun requestPhoneNoHint() {
googleApiClient = GoogleApiClient.Builder(context)
.enableAutoManage(
appCompatActivity
) { }
.addApi(Auth.CREDENTIALS_API)
.build()
val hintRequest = HintRequest.Builder()
.setHintPickerConfig(
CredentialPickerConfig.Builder()
.setShowCancelButton(true)
.build()
)
.setPhoneNumberIdentifierSupported(true)
.build()
val intent =
Auth.CredentialsApi.getHintPickerIntent(googleApiClient, hintRequest)
try {
appCompatActivity.startIntentSenderForResult(
intent.intentSender,
RC_HINT,
null,
0,
0,
0
)
} catch (e: Throwable) {
Log.e("PHONE_HINT", "Could not start hint picker Intent", e)
}
}
fun getPhoneNo(data: Intent): String {
val cred: Credential =
data.getParcelableExtra(Credential.EXTRA_KEY)!!
return cred.id
}
interface Callback
companion object {
const val RC_HINT = 1000
}
init {
this.context = appCompatActivity.applicationContext
}
}
// On Your Activity / Fragment
val autoDetectMobileNo = AutoDetectMobileNo(this)
autoDetectMobileNo!!.requestPhoneNoHint()
override fun onActivityResult(
requestCode: Int,
resultCode: Int,
data: Intent?
) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == AutoDetectMobileNo.RC_HINT) {
if (resultCode == Activity.RESULT_OK) {
val mPhoneNumber = autoDetectMobileNo!!.getPhoneNo(data)
}
}
}