Changed a few strings, made use of the ViewModels for substitution plan and food menu fragments, fixed a visual bug in the course_card.xml

This commit is contained in:
Deniz Düzgören
2019-09-18 11:12:10 +02:00
parent 797a634da4
commit be810e5bfb
10 changed files with 70 additions and 72 deletions
@@ -99,7 +99,8 @@ internal class DataFetcher(isPlan: Boolean, isMenu: Boolean, isJobService: Boole
mView.get()?.let { mView.get()?.let {
try { try {
it.findViewById<SwipeRefreshLayout>(R.id.pullToRefresh).isRefreshing = false it.findViewById<SwipeRefreshLayout>(R.id.pullToRefresh).isRefreshing = false
} catch (ignored: Exception) {} } catch (ignored: Exception) {
}
} }
mView.clear() mView.clear()
mContext.clear() mContext.clear()
@@ -108,8 +109,8 @@ internal class DataFetcher(isPlan: Boolean, isMenu: Boolean, isJobService: Boole
private fun requestFoodMenuData() { private fun requestFoodMenuData() {
val docFood = Jsoup.connect(foodUrl).get() val docFood = Jsoup.connect(foodUrl).get()
currentFoodTime = docFood.select("h1")[0].text() currentFoodTime = docFood.select("h1")[0].text()
if (currentFoodTime != prefs.getString("newFoodTime", "")) {
val foodRepository = FoodRepository(mApplication) val foodRepository = FoodRepository(mApplication)
if (currentFoodTime != prefs.getString("newFoodTime", "")) {
val foodElements = docFood.select("th") val foodElements = docFood.select("th")
foodRepository.deleteAll() foodRepository.deleteAll()
@@ -138,8 +139,8 @@ internal class DataFetcher(isPlan: Boolean, isMenu: Boolean, isJobService: Boole
private fun requestSubstPlan() { private fun requestSubstPlan() {
val doc = Jsoup.connect(substUrl).get() val doc = Jsoup.connect(substUrl).get()
currentTime = doc.select("h1")[0].text() currentTime = doc.select("h1")[0].text()
if (currentTime != prefs.getString("timeNew", "")) {
val substRepo = SubstRepository(mApplication) val substRepo = SubstRepository(mApplication)
if (currentTime != prefs.getString("timeNew", "")) {
val rows = doc.select("tr") val rows = doc.select("tr")
val paragraphs = doc.select("p") val paragraphs = doc.select("p")
val substArray = ArrayList<Subst>() val substArray = ArrayList<Subst>()
@@ -180,9 +181,9 @@ internal class DataFetcher(isPlan: Boolean, isMenu: Boolean, isJobService: Boole
var countOfNotificationItems = 0 var countOfNotificationItems = 0
var countOfMoreNotificationItems = 0 var countOfMoreNotificationItems = 0
if (jobService && prefs.getBoolean("notif", true)) { if (jobService && prefs.getBoolean("notif", true)) {
substArray.filter { substitution -> substArray.filter { substItem ->
HelperFunctions.checkPersonalSubstitutions( HelperFunctions.checkPersonalSubstitutions(
substitution, substItem,
coursePreference, coursePreference,
classPreference, classPreference,
false false
@@ -209,31 +210,31 @@ internal class DataFetcher(isPlan: Boolean, isMenu: Boolean, isJobService: Boole
} }
private fun sendNotification() { private fun sendNotification() {
mContext.get()?.let { c -> mContext.get()?.let { context ->
val openApp = Intent(c, Main::class.java) val openApp = Intent(context, Main::class.java)
openApp.flags = Intent.FLAG_ACTIVITY_NEW_TASK openApp.flags = Intent.FLAG_ACTIVITY_NEW_TASK
openApp.flags += Intent.FLAG_ACTIVITY_CLEAR_TASK openApp.flags += Intent.FLAG_ACTIVITY_CLEAR_TASK
val openAppPending = PendingIntent.getActivity(c, 0, openApp, 0) val openAppPending = PendingIntent.getActivity(context, 0, openApp, 0)
val notificationLayout = RemoteViews(c.packageName, R.layout.notification) val notificationLayout = RemoteViews(context.packageName, R.layout.notification)
notificationLayout.setTextViewText(R.id.notification_title, c.getString( notificationLayout.setTextViewText(R.id.notification_title, context.getString(
R.string.substitutionPlan R.string.substitutionPlan
)) ))
notificationLayout.setTextViewText(R.id.notification_textview, notificationText) notificationLayout.setTextViewText(R.id.notification_textview, notificationText)
val manager = c.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
HelperFunctions.getNotificationChannel(c, prefs) HelperFunctions.getNotificationChannel(context, prefs)
} }
val notification = NotificationCompat.Builder(c, HelperFunctions.notificationChannelId) val notification = NotificationCompat.Builder(context, HelperFunctions.notificationChannelId)
.setStyle(NotificationCompat.DecoratedCustomViewStyle()) .setStyle(NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(notificationLayout) .setCustomContentView(notificationLayout)
.setSmallIcon(R.drawable.ic_avh) .setSmallIcon(R.drawable.ic_avh)
.setContentIntent(openAppPending) .setContentIntent(openAppPending)
.setAutoCancel(true) .setAutoCancel(true)
.setColor(ContextCompat.getColor(c, R.color.colorAccent)) .setColor(ContextCompat.getColor(context, R.color.colorAccent))
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
val sound = if ((prefs.getString("ringtoneUri", "") ?: "").isNotEmpty()) { val sound = if ((prefs.getString("ringtoneUri", "") ?: "").isNotEmpty()) {
@@ -1,8 +1,11 @@
package com.denizd.substitutionplan.database package com.denizd.substitutionplan.database
import android.app.Application import android.app.Application
import android.view.View
import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.denizd.substitutionplan.data.DataFetcher
import com.denizd.substitutionplan.models.Food import com.denizd.substitutionplan.models.Food
internal class FoodViewModel(application: Application) : AndroidViewModel(application) { internal class FoodViewModel(application: Application) : AndroidViewModel(application) {
@@ -10,8 +13,22 @@ internal class FoodViewModel(application: Application) : AndroidViewModel(applic
private val repository: FoodRepository = private val repository: FoodRepository =
FoodRepository(application) FoodRepository(application)
val allFoods: LiveData<List<Food>>? val allFoods: LiveData<List<Food>>?
private val app = application
init { init {
allFoods = repository.allFoods allFoods = repository.allFoods
} }
fun refresh(swipeRefreshLayout: SwipeRefreshLayout, rootView: View) {
swipeRefreshLayout.isRefreshing = true
DataFetcher(
isPlan = false,
isMenu = true,
isJobService = false,
context = app,
application = app,
parentView = rootView,
forced = false
).execute()
}
} }
@@ -8,6 +8,7 @@ import com.denizd.substitutionplan.models.Subst
@Dao @Dao
internal interface SubstDao { internal interface SubstDao {
// @get:Query("SELECT * FROM subst_table ORDER BY date ASC, `group` ASC, time ASC, priority DESC")
@get:Query("SELECT * FROM subst_table ORDER BY priority DESC") @get:Query("SELECT * FROM subst_table ORDER BY priority DESC")
val allSubst: LiveData<List<Subst>> val allSubst: LiveData<List<Subst>>
@@ -1,8 +1,11 @@
package com.denizd.substitutionplan.database package com.denizd.substitutionplan.database
import android.app.Application import android.app.Application
import android.view.View
import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.denizd.substitutionplan.data.DataFetcher
import com.denizd.substitutionplan.models.Subst import com.denizd.substitutionplan.models.Subst
internal class SubstViewModel(application: Application) : AndroidViewModel(application) { internal class SubstViewModel(application: Application) : AndroidViewModel(application) {
@@ -10,8 +13,22 @@ internal class SubstViewModel(application: Application) : AndroidViewModel(appli
private val repository: SubstRepository = private val repository: SubstRepository =
SubstRepository(application) SubstRepository(application)
val allSubst: LiveData<List<Subst>>? val allSubst: LiveData<List<Subst>>?
private val app = application
init { init {
allSubst = repository.allSubst allSubst = repository.allSubst
} }
fun refresh(swipeRefreshLayout: SwipeRefreshLayout, rootView: View, refreshMenu: Boolean) {
swipeRefreshLayout.isRefreshing = true
DataFetcher(
isPlan = true,
isMenu = refreshMenu,
isJobService = false,
context = app,
application = app,
parentView = rootView,
forced = false
).execute()
}
} }
@@ -23,7 +23,6 @@ internal class FoodFragment : Fragment(R.layout.food_layout) {
private val mAdapter = FoodAdapter(foodArrayList) private val mAdapter = FoodAdapter(foodArrayList)
private lateinit var mContext: Context private lateinit var mContext: Context
private lateinit var prefs: SharedPreferences private lateinit var prefs: SharedPreferences
private lateinit var edit: SharedPreferences.Editor
private lateinit var recyclerView: RecyclerView private lateinit var recyclerView: RecyclerView
private lateinit var foodViewModel: FoodViewModel private lateinit var foodViewModel: FoodViewModel
@@ -31,7 +30,6 @@ internal class FoodFragment : Fragment(R.layout.food_layout) {
super.onAttach(context) super.onAttach(context)
mContext = context mContext = context
prefs = PreferenceManager.getDefaultSharedPreferences(mContext) prefs = PreferenceManager.getDefaultSharedPreferences(mContext)
edit = prefs.edit()
} }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@@ -41,7 +39,7 @@ internal class FoodFragment : Fragment(R.layout.food_layout) {
try { try {
recyclerView = view.findViewById(R.id.linear_food) recyclerView = view.findViewById(R.id.linear_food)
recyclerView.hasFixedSize() recyclerView.hasFixedSize()
recyclerView.layoutManager = GridLayoutManager(mContext, 1) // , RecyclerView.VERTICAL,false recyclerView.layoutManager = GridLayoutManager(mContext, 1)
recyclerView.adapter = mAdapter recyclerView.adapter = mAdapter
try { try {
@@ -61,29 +59,11 @@ internal class FoodFragment : Fragment(R.layout.food_layout) {
}) })
if (prefs.getBoolean("autoRefresh", false)) { if (prefs.getBoolean("autoRefresh", false)) {
pullToRefresh.isRefreshing = true foodViewModel.refresh(swipeRefreshLayout = pullToRefresh, rootView = view.rootView)
DataFetcher(
isPlan = false,
isMenu = true,
isJobService = false,
context = mContext,
application = activity!!.application,
parentView = view.rootView,
forced = false
).execute()
} }
pullToRefresh.setOnRefreshListener { pullToRefresh.setOnRefreshListener {
pullToRefresh.isRefreshing = true foodViewModel.refresh(swipeRefreshLayout = pullToRefresh, rootView = view.rootView)
DataFetcher(
isPlan = false,
isMenu = true,
isJobService = false,
context = mContext,
application = activity!!.application,
parentView = view.rootView,
forced = false
).execute()
} }
} }
} }
@@ -27,9 +27,9 @@ internal class PersonalPlanFragment : PlanFragment() {
persPlanEmpty = true persPlanEmpty = true
recyclerView.visibility = View.VISIBLE recyclerView.visibility = View.VISIBLE
substitutions.filter { substitution -> substitutions.filter { substItem ->
HelperFunctions.checkPersonalSubstitutions( HelperFunctions.checkPersonalSubstitutions(
substitution, substItem,
coursePreference, coursePreference,
classPreference, classPreference,
true true
@@ -38,8 +38,6 @@ internal class PersonalPlanFragment : PlanFragment() {
planCardList.add(substItem) planCardList.add(substItem)
} }
persPlanEmpty = (planCardList.size == 1 && planCardList[0].date.substring(0, 3) == "psa") || planCardList.isEmpty() persPlanEmpty = (planCardList.size == 1 && planCardList[0].date.substring(0, 3) == "psa") || planCardList.isEmpty()
planCardList.sortWith(Comparator { lhs, rhs -> rhs.priority.compareTo(lhs.priority) })
recyclerView.scheduleLayoutAnimation() recyclerView.scheduleLayoutAnimation()
mAdapter.setSubst(planCardList) mAdapter.setSubst(planCardList)
@@ -24,7 +24,6 @@ import kotlin.collections.ArrayList
internal open class PlanFragment : Fragment(R.layout.plan) { internal open class PlanFragment : Fragment(R.layout.plan) {
lateinit var recyclerView: RecyclerView lateinit var recyclerView: RecyclerView
lateinit var mAdapter: CardAdapter lateinit var mAdapter: CardAdapter
private lateinit var layoutManager: GridLayoutManager
var planCardList = ArrayList<Subst>() var planCardList = ArrayList<Subst>()
lateinit var substViewModel: SubstViewModel lateinit var substViewModel: SubstViewModel
private lateinit var mContext: Context private lateinit var mContext: Context
@@ -44,50 +43,34 @@ internal open class PlanFragment : Fragment(R.layout.plan) {
override fun onConfigurationChanged(newConfig: Configuration) { override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig) super.onConfigurationChanged(newConfig)
layoutManager = GridLayoutManager(mContext, getGridColumnCount(newConfig)) recyclerView.layoutManager = GridLayoutManager(mContext, getGridColumnCount(newConfig))
recyclerView.layoutManager = layoutManager
} }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
val pullToRefresh = view.findViewById<SwipeRefreshLayout>(R.id.pullToRefresh) substViewModel = ViewModelProviders.of(this).get(SubstViewModel::class.java)
val pullToRefresh = view.findViewById<SwipeRefreshLayout>(R.id.pullToRefresh)
recyclerView = view.findViewById(R.id.linearRecycler) recyclerView = view.findViewById(R.id.linearRecycler)
recyclerView.hasFixedSize() recyclerView.hasFixedSize()
layoutManager = GridLayoutManager(mContext, getGridColumnCount(resources.configuration)) recyclerView.layoutManager = GridLayoutManager(mContext, getGridColumnCount(resources.configuration))
recyclerView.layoutManager = layoutManager
mAdapter = CardAdapter(planCardList, prefs) mAdapter = CardAdapter(planCardList, prefs)
recyclerView.adapter = mAdapter recyclerView.adapter = mAdapter
if (prefs.getInt("firstTimeOpening", 0) == 1) { if (prefs.getInt("firstTimeOpening", 0) == 1) {
startFetch(view, pullToRefresh, true) substViewModel.refresh(swipeRefreshLayout = pullToRefresh, rootView = view.rootView, refreshMenu = true)
prefs.edit().putInt("firstTimeOpening", 2).apply() prefs.edit().putInt("firstTimeOpening", 2).apply()
} }
if (prefs.getBoolean("autoRefresh", false)) { if (prefs.getBoolean("autoRefresh", false)) {
startFetch(view, pullToRefresh, false) substViewModel.refresh(swipeRefreshLayout = pullToRefresh, rootView = view.rootView, refreshMenu = false)
} }
substViewModel = ViewModelProviders.of(this).get(SubstViewModel::class.java)
pullToRefresh.setOnRefreshListener { pullToRefresh.setOnRefreshListener {
startFetch(view, pullToRefresh, false) substViewModel.refresh(swipeRefreshLayout = pullToRefresh, rootView = view.rootView, refreshMenu = false)
} }
} }
private fun startFetch(view: View, pullToRefresh: SwipeRefreshLayout, refreshMenu: Boolean) {
pullToRefresh.isRefreshing = true
DataFetcher(
isPlan = true,
isMenu = refreshMenu,
isJobService = false,
context = mContext,
application = activity!!.application,
parentView = view.rootView,
forced = false
).execute()
}
private fun getGridColumnCount(config: Configuration): Int { private fun getGridColumnCount(config: Configuration): Int {
val tabletSize = resources.getBoolean(R.bool.isTablet) val tabletSize = resources.getBoolean(R.bool.isTablet)
return if (tabletSize) { return if (tabletSize) {
@@ -28,7 +28,7 @@
android:textAlignment="center" android:textAlignment="center"
android:textColor="@color/colorAccent" android:textColor="@color/colorAccent"
android:fontFamily="@font/manrope_extrabold" android:fontFamily="@font/manrope_extrabold"
android:textSize="24sp" android:textSize="28sp"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
@@ -225,7 +225,8 @@
app:layout_constraintTop_toBottomOf="@+id/txtMore" app:layout_constraintTop_toBottomOf="@+id/txtMore"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"> app:layout_constraintEnd_toEndOf="parent"
android:layoutAnimation="@anim/layout_animation_fall_down">
<CheckBox <CheckBox
android:id="@+id/cbGreetings" android:id="@+id/cbGreetings"
+4 -4
View File
@@ -91,19 +91,19 @@
<TextView <TextView
android:id="@+id/date" android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_marginTop="16dp" android:layout_marginTop="16dp"
android:layout_marginEnd="16dp" android:layout_marginEnd="16dp"
android:textAlignment="textEnd" android:textAlignment="textEnd"
android:textColor="@color/colorText" android:textColor="@color/colorText"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_max="200dp"/> app:layout_constraintWidth_max="200dp" />
<TextView <TextView
android:id="@+id/time" android:id="@+id/time"
android:layout_width="0dp" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
android:textAlignment="textEnd" android:textAlignment="textEnd"
@@ -114,7 +114,7 @@
<TextView <TextView
android:id="@+id/room" android:id="@+id/room"
android:layout_width="0dp" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
android:layout_marginBottom="16dp" android:layout_marginBottom="16dp"
+1 -1
View File
@@ -29,7 +29,7 @@
<string name="yourName">Your name</string> <string name="yourName">Your name</string>
<string name="nextGrade">Next, tell me which grade you\'re in.</string> <string name="nextGrade">Next, tell me which grade you\'re in.</string>
<string name="yourGrade">Your grade</string> <string name="yourGrade">Your grade</string>
<string name="seniorCourses">If you\'re in senior phase, please also tell me which courses you\'re enrolled in.</string> <string name="seniorCourses">If you\'re in senior phase (E-Phase, Q-Phase), please also tell me which courses you\'re enrolled in.</string>
<string name="yourCourses">Your courses</string> <string name="yourCourses">Your courses</string>
<string name="moreSettings">Lastly, just a few more settings.</string> <string name="moreSettings">Lastly, just a few more settings.</string>
<string name="notificationsAndSync">Receive course notifications</string> <string name="notificationsAndSync">Receive course notifications</string>