# ArchitecturePractice **Repository Path**: sunqihui2/ArchitecturePractice ## Basic Information - **Project Name**: ArchitecturePractice - **Description**: Android 应用架构组件(Architecture Components)实践 (实现代码的分层) - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-05-12 - **Last Updated**: 2022-06-01 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## ArchitecturePractice I wrote an article to introduce this project named of ArchitecturePractice. * [Android 应用架构组件(Architecture Components)实践](http://lijiankun24.com/Android-应用架构组件(Architecture-Components)实践/) Scan below QR code to download the apk.
* Created by lijiankun on 17/7/30.
*/
public class ZhihuListFragment extends LifecycleFragment {
// ZhihuListFragment 所对应的 ViewModel 类的对象
private ZhihuListViewModel mListViewModel = null;
......
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_zhihu_list, container, false);
initView(view);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
subscribeUI();
}
/**
* 将 ZhihuListFragment 对应的 ZhihuListViewModel 类中的 LiveData 添加注册监听到
* 此 ZhihuListFragment
*/
private void subscribeUI() {
// 通过 ViewModelProviders 创建对应的 ZhihuListViewModel 对象
ZhihuListViewModel.Factory factory = new ZhihuListViewModel
.Factory(MyApplication.getInstance()
, Injection.getDataRepository(MyApplication.getInstance()));
mListViewModel = ViewModelProviders.of(this, factory).get(ZhihuListViewModel.class);
mListViewModel.getZhihuList().observe(this, new Observer
* Created by lijiankun on 17/7/30.
*/
public class ZhihuListViewModel extends AndroidViewModel {
// 请求接口中查询的日期参数
private MutableLiveData
* Created by lijiankun on 17/7/7.
*/
public interface DataSource {
......
/**
* Zhihu 相关方法
*/
LiveData
* Created by lijiankun on 17/7/7.
*/
public class DataRepository {
private static DataRepository INSTANCE = null;
private final DataSource mRemoteDataSource;
private final DataSource mLocalDataSource;
private static Application sApplication = null;
private DataRepository(@NonNull DataSource remoteDataSource,
@NonNull DataSource localDataSource) {
mRemoteDataSource = remoteDataSource;
mLocalDataSource = localDataSource;
}
static DataRepository getInstance(@NonNull DataSource remoteDataSource,
@NonNull DataSource localDataSource,
Application application) {
if (INSTANCE == null) {
synchronized (DataRepository.class) {
if (INSTANCE == null) {
INSTANCE = new DataRepository(remoteDataSource, localDataSource);
sApplication = application;
}
}
}
return INSTANCE;
}
......
public LiveData
* Created by lijiankun on 17/7/7.
*/
public class RemoteDataSource implements DataSource {
private static RemoteDataSource INSTANCE = null;
private final MutableLiveData
* Created by lijiankun on 17/7/7.
*/
public class LocalDataSource implements DataSource {
private static LocalDataSource INSTANCE = null;
private LocalDataSource() {
}
public static LocalDataSource getInstance() {
if (INSTANCE == null) {
synchronized (LocalDataSource.class) {
if (INSTANCE == null) {
INSTANCE = new LocalDataSource();
}
}
}
return INSTANCE;
}
......
@Override
public LiveData>() {
@Override
public void onChanged(@Nullable List
> mZhihuList;
// 数据源
private DataRepository mDataRepository = null;
private ZhihuListViewModel(Application application, DataRepository dataRepository) {
super(application);
mDataRepository = dataRepository;
// 使用 Transformations.switchMap() 方法,当 View 改变 mZhihuPageDate 参数的值时,则进行 zhihu 列表数据的请求
mZhihuList = Transformations.switchMap(mZhihuPageDate, new Function
> apply(String input) {
return mDataRepository.getZhihuList(input);
}
});
}
/**
* 获取 Zhihu 列表数据
*
* @return Zhihu 列表数据
*/
public LiveData
> getZhihuList() {
return mZhihuList;
}
/**
* 数据请求状态由 DataRepository 控制,包括下拉刷新和上拉加载更多
*
* @return 是否在进行数据请求
*/
public LiveData
> getLastZhihuList();
LiveData
> getMoreZhihuList(String date);
......
LiveData
> getZhihuList(@NonNull String date) {
if (Util.isNetworkConnected(sApplication.getApplicationContext())) {
if (date.equals("today")) {
return mRemoteDataSource.getLastZhihuList();
} else {
return mRemoteDataSource.getMoreZhihuList(date);
}
} else {
if (date.equals("today")) {
return mLocalDataSource.getLastZhihuList();
} else {
return mLocalDataSource.getMoreZhihuList(date);
}
}
}
......
public LiveData
> mZhihuList;
private final ApiZhihu mApiZhihu;
private String mZhihuPageDate;
{
mIsLoadingZhihuList = new MutableLiveData<>();
mZhihuList = new MutableLiveData<>();
}
......
private RemoteDataSource() {
mApiZhihu = ApiManager.getInstance().getApiZhihu();
}
public static RemoteDataSource getInstance() {
if (INSTANCE == null) {
synchronized (RemoteDataSource.class) {
if (INSTANCE == null) {
INSTANCE = new RemoteDataSource();
}
}
}
return INSTANCE;
}
......
@Override
public LiveData
> getLastZhihuList() {
mIsLoadingZhihuList.setValue(true);
mApiZhihu.getLatestNews()
.enqueue(new Callback
> getMoreZhihuList(String date) {
mIsLoadingZhihuList.setValue(true);
mApiZhihu.getTheDaily(mZhihuPageDate)
.enqueue(new Callback
> getLastZhihuList() {
return AppDatabaseManager.getInstance().loadZhihuList();
}
@Override
public LiveData
> getMoreZhihuList(String date) {
return null;
}
@Override
public LiveData