با سلام دوستان
من برای کش کردن اطلاعات ارسالی از سرور خط به خط با استاد پیش رفتم اما در کمال تعجب برنامم کرش میکنه و ارور کرش هم اصلا نگفته که اشکال در چه کلاسی هست و تنها تفاوتش اینه که استاد در اکتیویتی پیاده سازی میکنه و من در فرگمنت
ممنون میشم کمکم کنید(:
Dao :
@Dao
public interface DataDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(DataModel dataModel);
@Query("SELECT * FROM tbl_catch")
LiveData<List<DataModel>> getListCatch();
}
: DataBase
@Database(entities = {DataModel.class},version = 1)
public abstract class DataBase extends RoomDatabase {
static DataBase dataBase;
public static DataBase getDataBase(Context context){
if (dataBase==null){
dataBase=Room.databaseBuilder(context.getApplicationContext(),DataBase.class,"db")
.fallbackToDestructiveMigration()
.build();
}
return dataBase;
}
public abstract DataDao getDao();
}
: Repository
public class Repository {
private static UrlRetrofit urlRetrofit;
DataDao dao;
public Repository(UrlRetrofit urlRetrofit, DataDao dao) {
this.urlRetrofit = urlRetrofit;
this.dao = dao;
}
public Completable refreshInfo(){
return urlRetrofit.latest().doOnSuccess(list -> dao.insert((DataModel) list)).ignoreElement();
}
public LiveData<List<DataModel>> getListCatch(){
return dao.getListCatch();
}
}
: ApiServise
public interface ApiServise {
@GET("getnews.php")
Single<List<DataModel>> latest();
}
: ApiServiceProvider
public class ApiServiceProvider {
public static ApiServise urlRetrofit;
private static String url="http://192.168.1.108/news/";
public static ApiServise getUrlRetrofit(){
if (urlRetrofit==null){
Retrofit retrofit=new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
urlRetrofit=retrofit.create(ApiServise.class);
}
return urlRetrofit;
}
}
: MainViewModel
public class MainViewModel extends ViewModel {
Repository repository;
Disposable disposable;
public MainViewModel(Repository repository) {
this.repository = repository;
repository.refreshInfo()
.subscribeOn(Schedulers.io())
.subscribe(new CompletableObserver() {
@Override
public void onSubscribe(@NonNull Disposable d) {
disposable=d;
}
@Override
public void onComplete() {
}
@Override
public void onError(@NonNull Throwable e) {
}
});
}
public LiveData<List<DataModel>> getLatestsModels(){
return repository.getListCatch();
}
@Override
protected void onCleared() {
disposable.dispose();
super.onCleared();
}
}
: ViewModelFactory
public class ViewModelFactory implements ViewModelProvider.Factory{
Repository repository;
public ViewModelFactory(Repository repository) {
this.repository = repository;
}
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
return (T) new MainViewModel(repository);
}
}
: Fragment
public class LastNews extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.last_news, container, false);
}
MainViewModel viewModel;
Adapter adapter;
RecyclerView recyclerView;
List<DataModel> latestsModels=new ArrayList<>();
@SuppressLint("FragmentLiveDataObserve")
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recyclerView=view.findViewById(R.id.recy_last_news);
viewModel=new ViewModelProvider(this,new ViewModelFactory(new Repository(ApiServiceProvider.getUrlRetrofit(),DataBase.getDataBase(getContext()).getDao())))
.get(MainViewModel.class);
viewModel.getLatestsModels().observe(this, new Observer<List<DataModel>>() {
@Override
public void onChanged(List<DataModel> list) {
recyclerView.setLayoutManager(new LinearLayoutManager(getContext(),RecyclerView.VERTICAL,false));
adapter=new Adapter(getContext(),latestsModels);
recyclerView.setAdapter(adapter);
}
});
}
}