1
0
mirror of https://github.com/wulkanowy/wulkanowy.git synced 2025-02-21 19:44:45 +01:00

Add semester switch in grades (#94)

This commit is contained in:
Mikołaj Pich 2018-05-02 21:51:23 +02:00 committed by Rafał Borcz
parent cb6d39fe15
commit 4ef3334bf7
15 changed files with 192 additions and 52 deletions

View File

@ -10,9 +10,9 @@ public interface DbContract {
Week getWeek(String date); Week getWeek(String date);
List<Subject> getSubjectList(); List<Subject> getSubjectList(int semesterName);
List<Grade> getNewGrades(); List<Grade> getNewGrades(int semesterName);
long getCurrentStudentId(); long getCurrentStudentId();
@ -20,5 +20,9 @@ public interface DbContract {
long getCurrentDiaryId(); long getCurrentDiaryId();
long getSemesterId(int name);
long getCurrentSemesterId(); long getCurrentSemesterId();
int getCurrentSemesterName();
} }

View File

@ -8,6 +8,7 @@ import io.github.wulkanowy.data.db.dao.entities.DaoSession;
import io.github.wulkanowy.data.db.dao.entities.DiaryDao; import io.github.wulkanowy.data.db.dao.entities.DiaryDao;
import io.github.wulkanowy.data.db.dao.entities.Grade; import io.github.wulkanowy.data.db.dao.entities.Grade;
import io.github.wulkanowy.data.db.dao.entities.GradeDao; import io.github.wulkanowy.data.db.dao.entities.GradeDao;
import io.github.wulkanowy.data.db.dao.entities.Semester;
import io.github.wulkanowy.data.db.dao.entities.SemesterDao; import io.github.wulkanowy.data.db.dao.entities.SemesterDao;
import io.github.wulkanowy.data.db.dao.entities.StudentDao; import io.github.wulkanowy.data.db.dao.entities.StudentDao;
import io.github.wulkanowy.data.db.dao.entities.Subject; import io.github.wulkanowy.data.db.dao.entities.Subject;
@ -37,15 +38,16 @@ public class DbRepository implements DbContract {
).unique(); ).unique();
} }
public List<Subject> getSubjectList() {
return daoSession.getSemesterDao().load(getCurrentSemesterId()).getSubjectList(); public List<Subject> getSubjectList(int semesterName) {
return daoSession.getSemesterDao().load(getSemesterId(semesterName)).getSubjectList();
} }
@Override @Override
public List<Grade> getNewGrades() { public List<Grade> getNewGrades(int semesterName) {
return daoSession.getGradeDao().queryBuilder().where( return daoSession.getGradeDao().queryBuilder().where(
GradeDao.Properties.IsNew.eq(1), GradeDao.Properties.IsNew.eq(1),
GradeDao.Properties.SemesterId.eq(getCurrentSemesterId()) GradeDao.Properties.SemesterId.eq(getSemesterId(semesterName))
).list(); ).list();
} }
@ -72,11 +74,28 @@ public class DbRepository implements DbContract {
).unique().getId(); ).unique().getId();
} }
@Override
public long getSemesterId(int name) {
return daoSession.getSemesterDao().queryBuilder().where(
SemesterDao.Properties.DiaryId.eq(getCurrentDiaryId()),
SemesterDao.Properties.Name.eq(String.valueOf(name))
).unique().getId();
}
@Override @Override
public long getCurrentSemesterId() { public long getCurrentSemesterId() {
return getCurrentSemester().getId();
}
@Override
public int getCurrentSemesterName() {
return Integer.valueOf(getCurrentSemester().getName());
}
private Semester getCurrentSemester() {
return daoSession.getSemesterDao().queryBuilder().where( return daoSession.getSemesterDao().queryBuilder().where(
SemesterDao.Properties.DiaryId.eq(getCurrentDiaryId()), SemesterDao.Properties.DiaryId.eq(getCurrentDiaryId()),
SemesterDao.Properties.Current.eq(true) SemesterDao.Properties.Current.eq(true)
).unique().getId(); ).unique();
} }
} }

View File

