Vivek Modi
07/18/2022, 12:08 PMprivate set
in here ?
var number : Int by mutableStateOf(0)
private set
loloof64
07/19/2022, 3:44 PMAli Khaleqi Yekta
07/19/2022, 6:36 PMandroidMain
)_; I'd be really happy to even get a clue of what to touch to make this work. I tried countless ways but nothing worked: https://stackoverflow.com/questions/73006307/gradle-sync-cannot-find-a-variant-matching-build-type-null-and-product-flavoMobile Dev.
07/20/2022, 9:36 PMwindowManager = getSystemService(WINDOW_SERVICE) as WindowManager
val inflater = baseContext.getSystemService(LAYOUT_INFLATER_SERVICE) as LayoutInflater
floatView = inflater.inflate(R.layout.floating_layout, null) as ViewGroup
webView = floatView.findViewById(R.id.webView)!!
LAYOUT_TYPE = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
} else {
WindowManager.LayoutParams.TYPE_TOAST
}
floatWindowLayoutParams = WindowManager.LayoutParams(
150.toPx(baseContext),
200.toPx(baseContext),
LAYOUT_TYPE!!,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT
)
floatWindowLayoutParams.gravity = Gravity.CENTER
floatWindowLayoutParams.x = 0
floatWindowLayoutParams.y = 0
windowManager.addView(floatView, floatWindowLayoutParams)
and when i want to start the view
btnWebView.setOnClickListener {
startService(Intent(this, FloatingWindowApp::class.java))
}
Can you help me ?Vivek Modi
07/21/2022, 7:58 PMSerializer has not been found for type 'ZonedDateTime'. To use context serializer as fallback, explicitly annotate type or property with @Contextual
@Serializable
@Parcelize
data class NearestResult(
val day: ZonedDateTime,
val event: String
) : Parcelable
Derek Alves
07/22/2022, 6:25 PMPrayansh Srivastava
07/22/2022, 7:03 PMspierce7
07/22/2022, 8:55 PMFlow
to watch the data changes from within the ViewModels, and then functions to pass events into the ViewModels, and to start loading etc.
How should errors be handled when a network request fails? Should it be part of the observed Flow type, or is there a better way to handle this?Jasmin Fajkic
07/23/2022, 6:39 PM@EntryPoint
@InstallIn(ActivityComponent::class)
interface ViewModelFactoryProvider {
fun communityFeedViewModelFactory(): CommunityFeedViewModel.Factory
fun postTileViewModelFactory(): PostTileViewModel.Factory
fun reactionViewModelFactory(): ReactionsViewModel.Factory
fun commentViewModelFactory(): CommentViewModel.Factory
}
Here is for example how i use it in ReactionsViewModel
class ReactionsViewModel
@AssistedInject
constructor(
@Assisted private var reactions: PostReactions,
@Assisted private var postId: String,
private var reactionsRepository: ReactionsRepository,
) : ViewModel() {
@AssistedFactory
interface Factory {
fun create(
@Assisted reactions: PostReactions,
@Assisted postId: String,
): ReactionsViewModel
}
@Suppress("UNCHECKED_CAST")
companion object {
fun provideFactory(
assistedFactory: Factory,
reactions: PostReactions,
postId: String
): ViewModelProvider.Factory = object : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return assistedFactory.create(reactions, postId) as T
}
}
}
}
@Composable
fun reactionsViewModel(
reactions: PostReactions,
id: String
): ReactionsViewModel {
val factory = EntryPointAccessors.fromActivity(
LocalContext.current as Activity,
ViewModelFactoryProvider::class.java
).reactionViewModelFactory()
return viewModel(
factory = ReactionsViewModel.provideFactory(
factory,
reactions,
postId = id,
), key = id
)
}
And here is how I provide reactions respository:
@Module
@InstallIn(ActivityComponent::class)
object ReactionsModule {
@Provides
fun provideReactionApi(retrofit: Retrofit):
ReactionsApi = retrofit.create(ReactionsApi::class.java)
@Provides
fun providesRRepository(ReactionsApi: ReactionsApi):
ReactionsRepository =
ReactionsRepositoryImpl(ReactionsApi)
}
Problem here is that I need to use ActivityComponent in module where I provide repository. If i try to access this reactions repository in any other viewmodel it crash because it needs to be ViewModelComponent. Is there any clever solution how to fix it?loloof64
07/23/2022, 7:56 PMI/System.out: referenceTable GDEF length=778 1
I/System.out: referenceTable GSUB length=6388 1
I/System.out: referenceTable GPOS length=66796 1
I/System.out: referenceTable head length=54 1
Asad Mukhtar
07/23/2022, 7:56 PMgalex
07/24/2022, 3:23 PMokhttp
and I didn't find a better place to ask this 😊
We're using okhttp
with Retrofit
and Apollo
, and I'd like to get the full http request right before it is really being executed.
I can get the Call.request()
to get the request object but this one doesn't contain the headers we're adding through interceptors for example...
Is there any extension to get the real request? Or am I correct to assume I can't use interceptors if I want to achieve this?Kareem Waleed
07/25/2022, 6:48 AMproguard
-keep public class com.example.library.ExtensionsKt{*;}
but this only made using the function ExtensionsKt.doSomething()
possible but not Model.doSomething()
Any ideas ?yousef shaaban
07/25/2022, 9:28 AMArtem Bezlyubchenko
07/25/2022, 1:58 PMexplicitNulls = false
, but I have 1 request, where I need to keep nulls…
Is there a way to keep nulls for this particular request without creating a separate client for this?
Thanks!Gaurav
07/25/2022, 4:06 PMoverride fun onBackPressed() {
if (supportFragmentManager.backStackEntryCount <= 1) {
finish()
} else {
supportFragmentManager.popBackStack()
}
}
now sometime i am getting this exception
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
this may be happening bcoz state is getting saved and then popBackStack() is called...
need help...how to handle this situation...i can put a check isStateSaved or not....but i want to handle those cases also where state is saved and user clicked on back press....What to do in such scenario...How to do back press handling when state is saved??
please suggest some solution..
Thanks a lot in advance.:thank-you:Tarun Deep Attri
07/26/2022, 8:42 PMShashi K
07/27/2022, 2:54 AMManuel Lorenzo
07/27/2022, 9:40 AM@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
private val placesViewModel: PlacesViewModel by viewModels()
private val locationPermissionRequest =
registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { permissionGranted ->
when {
permissionGranted -> placesViewModel.getNearbyPlaces(locationPermissionGiven = permissionGranted)
else -> showLocationPermissionDeniedExplanationDialog(permissionGranted)
}
}
// region Activity lifecycle
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
val adapter = PlacesAdapter()
val recyclerView = findViewById<RecyclerView>(R.id.placesRecyclerView)
val loadingProgressIndicator =
findViewById<CircularProgressIndicator>(R.id.loadingProgressIndicator)
recyclerView.adapter = adapter
lifecycleScope.launch {
observeChanges(loadingProgressIndicator, adapter)
}
checkForLocationPermissionsAndHandleResult()
}
private suspend fun observeChanges(
loadingProgressIndicator: CircularProgressIndicator,
adapter: PlacesAdapter
) {
placesViewModel.uiState.collect {
when {
it.loading -> loadingProgressIndicator.visibility = View.VISIBLE
it.places?.isNotEmpty() == true -> {
loadingProgressIndicator.visibility = View.GONE
adapter.submitList(it.places)
}
}
}
}
// endregion
// region Location permission related methods
/**
* Shows a dialog explaining that the [Manifest.permission.ACCESS_FINE_LOCATION] has been denied
* and that a default location will be used in order to fetch the nearby places.
*/
private fun showLocationPermissionDeniedExplanationDialog(permissionGranted: Boolean) {
AlertDialog.Builder(this)
.setMessage(getString(R.string.permission_denied_dialog_message))
.setPositiveButton(getString(R.string.permission_denied_dialog_positive_button_text)) { _, _ ->
placesViewModel.getNearbyPlaces(locationPermissionGiven = permissionGranted)
}
.show()
}
/**
* Check for the [Manifest.permission.ACCESS_FINE_LOCATION] permission.
* If the permission is granted, fetch the user's nearby places with [placesViewModel.getNearbyPlaces()].
* Otherwise, show a dialog explaining that because of denying the location permission, a default location will be used.
*/
private fun checkForLocationPermissionsAndHandleResult() {
when {
ContextCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
) == PackageManager.PERMISSION_GRANTED -> {
placesViewModel.getNearbyPlaces(locationPermissionGiven = true)
}
shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION) -> {
AlertDialog.Builder(this)
.setMessage(getString(R.string.permission_dialog_message_text))
.setNegativeButton(getString(R.string.permission_dialog_negative_button_text)) { dialog, _ ->
dialog.dismiss()
placesViewModel.getNearbyPlaces(locationPermissionGiven = false)
}
.setPositiveButton(getString(R.string.permission_dialog_positive_button_text)) { dialog, _ ->
locationPermissionRequest.launch(Manifest.permission.ACCESS_FINE_LOCATION)
dialog.dismiss()
placesViewModel.getNearbyPlaces(locationPermissionGiven = true)
}.show()
}
else -> {
locationPermissionRequest.launch(Manifest.permission.ACCESS_FINE_LOCATION)
}
}
}
// endregion
}
Manuel Lorenzo
07/27/2022, 9:41 AMlocationPermissionRequest
is generated again, to check for the permissions, so the VM is triggered againoday
07/27/2022, 1:44 PMstartActivityForResult
?
I have an Activity A that I start a Fragment A-2 inside, and that fragment A-2 starts another activity B, I need activity B to send back the result all the way to activity A using this new registerActivityForResult wayLandry Norris
07/27/2022, 9:21 PMalthaf
07/28/2022, 7:07 AMvar messageStyle: MessageStyle = MessageStyle.Large
set(value) {
var textStyle = 0
var iconSize = 0
var padding = 0
when (value) {
MessageStyle.Small -> {
textStyle = R.style.TextAppearance_Sc_Components_Body2_Regular
iconSize = context.resources.getDimensionPixelSize(R.dimen.icon_size_small)
padding = R.dimen.sc_space_xs
}
else -> {
textStyle = R.style.TextAppearance_Sc_Components_Body1_Regular
iconSize = context.resources.getDimensionPixelSize(R.dimen.icon_size_regular)
padding = R.dimen.sc_space_xs
}
}
tvMessage.setTextAppearance(textStyle)
ivMessageVariantIcon.layoutParams = ViewGroup.LayoutParams(iconSize,iconSize);
setPaddingRelative(padding, padding, padding, padding)
field = value
}
Chetan Tuteja
07/28/2022, 8:38 AMems
property in Jetpack Compose?KotlinLeaner
07/28/2022, 8:57 AMcompanion object {
const val TABLE_USER_ATTRIBUTE_EMPID = "_id"
const val TABLE_USER_ATTRIBUTE_DATA = "data"
}
vs
object DbConstants {
const val TABLE_USER_ATTRIBUTE_EMPID = "_id"
const val TABLE_USER_ATTRIBUTE_DATA = "data"
}
which is one recommended way ?juliocbcotta
07/28/2022, 9:36 AMHarsh P.
07/28/2022, 9:16 PMAsad Mukhtar
07/29/2022, 12:28 PMVivekpanchal64
07/29/2022, 3:19 PMAmrJyniat
07/30/2022, 11:11 AMfindOr
fun like find{}
in Kotlin collections but let me do another thing if the result was null?AmrJyniat
07/30/2022, 11:11 AMfindOr
fun like find{}
in Kotlin collections but let me do another thing if the result was null?Paul Woitaschek
07/30/2022, 1:13 PMusers.findOrElse({ it.name == "Alice" }) {
Person("alice")
}
AmrJyniat
07/30/2022, 1:22 PMval rightDevice = devices.find { it is AudioDevice.BluetoothHeadset
} ?:
devices.find { it is AudioDevice.WiredHeadset
} ?:
devices.find { it is AudioDevice.Earpiece }
Trying to make this code a bit cleanerPaul Woitaschek
07/30/2022, 1:27 PMfun <T : Any> List<T>.multiFind(vararg predicates: (T) -> Boolean): T? {
predicates.forEach { predicate ->
find(predicate)?.let { return it }
}
return null
}
😉devices.maxByOrNull {
when(it) {
is bluetooth -> 3
is wired -> 2
is earpiece -> 1
}
}
Zun
07/30/2022, 8:33 PMPaul Woitaschek
07/31/2022, 8:14 AMhfhbd
07/31/2022, 11:18 AMvar wired: Device? = null
var earpiece: Device? = null
for (device in devices) {
if (device is Bluetooth) { return device }
if (device is Wired && wired == null) { wired = device }
if (device is EarPiece && earpice == null) { earpiece = device }
}
return wired ?: earpiece
Even better with a sealed class and when