jaredrummler / Cyanea

A theme engine for Android
Apache License 2.0
1.45k stars 144 forks source link

How to use CyaneaDelegate in Java class? #31

Closed SourabhSNath closed 5 years ago

SourabhSNath commented 5 years ago

The java demo is incomplete?

jaredrummler commented 5 years ago

You can declare your activity to extend any of the activity classes that start with 'Cyanea' (e.g., CyaneaAppCompatActivity, CyaneaFragmentActivity).

public class MyActivity extends CyaneaAppCompatActivity {

}

If you can't extend your activity class, here is an example of what you'd need in your base class written in java:

import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.jaredrummler.cyanea.Cyanea;
import com.jaredrummler.cyanea.CyaneaResources;
import com.jaredrummler.cyanea.delegate.CyaneaDelegate;
import org.jetbrains.annotations.NotNull;

public class MyBaseActivity extends AppCompatActivity implements BaseCyaneaActivity {

  private CyaneaDelegate cyaneaDelegate;
  private CyaneaResources cyaneaResources;

  @Override protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(getCyaneaDelegate().wrap(newBase));
  }

  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    getCyaneaDelegate().onCreate(savedInstanceState);
    super.onCreate(savedInstanceState);
  }

  @Override public void onPostCreate(@Nullable Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    getCyaneaDelegate().onPostCreate(savedInstanceState);
  }

  @Override protected void onStart() {
    super.onStart();
    getCyaneaDelegate().onStart();
  }

  @Override protected void onResume() {
    super.onResume();
    getCyaneaDelegate().onResume();
  }

  @Override public boolean onCreateOptionsMenu(Menu menu) {
    getCyaneaDelegate().onCreateOptionsMenu(menu);
    return super.onCreateOptionsMenu(menu);
  }

  @Override public Resources getResources() {
    return getCyaneaResources();
  }

  @NotNull @Override public Cyanea getCyanea() {
    return Cyanea.getInstance();
  }

  @Override public int getThemeResId() {
    return 0;
  }

  private CyaneaDelegate getCyaneaDelegate() {
    if (cyaneaDelegate == null) {
      cyaneaDelegate = CyaneaDelegate.create(this, getCyanea(), getThemeResId());
    }
    return cyaneaDelegate;
  }

  private CyaneaResources getCyaneaResources() {
    if (cyaneaResources == null) {
      cyaneaResources = new CyaneaResources(super.getResources(), getCyanea());
    }
    return cyaneaResources;
  }

}
SourabhSNath commented 5 years ago

Thank you for the code!