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 {
try {
it.findViewById<SwipeRefreshLayout>(R.id.pullToRefresh).isRefreshing = false
} catch (ignored: Exception) {}
} catch (ignored: Exception) {
}
}
mView.clear()
mContext.clear()
@@ -108,8 +109,8 @@ internal class DataFetcher(isPlan: Boolean, isMenu: Boolean, isJobService: Boole
private fun requestFoodMenuData() {
val docFood = Jsoup.connect(foodUrl).get()
currentFoodTime = docFood.select("h1")[0].text()
val foodRepository = FoodRepository(mApplication)
if (currentFoodTime != prefs.getString("newFoodTime", "")) {
val foodRepository = FoodRepository(mApplication)
val foodElements = docFood.select("th")
foodRepository.deleteAll()
@@ -138,8 +139,8 @@ internal class DataFetcher(isPlan: Boolean, isMenu: Boolean, isJobService: Boole
private fun requestSubstPlan() {
val doc = Jsoup.connect(substUrl).get()
currentTime = doc.select("h1")[0].text()
val substRepo = SubstRepository(mApplication)
if (currentTime != prefs.getString("timeNew", "")) {
val substRepo = SubstRepository(mApplication)
val rows = doc.select("tr")
val paragraphs = doc.select("p")
val substArray = ArrayList<Subst>()
@@ -180,9 +181,9 @@ internal class DataFetcher(isPlan: Boolean, isMenu: Boolean, isJobService: Boole
var countOfNotificationItems = 0
var countOfMoreNotificationItems = 0
if (jobService && prefs.getBoolean("notif", true)) {
substArray.filter { substitution ->
substArray.filter { substItem ->
HelperFunctions.checkPersonalSubstitutions(
substitution,
substItem,
coursePreference,
classPreference,
false
@@ -209,31 +210,31 @@ internal class DataFetcher(isPlan: Boolean, isMenu: Boolean, isJobService: Boole
}
private fun sendNotification() {
mContext.get()?.let { c ->
val openApp = Intent(c, Main::class.java)
mContext.get()?.let { context ->
val openApp = Intent(context, Main::class.java)
openApp.flags = Intent.FLAG_ACTIVITY_NEW_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)
notificationLayout.setTextViewText(R.id.notification_title, c.getString(
val notificationLayout = RemoteViews(context.packageName, R.layout.notification)
notificationLayout.setTextViewText(R.id.notification_title, context.getString(
R.string.substitutionPlan
))
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) {
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())
.setCustomContentView(notificationLayout)
.setSmallIcon(R.drawable.ic_avh)
.setContentIntent(openAppPending)
.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) {
val sound = if ((prefs.getString("ringtoneUri", "") ?: "").isNotEmpty()) {
@@ -1,8 +1,11 @@
package com.denizd.substitutionplan.database
import android.app.Application
import android.view.View
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.denizd.substitutionplan.data.DataFetcher
import com.denizd.substitutionplan.models.Food
internal class FoodViewModel(application: Application) : AndroidViewModel(application) {
@@ -10,8 +13,22 @@ internal class FoodViewModel(application: Application) : AndroidViewModel(applic
private val repository: FoodRepository =
FoodRepository(application)
val allFoods: LiveData<List<Food>>?
private val app = application
init {
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
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")
val allSubst: LiveData<List<Subst>>
@@ -1,8 +1,11 @@
package com.denizd.substitutionplan.database
import android.app.Application
import android.view.View
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.denizd.substitutionplan.data.DataFetcher
import com.denizd.substitutionplan.models.Subst
internal class SubstViewModel(application: Application) : AndroidViewModel(application) {
@@ -10,8 +13,22 @@ internal class SubstViewModel(application: Application) : AndroidViewModel(appli
private val repository: SubstRepository =
SubstRepository(application)
val allSubst: LiveData<List<Subst>>?
private val app = application
init {
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 lateinit var mContext: Context
private lateinit var prefs: SharedPreferences
private lateinit var edit: SharedPreferences.Editor
private lateinit var recyclerView: RecyclerView
private lateinit var foodViewModel: FoodViewModel
@@ -31,7 +30,6 @@ internal class FoodFragment : Fragment(R.layout.food_layout) {
super.onAttach(context)
mContext = context
prefs = PreferenceManager.getDefaultSharedPreferences(mContext)
edit = prefs.edit()
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@@ -41,7 +39,7 @@ internal class FoodFragment : Fragment(R.layout.food_layout) {
try {
recyclerView = view.findViewById(R.id.linear_food)
recyclerView.hasFixedSize()
recyclerView.layoutManager = GridLayoutManager(mContext, 1) // , RecyclerView.VERTICAL,false
recyclerView.layoutManager = GridLayoutManager(mContext, 1)
recyclerView.adapter = mAdapter
try {
@@ -61,29 +59,11 @@ internal class FoodFragment : Fragment(R.layout.food_layout) {
})
if (prefs.getBoolean("autoRefresh", false)) {
pullToRefresh.isRefreshing = true
DataFetcher(
isPlan = false,
isMenu = true,
isJobService = false,
context = mContext,
application = activity!!.application,
parentView = view.rootView,
forced = false
).execute()
foodViewModel.refresh(swipeRefreshLayout = pullToRefresh, rootView = view.rootView)
}
pullToRefresh.setOnRefreshListener {
pullToRefresh.isRefreshing = true
DataFetcher(
isPlan = false,
isMenu = true,
isJobService = false,
context = mContext,
application = activity!!.application,
parentView = view.rootView,
forced = false
).execute()
foodViewModel.refresh(swipeRefreshLayout = pullToRefresh, rootView = view.rootView)
}
}
}
@@ -27,9 +27,9 @@ internal class PersonalPlanFragment : PlanFragment() {
persPlanEmpty = true
recyclerView.visibility = View.VISIBLE
substitutions.filter { substitution ->
substitutions.filter { substItem ->
HelperFunctions.checkPersonalSubstitutions(
substitution,
substItem,
coursePreference,
classPreference,
true
@@ -38,8 +38,6 @@ internal class PersonalPlanFragment : PlanFragment() {
planCardList.add(substItem)
}
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()
mAdapter.setSubst(planCardList)
@@ -24,7 +24,6 @@ import kotlin.collections.ArrayList
internal open class PlanFragment : Fragment(R.layout.plan) {
lateinit var recyclerView: RecyclerView
lateinit var mAdapter: CardAdapter
private lateinit var layoutManager: GridLayoutManager
var planCardList = ArrayList<Subst>()
lateinit var substViewModel: SubstViewModel
private lateinit var mContext: Context
@@ -44,50 +43,34 @@ internal open class PlanFragment : Fragment(R.layout.plan) {
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
layoutManager = GridLayoutManager(mContext, getGridColumnCount(newConfig))
recyclerView.layoutManager = layoutManager
recyclerView.layoutManager = GridLayoutManager(mContext, getGridColumnCount(newConfig))
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
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.hasFixedSize()
layoutManager = GridLayoutManager(mContext, getGridColumnCount(resources.configuration))
recyclerView.layoutManager = layoutManager
recyclerView.layoutManager = GridLayoutManager(mContext, getGridColumnCount(resources.configuration))
mAdapter = CardAdapter(planCardList, prefs)
recyclerView.adapter = mAdapter
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()
}
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 {
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 {
val tabletSize = resources.getBoolean(R.bool.isTablet)
return if (tabletSize) {
@@ -28,7 +28,7 @@
android:textAlignment="center"
android:textColor="@color/colorAccent"
android:fontFamily="@font/manrope_extrabold"
android:textSize="24sp"
android:textSize="28sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
@@ -225,7 +225,8 @@
app:layout_constraintTop_toBottomOf="@+id/txtMore"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
app:layout_constraintEnd_toEndOf="parent"
android:layoutAnimation="@anim/layout_animation_fall_down">
<CheckBox
android:id="@+id/cbGreetings"
+4 -4
View File
@@ -91,19 +91,19 @@
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:textAlignment="textEnd"
android:textColor="@color/colorText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_max="200dp"/>
app:layout_constraintWidth_max="200dp" />
<TextView
android:id="@+id/time"
android:layout_width="0dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textAlignment="textEnd"
@@ -114,7 +114,7 @@
<TextView
android:id="@+id/room"
android:layout_width="0dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="16dp"
+1 -1
View File
@@ -29,7 +29,7 @@
<string name="yourName">Your name</string>
<string name="nextGrade">Next, tell me which grade you\'re in.</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="moreSettings">Lastly, just a few more settings.</string>
<string name="notificationsAndSync">Receive course notifications</string>