Skip to content

Instantly share code, notes, and snippets.

Avatar

Josep joseprl89

View GitHub Profile
@joseprl89
joseprl89 / MainActivity.kt
Created Jun 23, 2019
5 star rating view Android
View MainActivity.kt
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 Jun 22, 2019
Rating view in Xcode10.1 playground
View 5 star rating view.swift
//: 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
How activeStateChanged delays the delivery of events until the lifecycle is active for Internals of Android Architecture Components Part II- LiveData
View ObserverWrapperActiveStateChanged.java
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
How SupportActivity attaches a ReportFragment for Internals of Android Architecture Components Part II- LiveData
View SupportActivityOnCreate.java
@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 May 27, 2018
How SupportActicity implements LifecycleOwner for Internals of Android Architecture Components Part II- LiveData
View SupportActivityLifecycleOwner.java
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
How LifecycleBoundObserver and ObserverWrapper work for Internals of Android Architecture Components Part II- LiveData
View LifecycleBoundObserver.java
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
How LiveData sets the value for Internals of Android Architecture Components Part II- LiveData
View MutableLiveDataSetValue.java
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
How LiveData observes a value for Internals of Android Architecture Components Part II- LiveData
View LiveDataObserve.java
@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
How LiveData is consumed for Internals of Android Architecture Components Part II- LiveData
View LiveDataConsumption.java
public class NameViewModel extends ViewModel {
private MutableLiveData<String> mCurrentName;
public MutableLiveData<String> getCurrentName() {
if (mCurrentName == null) {
mCurrentName = new MutableLiveData<String>();
}
return mCurrentName;
}
View FragmentActivityOnDestroy.java
public void onDestroy() {
mCalled = true;
// Use mStateSaved instead of isStateSaved() since we're past onStop()
if (mViewModelStore != null && !mHost.mFragmentManager.mStateSaved) {
mViewModelStore.clear();
}
}
You can’t perform that action at this time.