@ -16,11 +16,11 @@ public interface SyncContract {
void initLastUser() throws IOException, CryptoException; void initLastUser() throws IOException, CryptoException;
void syncGrades(long semesterId) throws VulcanException, IOException, ParseException; void syncGrades(int semesterName) throws VulcanException, IOException, ParseException;
void syncGrades() throws VulcanException, IOException, ParseException; void syncGrades() throws VulcanException, IOException, ParseException;
void syncSubjects(long semesterId) throws VulcanException, IOException; void syncSubjects(int semesterName) throws VulcanException, IOException;
void syncSubjects() throws VulcanException, IOException; void syncSubjects() throws VulcanException, IOException;

View File

@ -48,8 +48,8 @@ public class SyncRepository implements SyncContract {
} }
@Override @Override
public void syncGrades(long semesterId) throws VulcanException, IOException, ParseException { public void syncGrades(int semesterName) throws VulcanException, IOException, ParseException {
gradeSync.sync(semesterId); gradeSync.sync(semesterName);
} }
@Override @Override
@ -58,8 +58,8 @@ public class SyncRepository implements SyncContract {
} }
@Override @Override
public void syncSubjects(long semesterId) throws VulcanException, IOException { public void syncSubjects(int semesterName) throws VulcanException, IOException {
subjectSync.sync(semesterId); subjectSync.sync(semesterName);
} }
@Override @Override

View File

@ -68,7 +68,7 @@ public class SyncJob extends SimpleJobService {
repository.getSyncRepo().initLastUser(); repository.getSyncRepo().initLastUser();
repository.getSyncRepo().syncAll(); repository.getSyncRepo().syncAll();
gradeList = repository.getDbRepo().getNewGrades(); gradeList = repository.getDbRepo().getNewGrades(repository.getDbRepo().getCurrentSemesterName());
if (!gradeList.isEmpty() && repository.getSharedRepo().isNotifyEnable()) { if (!gradeList.isEmpty() && repository.getSharedRepo().isNotifyEnable()) {
showNotification(); showNotification();

View File

@ -5,6 +5,7 @@ import android.content.Intent;
import android.graphics.Color; import android.graphics.Color;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.View; import android.view.View;
import com.aurelhubert.ahbottomnavigation.AHBottomNavigation; import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
@ -51,6 +52,7 @@ public class MainActivity extends BaseActivity implements MainContract.View,
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
getActivityComponent().inject(this); getActivityComponent().inject(this);
setButterKnife(ButterKnife.bind(this)); setButterKnife(ButterKnife.bind(this));

View File

@ -24,6 +24,8 @@ public interface GradesContract {
void setActivityTitle(); void setActivityTitle();
void setCurrentSemester(int semester);
boolean isMenuVisible(); boolean isMenuVisible();
} }
@ -36,5 +38,7 @@ public interface GradesContract {
void onRefresh(); void onRefresh();
void onStart(View view, OnFragmentIsReadyListener listener); void onStart(View view, OnFragmentIsReadyListener listener);
void onSemesterChange(int which);
} }
} }

View File

