mirror of https://github.com/FongMi/TV.git
parent
9f2ac9b89a
commit
e64d65b5c9
@ -0,0 +1,78 @@ |
||||
package com.fongmi.android.tv.ui.base; |
||||
|
||||
import android.app.Activity; |
||||
import android.graphics.drawable.Drawable; |
||||
import android.os.Bundle; |
||||
import android.view.View; |
||||
|
||||
import androidx.appcompat.app.AppCompatActivity; |
||||
import androidx.viewbinding.ViewBinding; |
||||
|
||||
import com.fongmi.android.tv.R; |
||||
import com.fongmi.android.tv.api.WallConfig; |
||||
import com.fongmi.android.tv.event.RefreshEvent; |
||||
import com.fongmi.android.tv.utils.FileUtil; |
||||
import com.fongmi.android.tv.utils.Prefers; |
||||
import com.fongmi.android.tv.utils.ResUtil; |
||||
|
||||
import org.greenrobot.eventbus.EventBus; |
||||
import org.greenrobot.eventbus.Subscribe; |
||||
import org.greenrobot.eventbus.ThreadMode; |
||||
|
||||
import java.io.File; |
||||
|
||||
public abstract class BaseActivity extends AppCompatActivity { |
||||
|
||||
protected abstract ViewBinding getBinding(); |
||||
|
||||
@Override |
||||
protected void onCreate(Bundle savedInstanceState) { |
||||
super.onCreate(savedInstanceState); |
||||
setContentView(getBinding().getRoot()); |
||||
EventBus.getDefault().register(this); |
||||
setWall(); |
||||
initView(savedInstanceState); |
||||
initEvent(); |
||||
} |
||||
|
||||
protected Activity getActivity() { |
||||
return this; |
||||
} |
||||
|
||||
protected void initView(Bundle savedInstanceState) { |
||||
} |
||||
|
||||
protected void initEvent() { |
||||
} |
||||
|
||||
protected boolean isVisible(View view) { |
||||
return view.getVisibility() == View.VISIBLE; |
||||
} |
||||
|
||||
protected boolean isGone(View view) { |
||||
return view.getVisibility() == View.GONE; |
||||
} |
||||
|
||||
private void setWall() { |
||||
try { |
||||
File file = FileUtil.getWall(Prefers.getWall()); |
||||
if (file.exists() && file.length() > 0) getWindow().setBackgroundDrawable(WallConfig.drawable(Drawable.createFromPath(file.getPath()))); |
||||
else getWindow().setBackgroundDrawableResource(ResUtil.getDrawable(file.getName())); |
||||
} catch (Exception e) { |
||||
getWindow().setBackgroundDrawableResource(R.drawable.wallpaper_1); |
||||
} |
||||
} |
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN) |
||||
public void onRefreshEvent(RefreshEvent event) { |
||||
if (event.getType() != RefreshEvent.Type.WALL) return; |
||||
WallConfig.get().setDrawable(null); |
||||
setWall(); |
||||
} |
||||
|
||||
@Override |
||||
protected void onDestroy() { |
||||
super.onDestroy(); |
||||
EventBus.getDefault().unregister(this); |
||||
} |
||||
} |
||||
@ -0,0 +1,53 @@ |
||||
package com.fongmi.android.tv.ui.base; |
||||
|
||||
import android.os.Bundle; |
||||
import android.view.LayoutInflater; |
||||
import android.view.View; |
||||
import android.view.ViewGroup; |
||||
|
||||
import androidx.annotation.NonNull; |
||||
import androidx.annotation.Nullable; |
||||
import androidx.fragment.app.Fragment; |
||||
import androidx.viewbinding.ViewBinding; |
||||
|
||||
public abstract class BaseFragment extends Fragment { |
||||
|
||||
protected abstract ViewBinding getBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup container); |
||||
|
||||
private boolean init; |
||||
|
||||
@Nullable |
||||
@Override |
||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { |
||||
return getBinding(inflater, container).getRoot(); |
||||
} |
||||
|
||||
protected void initView() { |
||||
} |
||||
|
||||
protected void initEvent() { |
||||
} |
||||
|
||||
public boolean canBack() { |
||||
return true; |
||||
} |
||||
|
||||
private void resume() { |
||||
if (init) return; |
||||
initView(); |
||||
initEvent(); |
||||
init = true; |
||||
} |
||||
|
||||
@Override |
||||
public void setUserVisibleHint(boolean isVisibleToUser) { |
||||
super.setUserVisibleHint(isVisibleToUser); |
||||
if (isVisibleToUser) if (isResumed()) resume(); |
||||
} |
||||
|
||||
@Override |
||||
public void onResume() { |
||||
super.onResume(); |
||||
if (getUserVisibleHint()) resume(); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue