Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@joseprl89
joseprl89 / MainActivity.kt
Created June 23, 2019 16:07
5 star rating view Android
package com.example.myapplication
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import com.example.myapplication.views.StarView
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
@joseprl89
joseprl89 / 5 star rating view.swift
Last active June 22, 2019 20:29
Rating view in Xcode10.1 playground
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
struct Star {
let radius: CGFloat
init(radius: CGFloat) {
self.radius = radius
@joseprl89
joseprl89 / ObserverWrapperActiveStateChanged.java
Created May 27, 2018 20:06
How activeStateChanged delays the delivery of events until the lifecycle is active for Internals of Android Architecture Components Part II- LiveData
void activeStateChanged(boolean newActive) {
if (newActive == mActive) {
return;
}
// immediately set active state, so we'd never dispatch anything to inactive
// owner
mActive = newActive;
boolean wasInactive = LiveData.this.mActiveCount == 0;
LiveData.this.mActiveCount += mActive ? 1 : -1;
if (wasInactive && mActive) {
@joseprl89
joseprl89 / SupportActivityOnCreate.java
Created May 27, 2018 19:11
How SupportActivity attaches a ReportFragment for Internals of Android Architecture Components Part II- LiveData
@RestrictTo(LIBRARY_GROUP)
public class SupportActivity extends Activity implements LifecycleOwner {
// ...
@Override
@SuppressWarnings("RestrictedApi")
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ReportFragment.injectIfNeededIn(this);
}
@joseprl89
joseprl89 / SupportActivityLifecycleOwner.java
Last active June 6, 2021 06:14
How SupportActicity implements LifecycleOwner for Internals of Android Architecture Components Part II- LiveData
public class SupportActivity extends Activity implements LifecycleOwner {
...
private LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this);
...
@Override
protected void onSaveInstanceState(Bundle outState) {
@joseprl89
joseprl89 / LifecycleBoundObserver.java
Last active May 27, 2018 18:39
How LifecycleBoundObserver and ObserverWrapper work for Internals of Android Architecture Components Part II- LiveData
class LifecycleBoundObserver extends ObserverWrapper implements GenericLifecycleObserver {
@NonNull final LifecycleOwner mOwner;
LifecycleBoundObserver(@NonNull LifecycleOwner owner, Observer<T> observer) {
super(observer);
mOwner = owner;
}
@Override
boolean shouldBeActive() {
@joseprl89
joseprl89 / MutableLiveDataSetValue.java
Last active May 27, 2018 18:25
How LiveData sets the value for Internals of Android Architecture Components Part II- LiveData
protected void setValue(T value) {
assertMainThread("setValue");
mVersion++;
mData = value;
dispatchingValue(null);
}
private void dispatchingValue(@Nullable ObserverWrapper initiator) {
if (mDispatchingValue) {
mDispatchInvalidated = true;
@joseprl89
joseprl89 / LiveDataObserve.java
Created May 27, 2018 18:12
How LiveData observes a value for Internals of Android Architecture Components Part II- LiveData
@MainThread
public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<T> observer) {
if (owner.getLifecycle().getCurrentState() == DESTROYED) {
// ignore
return;
}
LifecycleBoundObserver wrapper = new LifecycleBoundObserver(owner, observer);
ObserverWrapper existing = mObservers.putIfAbsent(observer, wrapper);
if (existing != null && !existing.isAttachedTo(owner)) {
throw new IllegalArgumentException("Cannot add the same observer"
@joseprl89
joseprl89 / LiveDataConsumption.java
Created May 27, 2018 18:06
How LiveData is consumed for Internals of Android Architecture Components Part II- LiveData
public class NameViewModel extends ViewModel {
private MutableLiveData<String> mCurrentName;
public MutableLiveData<String> getCurrentName() {
if (mCurrentName == null) {
mCurrentName = new MutableLiveData<String>();
}
return mCurrentName;
}
public void onDestroy() {
mCalled = true;
// Use mStateSaved instead of isStateSaved() since we're past onStop()
if (mViewModelStore != null && !mHost.mFragmentManager.mStateSaved) {
mViewModelStore.clear();
}
}