@ -1,12 +1,17 @@
package io.github.wulkanowy.ui.main.grades; package io.github.wulkanowy.ui.main.grades;
import android.content.DialogInterface;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar; import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout; import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -40,6 +45,8 @@ public class GradesFragment extends BaseFragment implements GradesContract.View
@Inject @Inject
GradesContract.Presenter presenter; GradesContract.Presenter presenter;
int currentSemester = -1;
public GradesFragment() { public GradesFragment() {
// empty constructor for fragment // empty constructor for fragment
} }
@ -59,6 +66,40 @@ public class GradesFragment extends BaseFragment implements GradesContract.View
return view; return view;
} }
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.semester_switch, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_filter) {
CharSequence[] items = new CharSequence[]{
getResources().getString(R.string.semester_text, 1),
getResources().getString(R.string.semester_text, 2),
};
new AlertDialog.Builder(getContext())
.setTitle(R.string.switch_semester)
.setNegativeButton(R.string.cancel, null)
.setSingleChoiceItems(items, this.currentSemester, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
presenter.onSemesterChange(which);
dialog.cancel();
}
}).show();
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
@Override @Override
protected void setUpOnViewCreated(View fragmentView) { protected void setUpOnViewCreated(View fragmentView) {
noItemView.setVisibility(View.GONE); noItemView.setVisibility(View.GONE);
@ -87,6 +128,10 @@ public class GradesFragment extends BaseFragment implements GradesContract.View
setTitle(getString(R.string.grades_text)); setTitle(getString(R.string.grades_text));
} }
public void setCurrentSemester(int currentSemester) {
this.currentSemester = currentSemester;
}
@Override @Override
public void onRefresh() { public void onRefresh() {
presenter.onRefresh(); presenter.onRefresh();

View File

@ -27,6 +27,8 @@ public class GradesPresenter extends BasePresenter<GradesContract.View>
private boolean isFirstSight = false; private boolean isFirstSight = false;
private int semesterName;
@Inject @Inject
GradesPresenter(RepositoryContract repository) { GradesPresenter(RepositoryContract repository) {
super(repository); super(repository);
@ -41,15 +43,30 @@ public class GradesPresenter extends BasePresenter<GradesContract.View>
getView().setActivityTitle(); getView().setActivityTitle();
} }
semesterName = getRepository().getDbRepo().getCurrentSemesterName();
getView().setCurrentSemester(semesterName - 1);
if (!isFirstSight) { if (!isFirstSight) {
isFirstSight = true; isFirstSight = true;
loadingTask = new AbstractTask(); reloadGrades();
loadingTask.setOnFirstLoadingListener(this);
loadingTask.execute();
} }
} }
@Override
public void onSemesterChange(int which) {
semesterName = which + 1;
getView().setCurrentSemester(which);
reloadGrades();
}
private void reloadGrades() {
loadingTask = new AbstractTask();
loadingTask.setOnFirstLoadingListener(this);
loadingTask.execute();
}
@Override @Override
public void onFragmentVisible(boolean isVisible) { public void onFragmentVisible(boolean isVisible) {
if (isVisible) { if (isVisible) {
@ -71,8 +88,8 @@ public class GradesPresenter extends BasePresenter<GradesContract.View>
@Override @Override
public void onDoInBackgroundRefresh() throws Exception { public void onDoInBackgroundRefresh() throws Exception {
getRepository().getSyncRepo().syncSubjects(); getRepository().getSyncRepo().syncSubjects(semesterName);
getRepository().getSyncRepo().syncGrades(); getRepository().getSyncRepo().syncGrades(semesterName);
} }
@Override @Override
@ -85,11 +102,9 @@ public class GradesPresenter extends BasePresenter<GradesContract.View>
@Override @Override
public void onEndRefreshAsync(boolean success, Exception exception) { public void onEndRefreshAsync(boolean success, Exception exception) {
if (success) { if (success) {
loadingTask = new AbstractTask(); reloadGrades();
loadingTask.setOnFirstLoadingListener(this);
loadingTask.execute();
int numberOfNewGrades = getRepository().getDbRepo().getNewGrades().size(); int numberOfNewGrades = getRepository().getDbRepo().getNewGrades(semesterName).size();
if (numberOfNewGrades <= 0) { if (numberOfNewGrades <= 0) {
getView().onRefreshSuccessNoGrade(); getView().onRefreshSuccessNoGrade();
@ -104,7 +119,7 @@ public class GradesPresenter extends BasePresenter<GradesContract.View>
@Override @Override
public void onDoInBackgroundLoading() { public void onDoInBackgroundLoading() {
List<Subject> subjectList = getRepository().getDbRepo().getSubjectList(); List<Subject> subjectList = getRepository().getDbRepo().getSubjectList(semesterName);
boolean isShowSummary = getRepository().getSharedRepo().isShowGradesSummary(); boolean isShowSummary = getRepository().getSharedRepo().isShowGradesSummary();
headerItems = new ArrayList<>(); headerItems = new ArrayList<>();
@ -136,12 +151,8 @@ public class GradesPresenter extends BasePresenter<GradesContract.View>
@Override @Override
public void onEndLoadingAsync(boolean result, Exception exception) { public void onEndLoadingAsync(boolean result, Exception exception) {
if (headerItems.isEmpty()) { getView().showNoItem(headerItems.isEmpty());
getView().showNoItem(true); getView().updateAdapterList(headerItems);
} else {
getView().updateAdapterList(headerItems);
getView().showNoItem(false);
}
listener.onFragmentIsReady(); listener.onFragmentIsReady();
} }

View File

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M10,18h4v-2h-4v2zM3,6v2h18L21,6L3,6zM6,13h12v-2L6,11v2z"/>
</vector>

View File

@ -1,33 +1,62 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_activity_container" android:id="@+id/main_activity_container"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context="io.github.wulkanowy.ui.main.MainActivity"> android:orientation="vertical"
android:weightSum="1"
tools:context="io.github.wulkanowy.ui.main.MainActivity"
>
<RelativeLayout <android.support.design.widget.AppBarLayout
android:id="@+id/main_activity_progress_bar"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content"
android:gravity="center"> android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
>
<ProgressBar <android.support.v7.widget.Toolbar
android:layout_width="wrap_content" android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
/>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior"
>
<RelativeLayout
android:id="@+id/main_activity_progress_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true" />
</RelativeLayout>
<com.aurelhubert.ahbottomnavigation.AHBottomNavigationViewPager
android:id="@+id/main_activity_view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:indeterminate="true" /> android:layout_marginBottom="@dimen/bottom_navigation_height" />
</RelativeLayout>
<com.aurelhubert.ahbottomnavigation.AHBottomNavigationViewPager <com.aurelhubert.ahbottomnavigation.AHBottomNavigation
android:id="@+id/main_activity_view_pager" android:id="@+id/main_activity_nav"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/bottom_navigation_height" /> android:layout_gravity="bottom" />
<com.aurelhubert.ahbottomnavigation.AHBottomNavigation </android.support.design.widget.CoordinatorLayout>
android:id="@+id/main_activity_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
</android.support.design.widget.CoordinatorLayout> </LinearLayout>

View File

@ -0,0 +1,13 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="io.github.wulkanowy.timetable.MainActivity"
>
<item
android:id="@+id/action_filter"
android:orderInCategory="100"
android:title="@string/switch_semester"
android:icon="@drawable/ic_filter_list_black_24dp"
app:showAsAction="always"
/>
</menu>

View File

@ -52,6 +52,10 @@
<string name="dialog_date_text">Data</string> <string name="dialog_date_text">Data</string>
<string name="dialog_color_text">Kolor</string> <string name="dialog_color_text">Kolor</string>
<string name="switch_semester">Zmień semestr</string>
<string name="semester_text">Semestr %d</string>
<string name="cancel">Anuluj</string>
<string name="timetable_dialog_lesson">Lekcja</string> <string name="timetable_dialog_lesson">Lekcja</string>
<string name="timetable_dialog_room">Sala</string> <string name="timetable_dialog_room">Sala</string>
<string name="timetable_dialog_group">Grupa</string> <string name="timetable_dialog_group">Grupa</string>

View File

@ -52,6 +52,10 @@
<string name="dialog_date_text">Date</string> <string name="dialog_date_text">Date</string>
<string name="dialog_color_text">Color</string> <string name="dialog_color_text">Color</string>
<string name="switch_semester">Switch semester</string>
<string name="semester_text">Semester %d</string>
<string name="cancel">Cancel</string>
<string name="timetable_dialog_lesson">Lesson</string> <string name="timetable_dialog_lesson">Lesson</string>
<string name="timetable_dialog_room">Room</string> <string name="timetable_dialog_room">Room</string>
<string name="timetable_dialog_group">Group</string> <string name="timetable_dialog_group">Group</string>

View File

@ -1,6 +1,6 @@
<resources> <resources>
<style name="WulkanowyTheme" parent="Base.Theme.AppCompat.Light"> <style name="WulkanowyTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorPrimary</item> <item name="colorAccent">@color/colorPrimary</item>