dev
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
package com.mogo.module.main;
|
||||
|
||||
import android.view.MotionEvent;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import com.alibaba.android.arouter.launcher.ARouter;
|
||||
import com.mogo.commons.mvp.MvpActivity;
|
||||
import com.mogo.map.listener.IMogoMapListener;
|
||||
import com.mogo.map.model.MogoPoi;
|
||||
import com.mogo.module.main.cards.MogoModulesHandler;
|
||||
import com.mogo.module.main.cards.MogoModulesManager;
|
||||
import com.mogo.service.MogoServicePaths;
|
||||
import com.mogo.service.map.IMogoMapService;
|
||||
import com.mogo.service.module.IMogoModuleProvider;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* @author congtaowang
|
||||
* @since 2019-12-23
|
||||
* <p>
|
||||
* 描述
|
||||
*/
|
||||
public class MainActivity extends MvpActivity< MainView, MainPresenter > implements MainView {
|
||||
|
||||
IMogoMapService ims;
|
||||
MogoModulesHandler handler;
|
||||
|
||||
@Override
|
||||
protected int getLayoutId() {
|
||||
return R.layout.module_main_activity_main;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initViews() {
|
||||
|
||||
handler = new MogoModulesManager( this );
|
||||
ims = ( IMogoMapService ) ARouter.getInstance().build( MogoServicePaths.PATH_SERVICES_MAP ).navigation();
|
||||
if ( ims != null ) {
|
||||
ims.registerHostMapListener( handler );
|
||||
}
|
||||
handler.loadMap( R.id.module_main_id_fragment_container );
|
||||
|
||||
Collection< IMogoModuleProvider > providers = handler.loadCards();
|
||||
for ( IMogoModuleProvider provider : providers ) {
|
||||
if ( provider == null ) {
|
||||
continue;
|
||||
}
|
||||
if ( provider.getType() == IMogoModuleProvider.TYPE_FRAGMENT ) {
|
||||
final Fragment fragment = provider.createFragment( this, null );
|
||||
if ( fragment != null ) {
|
||||
getSupportFragmentManager().beginTransaction()
|
||||
.add( R.id.module_main_id_fragment_container, fragment )
|
||||
.commitAllowingStateLoss();
|
||||
}
|
||||
}
|
||||
}
|
||||
handler.setEnable( providers.iterator().next().getModuleName() );
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
protected MainPresenter createPresenter() {
|
||||
return new MainPresenter( this );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.mogo.module.main;
|
||||
|
||||
import com.mogo.commons.mvp.Presenter;
|
||||
|
||||
/**
|
||||
* @author congtaowang
|
||||
* @since 2019-12-23
|
||||
* <p>
|
||||
* 描述
|
||||
*/
|
||||
public class MainPresenter extends Presenter< MainView > {
|
||||
|
||||
public MainPresenter( MainView view ) {
|
||||
super( view );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.mogo.module.main;
|
||||
|
||||
import com.mogo.commons.mvp.IView;
|
||||
|
||||
/**
|
||||
* @author congtaowang
|
||||
* @since 2019-12-23
|
||||
* <p>
|
||||
* 主页 view 接口
|
||||
*/
|
||||
public interface MainView extends IView {
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.mogo.module.main.cards;
|
||||
|
||||
import com.mogo.map.listener.IMogoMapListener;
|
||||
import com.mogo.service.module.IMogoModuleProvider;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* @author congtaowang
|
||||
* @since 2019-12-24
|
||||
* <p>
|
||||
* 卡片管理
|
||||
*/
|
||||
public interface MogoModulesHandler extends IMogoMapListener {
|
||||
|
||||
/**
|
||||
* 加载卡片
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
Collection< IMogoModuleProvider > loadCards();
|
||||
|
||||
/**
|
||||
* 加载小智语音
|
||||
*
|
||||
* @param containerId 容器id
|
||||
*/
|
||||
void loadAIAssist( int containerId );
|
||||
|
||||
/**
|
||||
* 加载天气
|
||||
*
|
||||
* @param containerId 容器id
|
||||
*/
|
||||
void loadWeather( int containerId );
|
||||
|
||||
/**
|
||||
* 加载地图
|
||||
*
|
||||
* @param containerId 容器id
|
||||
*/
|
||||
void loadMap( int containerId );
|
||||
|
||||
/**
|
||||
* 设置某一个module可用
|
||||
*
|
||||
* @param module
|
||||
*/
|
||||
void setEnable( String module );
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
package com.mogo.module.main.cards;
|
||||
|
||||
import android.content.Context;
|
||||
import android.text.TextUtils;
|
||||
import android.view.MotionEvent;
|
||||
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import com.alibaba.android.arouter.launcher.ARouter;
|
||||
import com.mogo.map.listener.IMogoMapListener;
|
||||
import com.mogo.map.model.MogoPoi;
|
||||
import com.mogo.module.common.MogoModulePaths;
|
||||
import com.mogo.module.main.MainActivity;
|
||||
import com.mogo.service.module.IMogoModuleProvider;
|
||||
import com.mogo.utils.ResourcesHelper;
|
||||
import com.mogo.utils.logger.Logger;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author congtaowang
|
||||
* @since 2019-12-24
|
||||
* <p>
|
||||
* 卡片加载
|
||||
*/
|
||||
public class MogoModulesManager implements MogoModulesHandler, IMogoMapListener {
|
||||
|
||||
private static final String TAG = "MogoModulesManager";
|
||||
|
||||
private MainActivity mActivity;
|
||||
private final Map< String, IMogoModuleProvider > mCardProviders = new HashMap<>();
|
||||
private IMogoModuleProvider mMapProvider;
|
||||
private String mEnableModuleName = null;
|
||||
|
||||
public MogoModulesManager( MainActivity activity ) {
|
||||
if ( activity == null ) {
|
||||
throw new NullPointerException( "activity can't be null." );
|
||||
}
|
||||
this.mActivity = activity;
|
||||
}
|
||||
|
||||
private Context getContext() {
|
||||
return mActivity;
|
||||
}
|
||||
|
||||
private Context getApplicationContext() {
|
||||
return mActivity.getApplicationContext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection< IMogoModuleProvider > loadCards() {
|
||||
IMogoModuleProvider demo = load( MogoModulePaths.PATH_MODULE_DEMO );
|
||||
mCardProviders.put( demo.getModuleName(), demo );
|
||||
return mCardProviders.values();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadAIAssist( int containerId ) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadWeather( int containerId ) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadMap( int containerId ) {
|
||||
mMapProvider = load( MogoModulePaths.PATH_MODULE_MAP );
|
||||
addFragment( mMapProvider, containerId );
|
||||
}
|
||||
|
||||
private IMogoModuleProvider load( String path ) {
|
||||
return ( IMogoModuleProvider ) ARouter.getInstance().build( path ).navigation( getContext() );
|
||||
}
|
||||
|
||||
private void addFragment( IMogoModuleProvider provider, int containerId ) {
|
||||
if ( provider == null ) {
|
||||
Logger.e( TAG, "add fragment fail cause provider == null, container is %s", ResourcesHelper.getResNameById( getApplicationContext(), containerId ) );
|
||||
return;
|
||||
}
|
||||
final Fragment fragment = provider.createFragment( getContext(), null );
|
||||
if ( fragment == null ) {
|
||||
Logger.e( TAG, "add fragment fail cause fragment == null, container is %s", ResourcesHelper.getResNameById( getApplicationContext(), containerId ) );
|
||||
return;
|
||||
}
|
||||
mActivity.getSupportFragmentManager().beginTransaction()
|
||||
.add( containerId, fragment, provider.getModuleName() )
|
||||
.commitAllowingStateLoss();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEnable( String module ) {
|
||||
mEnableModuleName = module;
|
||||
final Set< Map.Entry< String, IMogoModuleProvider > > entries = mCardProviders.entrySet();
|
||||
if ( !entries.isEmpty() ) {
|
||||
for ( Map.Entry< String, IMogoModuleProvider > entry : entries ) {
|
||||
final String key = entry.getKey();
|
||||
final IMogoModuleProvider provider = entry.getValue();
|
||||
if ( TextUtils.equals( key, module ) ) {
|
||||
provider.getCardLifecycle().onPerform();
|
||||
} else {
|
||||
provider.getCardLifecycle().onDisable();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMapLoaded() {
|
||||
if ( mEnableModuleName != null ) {
|
||||
if ( mCardProviders.get( mEnableModuleName ) != null ) {
|
||||
mCardProviders.get( mEnableModuleName ).getMapListener().onMapLoaded();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTouch( MotionEvent motionEvent ) {
|
||||
if ( mEnableModuleName != null ) {
|
||||
if ( mCardProviders.get( mEnableModuleName ) != null ) {
|
||||
mCardProviders.get( mEnableModuleName ).getMapListener().onTouch( motionEvent );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPOIClick( MogoPoi poi ) {
|
||||
if ( mEnableModuleName != null ) {
|
||||
if ( mCardProviders.get( mEnableModuleName ) != null ) {
|
||||
mCardProviders.get( mEnableModuleName ).getMapListener().onPOIClick( poi );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user