dev
This commit is contained in:
@@ -2,6 +2,12 @@ package com.mogo.commons;
|
||||
|
||||
import android.app.Application;
|
||||
|
||||
import com.alibaba.android.arouter.launcher.ARouter;
|
||||
import com.elegant.analytics.Analytics;
|
||||
import com.elegant.analytics.AnalyticsConfig;
|
||||
import com.mogo.commons.debug.DebugConfig;
|
||||
import com.mogo.utils.TipToast;
|
||||
|
||||
/**
|
||||
* @author congtaowang
|
||||
* @since 2019-12-23
|
||||
@@ -12,13 +18,36 @@ public class AbsMogoApplication extends Application {
|
||||
|
||||
private static Application sApp;
|
||||
|
||||
public static Application getApp() {
|
||||
return sApp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
sApp = this;
|
||||
init();
|
||||
}
|
||||
|
||||
public static Application getApp() {
|
||||
return sApp;
|
||||
protected void init() {
|
||||
|
||||
// 初始化埋点
|
||||
Analytics.getInstance().start( sApp );
|
||||
// 0 - debug 近实时上报,积累一条埋点上报,或者积累3秒上报一次。
|
||||
// 2 - 本地缓存,聚合上报,积累30条埋点上报,或者积累60秒上报一次。
|
||||
Analytics.getInstance().start( sApp );
|
||||
AnalyticsConfig.getInstance( sApp ).setMode( DebugConfig.isDebug() ? 0 : 2 );
|
||||
AnalyticsConfig.getInstance( sApp ).shouldLog( DebugConfig.isDebug() );
|
||||
|
||||
// 初始化 arouter
|
||||
if ( DebugConfig.isDebug() ) {
|
||||
ARouter.openDebug();
|
||||
ARouter.openLog();
|
||||
}
|
||||
ARouter.init( sApp );
|
||||
|
||||
// 初始化toast
|
||||
TipToast.init( sApp, null );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.mogo.commons.analytics;
|
||||
|
||||
import com.elegant.analytics.Analytics;
|
||||
import com.mogo.commons.network.ParamUtils;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author congtaowang
|
||||
* @since 2019-12-23
|
||||
* <p>
|
||||
* 埋点
|
||||
*/
|
||||
public class AnalyticsUtils {
|
||||
|
||||
/**
|
||||
* 埋点
|
||||
*
|
||||
* @param event 事件名称
|
||||
* @param properties 事件参数
|
||||
*/
|
||||
public static void track( String event, Map< String, Object > properties ) {
|
||||
Analytics.getInstance().setCustomParams( ParamUtils.getAnalyticsParameters() );
|
||||
Analytics.getInstance().track( event, properties );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.mogo.commons.debug;
|
||||
|
||||
/**
|
||||
* @author congtaowang
|
||||
* @since 2019-12-23
|
||||
* <p>
|
||||
* 各个莫快递调试信息控制接口
|
||||
*/
|
||||
public class DebugConfig {
|
||||
|
||||
private static boolean sDebug = true;
|
||||
|
||||
public static boolean isDebug() {
|
||||
return sDebug;
|
||||
}
|
||||
|
||||
public static void setDebug( boolean sDebug ) {
|
||||
DebugConfig.sDebug = sDebug;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.mogo.commons.mvp;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
/**
|
||||
* @author congtaowang
|
||||
* @since 2019-12-23
|
||||
* <p>
|
||||
* mvp的view接口
|
||||
*/
|
||||
public interface IView {
|
||||
|
||||
Context getContext();
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.mogo.commons.mvp;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
/**
|
||||
* @author congtaowang
|
||||
* @since 2019-12-23
|
||||
* <p>
|
||||
* mvp 的 activity
|
||||
*/
|
||||
public abstract class MvpActivity< V extends IView, P extends Presenter< V > >
|
||||
extends AppCompatActivity implements IView {
|
||||
|
||||
protected P mPresenter;
|
||||
|
||||
@Override
|
||||
protected void onCreate( @Nullable Bundle savedInstanceState ) {
|
||||
super.onCreate( savedInstanceState );
|
||||
setContentView( getLayoutId() );
|
||||
initViews();
|
||||
mPresenter = createPresenter();
|
||||
getLifecycle().addObserver( mPresenter );
|
||||
}
|
||||
|
||||
/**
|
||||
* 布局资源
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
protected abstract int getLayoutId();
|
||||
|
||||
/**
|
||||
* 初始化控件,必须在初始化完成之后才可以实例化presenter,避免
|
||||
* presenter 生命周期错乱
|
||||
*/
|
||||
protected abstract void initViews();
|
||||
|
||||
/**
|
||||
* 创建 presenter 实例
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@NonNull
|
||||
protected abstract P createPresenter();
|
||||
|
||||
@Override
|
||||
public Context getContext() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
getLifecycle().removeObserver( mPresenter );
|
||||
if ( mPresenter != null ) {
|
||||
getLifecycle().removeObserver( mPresenter );
|
||||
}
|
||||
mPresenter = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package com.mogo.commons.mvp;
|
||||
|
||||
import android.content.Context;
|
||||
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;
|
||||
|
||||
/**
|
||||
* @author congtaowang
|
||||
* @since 2019-12-23
|
||||
* <p>
|
||||
* mvp fragment
|
||||
*/
|
||||
public abstract class MvpFragment< V extends IView, P extends Presenter< V > > extends Fragment implements IView {
|
||||
|
||||
private Context mContext;
|
||||
protected P mPresenter;
|
||||
private View mRootView;
|
||||
|
||||
@Override
|
||||
public void onAttach( Context context ) {
|
||||
super.onAttach( context );
|
||||
mContext = context;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView( @NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState ) {
|
||||
return inflater.inflate( getLayoutId(), container, false );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated( @NonNull View view, @Nullable Bundle savedInstanceState ) {
|
||||
super.onViewCreated( view, savedInstanceState );
|
||||
mRootView = view;
|
||||
}
|
||||
|
||||
/**
|
||||
* 布局资源
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
protected abstract int getLayoutId();
|
||||
|
||||
@Override
|
||||
public void onActivityCreated( @Nullable Bundle savedInstanceState ) {
|
||||
super.onActivityCreated( savedInstanceState );
|
||||
initViews();
|
||||
mPresenter = createPresenter();
|
||||
getViewLifecycleOwner().getLifecycle().addObserver( mPresenter );
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化控件,必须在初始化完成之后才可以实例化presenter,避免
|
||||
* presenter 生命周期错乱
|
||||
*/
|
||||
protected abstract void initViews();
|
||||
|
||||
/**
|
||||
* 创建 presenter 实例
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@NonNull
|
||||
protected abstract P createPresenter();
|
||||
|
||||
@Nullable
|
||||
protected < T extends View > T findViewById( int id ) {
|
||||
if ( mRootView != null ) {
|
||||
return ( T ) mRootView.findViewById( id );
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public Context getContext() {
|
||||
return mContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyView() {
|
||||
super.onDestroyView();
|
||||
if ( mPresenter != null ) {
|
||||
getViewLifecycleOwner().getLifecycle().removeObserver( mPresenter );
|
||||
}
|
||||
mPresenter = null;
|
||||
mRootView = null;
|
||||
mContext = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.mogo.commons.mvp;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.lifecycle.Lifecycle;
|
||||
import androidx.lifecycle.LifecycleObserver;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
import androidx.lifecycle.OnLifecycleEvent;
|
||||
|
||||
/**
|
||||
* @author congtaowang
|
||||
* @since 2019-12-23
|
||||
* <p>
|
||||
* mvp 的 presenter
|
||||
*/
|
||||
public abstract class Presenter< V extends IView > implements LifecycleObserver {
|
||||
|
||||
private V mView;
|
||||
|
||||
public Presenter( V view ) {
|
||||
this.mView = view;
|
||||
}
|
||||
|
||||
@OnLifecycleEvent( Lifecycle.Event.ON_CREATE )
|
||||
public void onCreate( @NonNull LifecycleOwner owner ) {
|
||||
}
|
||||
|
||||
@OnLifecycleEvent( Lifecycle.Event.ON_START )
|
||||
public void onStart( @NonNull LifecycleOwner owner ) {
|
||||
}
|
||||
|
||||
@OnLifecycleEvent( Lifecycle.Event.ON_RESUME )
|
||||
public void onResume( @NonNull LifecycleOwner owner ) {
|
||||
}
|
||||
|
||||
@OnLifecycleEvent( Lifecycle.Event.ON_PAUSE )
|
||||
public void onPause( @NonNull LifecycleOwner owner ) {
|
||||
}
|
||||
|
||||
@OnLifecycleEvent( Lifecycle.Event.ON_STOP )
|
||||
public void onStop( @NonNull LifecycleOwner owner ) {
|
||||
}
|
||||
|
||||
@OnLifecycleEvent( Lifecycle.Event.ON_DESTROY )
|
||||
public void onDestroy( @NonNull LifecycleOwner owner ) {
|
||||
}
|
||||
|
||||
@OnLifecycleEvent( Lifecycle.Event.ON_ANY )
|
||||
public void onLifecycleChanged(
|
||||
@NonNull LifecycleOwner owner,
|
||||
@NonNull Lifecycle.Event event ) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.mogo.commons.network;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author congtaowang
|
||||
* @since 2019-12-23
|
||||
* <p>
|
||||
* 描述
|
||||
*/
|
||||
public class ParamUtils {
|
||||
|
||||
public static Map< String, Object > getAnalyticsParameters() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user