Skip to content

Commit

Permalink
Version 1.2.3: Refactored MainActivity class, improved handling uncau…
Browse files Browse the repository at this point in the history
…ght exceptions, fixed bugs
  • Loading branch information
jakweg committed Dec 14, 2018
1 parent 4fc4d8b commit 5cb4c77
Show file tree
Hide file tree
Showing 53 changed files with 1,830 additions and 191 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Mobishit
*Alternatywna, otwartoźródłowa aplikacja do dziennika mobireg z wieloma funkcjami, takimi jak pokazywanie średnich, powiadomienia o nowych ocenach, odliczanie czasu do końca lekcji czy widżet z planem godzin.*

## <a href="https://github.com/JakubekWeg/Mobishit/releases/download/v1.2/mobishit.apk"><b>Pobierz aplikację</b></a>
## <a href="https://github.com/JakubekWeg/Mobishit/releases/download/v1.3-alpha/mobishit.apk"><b>Pobierz aplikację</b></a>

### Instrukcja instalacji po kliknęciu w <i> View all of README.md</i>

Expand Down
12 changes: 6 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ android {
applicationId "jakubweg.mobishit"
minSdkVersion 14
targetSdkVersion 28
versionCode 8
versionName "1.2.3"
versionCode 9
versionName "1.3 - alpha" //TODO
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
shrinkResources true
minifyEnabled true
shrinkResources false
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
Expand All @@ -46,9 +46,9 @@ dependencies {
implementation 'android.arch.lifecycle:extensions:1.1.1'
annotationProcessor 'android.arch.lifecycle:compiler:1.1.1'

implementation "android.arch.work:work-runtime:1.0.0-alpha11"
implementation "android.arch.work:work-runtime:1.0.0-alpha12"

implementation 'com.google.firebase:firebase-core:16.0.5'
implementation 'com.google.firebase:firebase-core:16.0.6'
implementation 'com.google.firebase:firebase-messaging:17.3.4'
implementation 'org.jsoup:jsoup:1.11.3'

Expand Down
3 changes: 2 additions & 1 deletion app/release/api_provider.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"fcm_handler": "https://jakubwe.ga/mobishit/fcm_handler.py",
"averages": "https://jakubwe.ga/mobishit/averages.py",
"tests": "https://jakubwe.ga/mobishit/tests.py",
"crashes": "https://jakubwe.ga/mobishit/crashes.py"
"crashes": "https://jakubwe.ga/mobishit/crashes.py",
"messages": "https://jakubwe.ga/mobishit/messages.py"
}
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
tools:replace="android:supportsRtl">

<activity
android:windowSoftInputMode="adjustPan"
android:name=".activity.MainActivity"
android:label="@string/title_activity_main"
android:launchMode="singleTask"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ abstract class DoublePanelActivity : FragmentActivity() {
}

supportFragmentManager.beginTransaction()
.setCustomAnimations(R.anim.fragment_enter, R.anim.fade_out, R.anim.fade_in, R.anim.fragment_exit)
.replace(secondFragmentContainerId, fragment)
.addToBackStack(null)
.commitAllowingStateLoss()
Expand Down
14 changes: 11 additions & 3 deletions app/src/main/java/jakubweg/mobishit/activity/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import jakubweg.mobishit.fragment.*
import jakubweg.mobishit.helper.MobiregAdjectiveManager
import jakubweg.mobishit.helper.MobiregPreferences
import jakubweg.mobishit.helper.SnackbarController
import jakubweg.mobishit.service.MessageUploadWorker
import jakubweg.mobishit.service.UpdateWorker

class MainActivity : DoublePanelActivity() {
Expand Down Expand Up @@ -51,7 +52,7 @@ class MainActivity : DoublePanelActivity() {
val drawerLayout get() = findViewById<DrawerLayout>(R.id.drawer_layout)!!

lateinit var snackbar: SnackbarController
lateinit var navigationUtils: MainActivityNavigationLayoutUtils
private lateinit var navigationUtils: MainActivityNavigationLayoutUtils
private var currentSelectedItemId = 0

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -84,7 +85,7 @@ class MainActivity : DoublePanelActivity() {

navigationView.getHeaderViewById<TextView>(R.id.textMotto)
.text = "Mobireg – dziennik tak ${
if (preferences.sex != "M")
if (preferences.sex == "M")
MobiregAdjectiveManager.getRandom()
else "świetny"} jak twoje oceny"
}
Expand All @@ -107,7 +108,13 @@ class MainActivity : DoublePanelActivity() {
fun onNavigationItemSelected(itemId: Int, requestNewLayout: Boolean) {
when (itemId) {
R.id.nav_force_refresh ->
tryToRefresh()
MessageUploadWorker.requestMessageSent(
this,
0,
"chuj",
"chujchuj")
//tryToRefresh()

R.id.nav_app_update ->
startActivity(Intent(Intent.ACTION_VIEW,
Uri.parse(preferences.getAppUpdateInfo()
Expand All @@ -130,6 +137,7 @@ class MainActivity : DoublePanelActivity() {
R.id.nav_messages -> MessagesListFragment.newInstance()
R.id.nav_about -> AboutFragment.newInstance()
R.id.nav_settings -> GeneralPreferenceFragment.newInstance()
R.id.nav_calculate_average -> VirtualMarksFragment.newInstance()
else -> null
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class MainActivityNavigationLayoutUtils(activity: MainActivity)

activity.menuInflater.inflate(R.menu.activity_main_toolbar, toolbar.menu)
if (MobiregPreferences.get(activity).getAppUpdateInfo() != null)
toolbar.menu?.findItem(R.id.nav_app_update)?.isVisible = true
navigationView.menu?.findItem(R.id.nav_app_update)?.isVisible = true
toolbar.setOnMenuItemClickListener(::onMenuItemClicked)
}

Expand Down Expand Up @@ -252,6 +252,7 @@ class MainActivityNavigationLayoutUtils(activity: MainActivity)
R.id.nav_tests -> "Sprawdziany"
R.id.nav_about -> "O aplikacji"
R.id.nav_settings -> "Ustawienia"
R.id.nav_calculate_average -> "Oblicz średnią"
else -> return
}
}
Expand Down Expand Up @@ -441,4 +442,6 @@ class MainActivitySyncObserver(
fun Menu.forEach(func: (MenuItem) -> Unit) {
for (i in 0 until size())
func.invoke(getItem(i))
}
}

inline fun <E> List<E>.averageBy(crossinline getter: (E) -> Double) = sumByDouble(getter).div(size.toDouble())
2 changes: 1 addition & 1 deletion app/src/main/java/jakubweg/mobishit/db/AppDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import android.content.Context
import jakubweg.mobishit.helper.MobiregPreferences
import jakubweg.mobishit.helper.SettingsMigrationHelper.deleteDatabaseAndRequestNew

@Database(entities = [Teacher::class, RoomData::class, TermData::class, SubjectData::class, GroupData::class, GroupTerm::class, MarkScaleGroup::class, MarkScale::class, MarkDivisionGroup::class, MarkKind::class, MarkGroupGroup::class, MarkGroup::class, EventType::class, EventTypeTeacher::class, EventTypeTerm::class, EventTypeGroup::class, EventData::class, EventIssue::class, EventEvent::class, AttendanceType::class, AttendanceData::class, MarkData::class, StudentGroup::class, EventTypeSchedule::class, LessonData::class, MessageData::class, TestData::class, AverageCacheData::class, ComparisonCacheData::class],
@Database(entities = [Teacher::class, RoomData::class, TermData::class, SubjectData::class, GroupData::class, GroupTerm::class, MarkScaleGroup::class, MarkScale::class, MarkDivisionGroup::class, MarkKind::class, MarkGroupGroup::class, MarkGroup::class, EventType::class, EventTypeTeacher::class, EventTypeTerm::class, EventTypeGroup::class, EventData::class, EventIssue::class, EventEvent::class, AttendanceType::class, AttendanceData::class, MarkData::class, StudentGroup::class, EventTypeSchedule::class, LessonData::class, MessageData::class, TestData::class, AverageCacheData::class, ComparisonCacheData::class, SentMessageData::class],
version = 1, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {
abstract val mainDao: MainDao
Expand Down
23 changes: 20 additions & 3 deletions app/src/main/java/jakubweg/mobishit/db/DataCreator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package jakubweg.mobishit.db

import android.graphics.Color
import com.google.gson.JsonElement
import com.google.gson.JsonObject
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonToken
import jakubweg.mobishit.helper.DateHelper
Expand Down Expand Up @@ -128,7 +129,7 @@ class DataCreator {
when (jr.nextName()) {
"id" -> id = jr.nextInt()
"name" -> name = jr.nextString()!!
"parents_id" -> parentId = jr.nextInt()
"parent_id" -> parentId = jr.nextInt()
"abbr" -> abbr = jr.nextString()!!
"type" -> type = jr.nextString()!!
"action" -> isDeleted = jr.nextString() == "D"
Expand Down Expand Up @@ -167,18 +168,24 @@ class DataCreator {
fun markScaleGroup(jr: JsonReader): MarkScaleGroup {
var id = 0
var name = ""
var isPublic = false
var markType = ""
var isDefault = false
jr.beginObject()
var isDeleted = false
while (jr.hasNext()) {
when (jr.nextName()) {
"id" -> id = jr.nextInt()
"name" -> name = jr.nextString()!!
"public" -> isPublic = jr.nextInt() != 0
"mark_types" -> markType = jr.nextString()!!
"is_default" -> isDefault = jr.nextInt() != 0
"action" -> isDeleted = jr.nextString() == "D"
else -> jr.skipValue()
}
}

return MarkScaleGroup(id, name).also {
return MarkScaleGroup(id, name, markType, isPublic, isDefault).also {
if (isDeleted) throw ObjectDeletedNotifier(it, it.id)
}
}
Expand Down Expand Up @@ -218,12 +225,14 @@ class DataCreator {
var type = 0
var rangeMin = 0f
var rangeMax = 0f
var isPublic = false
var markScaleGroupId = 0
jr.beginObject()
var isDeleted = false
while (jr.hasNext()) {
when (jr.nextName()) {
"id" -> id = jr.nextInt()
"public" -> isPublic = jr.nextInt() != 0
"mark_scale_groups_id" -> markScaleGroupId = jr.nextInt()
"name" -> name = jr.nextString()!!
"type" -> type = jr.nextInt()
Expand All @@ -234,7 +243,7 @@ class DataCreator {
}
}

return MarkDivisionGroup(id, name, type, rangeMin, rangeMax, markScaleGroupId).also {
return MarkDivisionGroup(id, name, type, isPublic, rangeMin, rangeMax, markScaleGroupId).also {
if (isDeleted) throw ObjectDeletedNotifier(it, it.id)
}
}
Expand Down Expand Up @@ -756,3 +765,11 @@ fun JsonReader.nextFloatOrNull() = if (peek() == JsonToken.NULL) {
} else nextDouble().toFloat()

val JsonElement?.asStringOrNull get() = if (this == null || isJsonNull) null else asString!!

fun JsonObject.getBoolean(key: String, defaultValue: Boolean): Boolean {
val obj = this[key] ?: return defaultValue
return if (obj.isJsonPrimitive)
obj.asBoolean
else
obj.asString?.equals("true", true) ?: defaultValue
}
20 changes: 18 additions & 2 deletions app/src/main/java/jakubweg/mobishit/db/Entities.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ class GroupData(@PrimaryKey(autoGenerate = true) val id: Int, val name: String,
class GroupTerm(@PrimaryKey(autoGenerate = true) val id: Int, val groupId: Int, val termId: Int)

@Entity(tableName = "MarkScaleGroups")
class MarkScaleGroup(@PrimaryKey(autoGenerate = true) val id: Int, val name: String)
class MarkScaleGroup(@PrimaryKey(autoGenerate = true) val id: Int, val name: String, val markType: String, val isPublic: Boolean, val isDefault: Boolean)

@Entity(tableName = "MarkScales")
class MarkScale(@PrimaryKey(autoGenerate = true) val id: Int, val markScaleGroupId: Int, val abbreviation: String, val name: String, val markValue: Float, val noCountToAverage: Boolean)

@Entity(tableName = "MarkDivisionGroups")
class MarkDivisionGroup(@PrimaryKey(autoGenerate = true) val id: Int, val name: String, val type: Int, val rangeMin: Float, val rangeMax: Float, val markScaleGroupsId: Int)
class MarkDivisionGroup(@PrimaryKey(autoGenerate = true) val id: Int, val name: String, val type: Int, val isPublic: Boolean, val rangeMin: Float, val rangeMax: Float, val markScaleGroupsId: Int)

@Entity(tableName = "MarkKinds")
class MarkKind(@PrimaryKey(autoGenerate = true) val id: Int, val name: String, val abbreviation: String, val defaultMarkType: Int?, val defaultMarkScaleGroupId: Int?, val defaultWeight: Int?, val position: Int, val cssStyle: String)
Expand Down Expand Up @@ -181,3 +181,19 @@ class ComparisonCacheData(@PrimaryKey(autoGenerate = true) val id: Int,
jo["img_school"].asStringOrNull
)
}

@Entity(tableName = "SentMessages")
class SentMessageData(@PrimaryKey(autoGenerate = true) val id: Int,
val subject: String,
val content: String,
val sentTime: Long,
val receiverId: Int,
val status: Int) {
companion object {
const val STATUS_ENQUEUED = 0
const val STATUS_IN_PROGRESS = 2
const val STATUS_SUCCEEDED = 3
const val STATUS_FAILED = 4
const val STATUS_CANCELLED = 5
}
}
20 changes: 19 additions & 1 deletion app/src/main/java/jakubweg/mobishit/db/MarkDao.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jakubweg.mobishit.db

import android.arch.persistence.room.*
import android.util.SparseArray
import jakubweg.mobishit.helper.DateHelper

@Suppress("FunctionName")
Expand All @@ -13,6 +14,14 @@ interface MarkDao {
const val PARENT_TYPE_COUNT_AVERAGE = 3
const val PARENT_TYPE_COUNT_WORSE = 4 //lol, i don't know why xD
const val PARENT_TYPE_COUNT_BEST = 5

val parentTypesAsText = SparseArray<String>(5).also {
it.put(MarkDao.PARENT_TYPE_COUNT_EVERY, "Liczy się każda ocena")
it.put(MarkDao.PARENT_TYPE_COUNT_AVERAGE, "Liczy się średnia z ocen")
it.put(MarkDao.PARENT_TYPE_COUNT_LAST, "Liczy się ostatnia ocena")
it.put(MarkDao.PARENT_TYPE_COUNT_BEST, "Liczy się lepsza ocena")
it.put(MarkDao.PARENT_TYPE_COUNT_WORSE, "Liczy się gorsza ocena")
}
}


Expand Down Expand Up @@ -42,7 +51,8 @@ interface MarkDao {

@Query("""SELECT
Marks.id, MarkGroups.description, MarkScales.abbreviation, IFNULL(MarkScales.markValue, -1) AS 'markScaleValue',
IFNULL(weight, IFNULL(MarkKinds.defaultWeight, -1)) as 'weight', MarkScales.noCountToAverage, IFNULL(Marks.markValue, -1) AS 'markPointsValue',
IFNULL(weight, IFNULL(MarkKinds.defaultWeight, -1)) as 'weight', MarkScales.noCountToAverage,
IFNULL(Marks.markValue, -1) AS 'markPointsValue',
MarkGroups.countPointsWithoutBase, IFNULL(MarkGroups.markValueMax, -1) as 'markValueMax', Terms.id AS 'termId',
parentType, IFNULL(MarkGroups.parentId, -1) as 'parentId', MarkGroups.id AS markGroupId, addTime
FROM Marks
Expand Down Expand Up @@ -114,6 +124,14 @@ WHERE Marks.id = :id AND visibility = 0 LIMIT 1""")
fun getDeletedMarkInfo(id: Int): DeletedMarkData


class MarkScaleShortInfo(val abbreviation: String, val markValue: Float)

@Query("""SELECT abbreviation, markValue FROM MarkScales
WHERE MarkScales.markScaleGroupId = :groupId AND not noCountToAverage AND length(abbreviation) > 0
ORDER BY markValue""")
fun getMarkScalesByGroupId(groupId: Int): List<MarkScaleShortInfo>


@Query("DELETE FROM AverageCaches")
fun clearAverageCache()

Expand Down
42 changes: 39 additions & 3 deletions app/src/main/java/jakubweg/mobishit/db/MessageDao.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package jakubweg.mobishit.db

import android.arch.persistence.room.Dao
import android.arch.persistence.room.Ignore
import android.arch.persistence.room.Query
import android.arch.lifecycle.LiveData
import android.arch.persistence.room.*
import jakubweg.mobishit.helper.DateHelper

@Dao
Expand Down Expand Up @@ -38,4 +37,41 @@ interface MessageDao {

@Query("UPDATE Messages SET readTime = :time WHERE id = :id")
fun updateReadTime(id: Int, time: Long)

class TeacherIdAndName(val id: Int, val fullName: String) {
override fun toString() = fullName
}

@Query("SELECT id, name || ' ' || surname as fullName FROM Teachers ORDER BY name,surname")
fun getTeachers(): List<TeacherIdAndName>


@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertSentRequest(obj: SentMessageData): Long

class SentMessageShortData(val fullName: String?, val subject: String, val sentTime: Long, val receiverId: Int, val status: Int) {
fun statusAsString() = when (status) {
SentMessageData.STATUS_ENQUEUED -> "Oczekuje na wysłanie"
SentMessageData.STATUS_FAILED -> "Niepowodzenie wysłania"
SentMessageData.STATUS_IN_PROGRESS -> "Wysyłanie…"
SentMessageData.STATUS_SUCCEEDED -> "Wysłana"
SentMessageData.STATUS_CANCELLED -> "Anulowana"
else -> throw IllegalStateException("unknown status")
}
}

@Query("""SELECT name || ' ' || surname as fullName, subject, sentTime, status, receiverId FROM SentMessages
LEFT OUTER JOIN Teachers ON Teachers.id = receiverId
ORDER BY sentTime DESC
""")
fun getAllSentMessages(): LiveData<List<SentMessageShortData>>

class ShortInfoToSendMessage(val subject: String, val content: String, val receiverId: Int)

@Query("SELECT subject, content, receiverId FROM SentMessages WHERE id = :id LIMIT 1")
fun getMessageToSentById(id: Long): ShortInfoToSendMessage


@Query("UPDATE SentMessages SET status = :status WHERE id = :id")
fun markMessageStatus(id: Long, status: Int)
}
15 changes: 2 additions & 13 deletions app/src/main/java/jakubweg/mobishit/fragment/AboutFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import android.widget.ImageView
import jakubweg.mobishit.BuildConfig
import jakubweg.mobishit.R
import jakubweg.mobishit.activity.FragmentActivity
import jakubweg.mobishit.helper.CrashHandler
import jakubweg.mobishit.helper.MobiregPreferences
import jakubweg.mobishit.helper.textView

Expand All @@ -35,21 +34,11 @@ class AboutFragment : Fragment() {

@SuppressLint("SetTextI18n")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
view.textView(R.id.textVersionInfo)!!.text = "Mobishit ${BuildConfig.VERSION_NAME} (${BuildConfig.VERSION_CODE}) - ${BuildConfig.BUILD_TYPE}"
view.textView(R.id.textVersionInfo)!!.text = "Mobishit ${BuildConfig.VERSION_NAME} (${BuildConfig.VERSION_CODE})"


view.findViewById<View>(R.id.btnOpenGithub)!!.setOnClickListener {
//openGithub() TODO
throw IllegalStateException("You can't click the button")
Thread {
}.also {
it.uncaughtExceptionHandler = Thread.UncaughtExceptionHandler { t, e ->
t ?: return@UncaughtExceptionHandler
e ?: return@UncaughtExceptionHandler
CrashHandler.onNewCrash(activity!!, t, e)
}
it.start()
}
openGithub()
}
view.findViewById<Button>(R.id.btnReportError)!!.apply {
val ending = when (MobiregPreferences.get(context ?: return).sex) {
Expand Down
Loading

0 comments on commit 5cb4c77

Please sign in to comment.