Browse Source

add teacher's widget :eyes:

Ethosa 3 years ago
parent
commit
95c5502e36

+ 2 - 0
.idea/misc.xml

@@ -23,6 +23,7 @@
         <entry key="..\:/android projects/KTC/app/src/main/res/drawable/ic_megaphone.xml" value="0.144" />
         <entry key="..\:/android projects/KTC/app/src/main/res/drawable/ic_mode_landscape.xml" value="0.144" />
         <entry key="..\:/android projects/KTC/app/src/main/res/drawable/ic_portrait.xml" value="0.144" />
+        <entry key="..\:/android projects/KTC/app/src/main/res/drawable/ic_reload.xml" value="0.112" />
         <entry key="..\:/android projects/KTC/app/src/main/res/drawable/ic_settings.xml" value="0.144" />
         <entry key="..\:/android projects/KTC/app/src/main/res/drawable/item.xml" value="0.1" />
         <entry key="..\:/android projects/KTC/app/src/main/res/drawable/navigation.xml" value="0.1185" />
@@ -80,6 +81,7 @@
         <entry key="..\:/android projects/KTC/app/src/main/res/layout/wall.xml" value="0.67" />
         <entry key="..\:/android projects/KTC/app/src/main/res/layout/widget_lesson.xml" value="0.43803680981595094" />
         <entry key="..\:/android projects/KTC/app/src/main/res/layout/widget_timetable.xml" value="0.5" />
+        <entry key="..\:/android projects/KTC/app/src/main/res/layout/widget_tlesson.xml" value="0.371875" />
         <entry key="..\:/android projects/KTC/app/src/main/res/menu/bottom_nav_menu.xml" value="0.371875" />
         <entry key="..\:/android projects/KTC/app/src/main/res/menu/menu_album.xml" value="0.371875" />
         <entry key="..\:/android projects/KTC/app/src/main/res/menu/menu_wall_post.xml" value="0.371875" />

+ 2 - 2
app/build.gradle

@@ -10,8 +10,8 @@ android {
         applicationId "com.ethosa.ktc"
         minSdk 21
         targetSdk 32
-        versionCode 14
-        versionName "0.8.3"
+        versionCode 15
+        versionName "0.8.4"
         ndk {
             abiFilters 'armeabi-v7a','arm64-v8a','x86','x86_64'
         }

+ 13 - 2
app/src/main/AndroidManifest.xml

@@ -14,6 +14,17 @@
         android:theme="@style/Theme.KTC"
         android:usesCleartextTraffic="true"
         tools:targetApi="m">
+        <receiver
+            android:name=".ui.widgets.TeacherTimetableWidget"
+            android:exported="true">
+            <intent-filter>
+                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
+            </intent-filter>
+
+            <meta-data
+                android:name="android.appwidget.provider"
+                android:resource="@xml/teacher_timetable_widget_info" />
+        </receiver>
         <receiver
             android:name=".ui.widgets.TimetableWidget"
             android:exported="true">
@@ -38,9 +49,9 @@
             android:theme="@style/Theme.KTC.NoActionBar" />
         <activity
             android:name=".ui.activities.MainActivity"
+            android:alwaysRetainTaskState="true"
             android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"
-            android:exported="true"
-            android:alwaysRetainTaskState="true">
+            android:exported="true">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
 

+ 4 - 0
app/src/main/java/com/ethosa/ktc/ui/fragments/TimetableFragment.kt

@@ -70,6 +70,10 @@ class TimetableFragment : IOFragmentBackPressed() {
         // toggle isStudent
         binding.toggleTimetable.setOnClickListener {
             Preferences.isStudent = !Preferences.isStudent
+            if (Preferences.isStudent && Preferences.timetableState == 2 && Preferences.teacherId == 0)
+                Preferences.isStudent = !Preferences.isStudent
+            if (!Preferences.isStudent && Preferences.timetableState == 2 && Preferences.group!!.id == 0)
+                Preferences.isStudent = !Preferences.isStudent
             loadState()
         }
 

+ 145 - 0
app/src/main/java/com/ethosa/ktc/ui/widgets/TeacherTimetableWidget.kt

@@ -0,0 +1,145 @@
+package com.ethosa.ktc.ui.widgets
+
+import android.annotation.SuppressLint
+import android.app.PendingIntent
+import android.appwidget.AppWidgetManager
+import android.appwidget.AppWidgetProvider
+import android.content.ComponentName
+import android.content.Context
+import android.content.Intent
+import android.os.Build
+import android.widget.RemoteViews
+import com.ethosa.ktc.Preferences
+import com.ethosa.ktc.R
+import com.ethosa.ktc.college.CollegeApi
+import com.ethosa.ktc.college.CollegeCallback
+import com.ethosa.ktc.college.teacher.TeacherTimetable
+import com.ethosa.ktc.ui.activities.MainActivity
+import com.google.gson.Gson
+import okhttp3.Call
+import okhttp3.Response
+import java.util.*
+
+
+/**
+ * Implementation of App Widget functionality.
+ */
+class TeacherTimetableWidget : AppWidgetProvider() {
+    private val college = CollegeApi()
+    private var preferences: Preferences? = null
+
+    override fun onUpdate(
+        context: Context,
+        appWidgetManager: AppWidgetManager,
+        appWidgetIds: IntArray
+    ) {
+        preferences = Preferences(context)
+        // There may be multiple widgets active, so update all of them
+        for (appWidgetId in appWidgetIds) {
+            updateAppWidget(context, appWidgetManager, appWidgetId)
+        }
+    }
+
+    /**
+     * Update timetable for current widget
+     */
+    @SuppressLint("UnspecifiedImmutableFlag")
+    private fun updateWidgetPendingIntent(
+        context: Context?,
+        appWidgetId: Int
+    ): PendingIntent {
+        val intent = Intent(context, TimetableWidget::class.java)
+        val ids = AppWidgetManager.getInstance(context)
+            .getAppWidgetIds(ComponentName(context!!, TimetableWidget::class.java))
+        intent.action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
+        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids)
+
+        return PendingIntent.getBroadcast(
+            context,
+            appWidgetId,
+            intent,
+            when {
+                Build.VERSION.SDK_INT >= 31 -> PendingIntent.FLAG_MUTABLE
+                else -> PendingIntent.FLAG_UPDATE_CURRENT
+            })
+    }
+
+    /**
+     * Open app with pending intent
+     */
+    @SuppressLint("UnspecifiedImmutableFlag")
+    private fun openAppPendingIntent(
+        context: Context?,
+        appWidgetId: Int
+    ): PendingIntent {
+        val intent = Intent(context, MainActivity::class.java)
+        return PendingIntent.getActivity(
+            context,
+            appWidgetId,
+            intent,
+            when {
+                Build.VERSION.SDK_INT >= 31 -> PendingIntent.FLAG_MUTABLE
+                else -> PendingIntent.FLAG_UPDATE_CURRENT
+            })
+    }
+
+    @SuppressLint("RemoteViewLayout")
+    private fun updateAppWidget(
+        context: Context,
+        appWidgetManager: AppWidgetManager,
+        appWidgetId: Int
+    ) {
+        // Construct the RemoteViews object
+        val views = RemoteViews(context.packageName, R.layout.widget_timetable)
+        views.setOnClickPendingIntent(
+            R.id.timetable_widget_reload,
+            updateWidgetPendingIntent(context, appWidgetId)
+        )
+        views.setOnClickPendingIntent(
+            R.id.timetable_widget_background,
+            openAppPendingIntent(context, appWidgetId)
+        )
+        // Load last group ID
+        val teacherId = Preferences.teacherId
+        val branchId = Preferences.branch!!.id
+        val calendar = Calendar.getInstance()
+        val weekday = calendar.get(Calendar.DAY_OF_WEEK)
+
+        college.fetchTeacherTimetable(branchId, teacherId, object : CollegeCallback {
+            @SuppressLint("SetTextI18n")
+            override fun onResponse(call: Call, response: Response) {
+                // Parse JSON
+                val json = response.body?.string()
+                val timetable = Gson().fromJson(json, TeacherTimetable::class.java)
+                println(weekday)
+                // Get current day timetable
+                val day =
+                    when {
+                        weekday >= 2 -> timetable.week[weekday-2]
+                        weekday > 1 -> timetable.week[1]
+                        else -> timetable.week[0]
+                    }
+
+                // Setup widget
+                views.setTextViewText(R.id.timetable_widget_title, "${timetable.teacher} - ${day.title}")
+                views.removeAllViews(R.id.timetable_widget_lessons)
+
+                // Setup views
+                for (l in day.lessons) {
+                    println(l)
+                    // Load lesson data
+                    if (l.group == "") continue
+                    val lesson = RemoteViews(context.packageName, R.layout.widget_tlesson)
+                    lesson.setTextViewText(R.id.widget_tlesson_title, l.title)
+                    lesson.setTextViewText(R.id.widget_tlesson_classroom, l.classroom)
+                    lesson.setTextViewText(R.id.widget_tlesson_group, l.group)
+                    lesson.setTextViewText(R.id.widget_tlesson_number, l.number)
+                    views.addView(R.id.timetable_widget_lessons, lesson)
+                }
+                // Update widget
+                appWidgetManager.updateAppWidget(appWidgetId, views)
+            }
+        })
+        appWidgetManager.updateAppWidget(appWidgetId, views)
+    }
+}

+ 5 - 5
app/src/main/java/com/ethosa/ktc/ui/widgets/TimetableWidget.kt

@@ -7,9 +7,9 @@ import android.appwidget.AppWidgetProvider
 import android.content.ComponentName
 import android.content.Context
 import android.content.Intent
-import android.content.SharedPreferences
 import android.os.Build
 import android.widget.RemoteViews
+import com.ethosa.ktc.Preferences
 import com.ethosa.ktc.R
 import com.ethosa.ktc.college.CollegeApi
 import com.ethosa.ktc.college.CollegeCallback
@@ -26,14 +26,14 @@ import java.util.*
  */
 class TimetableWidget : AppWidgetProvider() {
     private val college = CollegeApi()
-    private var preferences: SharedPreferences? = null
+    private var preferences: Preferences? = null
 
     override fun onUpdate(
         context: Context,
         appWidgetManager: AppWidgetManager,
         appWidgetIds: IntArray
     ) {
-        preferences = context.getSharedPreferences("com.ethosa.ktc", Context.MODE_PRIVATE)
+        preferences = Preferences(context)
         // There may be multiple widgets active, so update all of them
         for (appWidgetId in appWidgetIds) {
             updateAppWidget(context, appWidgetManager, appWidgetId)
@@ -100,11 +100,11 @@ class TimetableWidget : AppWidgetProvider() {
             openAppPendingIntent(context, appWidgetId)
         )
         // Load last group ID
-        val groupId = preferences?.getInt("group", 0)
+        val groupId = Preferences.group!!.id
         val calendar = Calendar.getInstance()
         val weekday = calendar.get(Calendar.DAY_OF_WEEK)
 
-        college.fetchTimetable(groupId!!, object : CollegeCallback {
+        college.fetchTimetable(groupId, object : CollegeCallback {
             @SuppressLint("SetTextI18n")
             override fun onResponse(call: Call, response: Response) {
                 // Parse JSON

+ 0 - 0
app/src/main/res/drawable-v21/ic_reload.xml → app/src/main/res/drawable/ic_reload.xml


+ 0 - 1
app/src/main/res/layout/widget_lesson.xml

@@ -72,7 +72,6 @@
                 android:lineSpacingExtra="-4sp"
                 android:padding="0dp"
                 android:text="@string/lesson_title"
-                android:textSize="14sp"
                 tools:textStyle="bold" />
 
             <LinearLayout

+ 65 - 0
app/src/main/res/layout/widget_tlesson.xml

@@ -0,0 +1,65 @@
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:orientation="vertical"
+    android:paddingTop="4dp"
+    android:paddingBottom="4dp">
+
+    <LinearLayout
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:gravity="center"
+        android:orientation="horizontal"
+        tools:ignore="UselessParent">
+
+        <TextView
+            android:id="@+id/widget_tlesson_number"
+            android:layout_width="wrap_content"
+            android:layout_height="match_parent"
+            android:layout_marginStart="8dp"
+            android:layout_marginEnd="4dp"
+            android:gravity="center"
+            android:text="@string/lesson_number"
+            android:textStyle="bold" />
+
+        <LinearLayout
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:gravity="center"
+            android:orientation="vertical">
+
+            <TextView
+                android:id="@+id/widget_tlesson_title"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:lineSpacingExtra="-4sp"
+                android:text="@string/lesson_title"
+                android:textStyle="bold" />
+
+            <LinearLayout
+                android:layout_width="wrap_content"
+                android:layout_height="match_parent"
+                android:gravity="center"
+                android:orientation="horizontal">
+
+                <TextView
+                    android:id="@+id/widget_tlesson_group"
+                    android:layout_width="wrap_content"
+                    android:layout_height="wrap_content"
+                    android:layout_marginEnd="4dp"
+                    android:text="@string/lesson_teacher" />
+
+                <TextView
+                    android:id="@+id/widget_tlesson_classroom"
+                    android:layout_width="0dp"
+                    android:layout_height="wrap_content"
+                    android:layout_marginStart="4dp"
+                    android:layout_weight="1"
+                    android:text="@string/lesson_classroom" />
+            </LinearLayout>
+
+        </LinearLayout>
+    </LinearLayout>
+
+</LinearLayout>

+ 1 - 1
app/src/main/res/menu/bottom_nav_menu.xml

@@ -18,6 +18,6 @@
 
     <item
         android:id="@+id/navigation_settings"
-        android:icon="@drawable/ic_settings"
+        android:icon="@drawable/ic_portrait"
         android:title="@string/title_settings" />
 </menu>

+ 4 - 2
app/src/main/res/values/strings.xml

@@ -5,7 +5,8 @@
     <string name="github">Github</string>
 
     <string name="add_widget">Расписание</string>
-    <string name="app_widget_description">Отображается расписание на сегодня</string>
+    <string name="app_widget_description">Виджет отображает расписание для студентов на сегодняшний день</string>
+    <string name="app_widget_teacher_description">Виджет отображает расписание для преподавателей на сегодняшний день</string>
 
     <string name="update_dialog_title">Доступно обновление!</string>
     <string name="update_dialog_positive">Обновить приложение</string>
@@ -15,7 +16,7 @@
     <string name="title_news">Новости</string>
     <string name="title_timetable">Расписание</string>
     <string name="title_gallery">Галерея</string>
-    <string name="title_settings">Настройки</string>
+    <string name="title_settings">Личный кабинет</string>
     <string name="title_about">О приложении</string>
     <string name="title_contact_info">Контактная информация</string>
     <string name="title_pro_college">ProCollege</string>
@@ -63,4 +64,5 @@
 
     <string name="placeholder">Placeholder</string>
     <string name="app_version_string">v0.0.1</string>
+    <string name="appwidget_text">EXAMPLE</string>
 </resources>

+ 1 - 0
app/src/main/res/values/themes.xml

@@ -1,4 +1,5 @@
 <resources xmlns:tools="http://schemas.android.com/tools">
+
     <style name="Theme.KTC" parent="Theme.MaterialComponents.DayNight.NoActionBar">
         <item name="colorPrimary">@color/primary</item>
         <item name="colorPrimaryDark">@color/primary_variant</item>

+ 16 - 0
app/src/main/res/xml/teacher_timetable_widget_info.xml

@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="utf-8"?>
+<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:description="@string/app_widget_teacher_description"
+    android:initialKeyguardLayout="@layout/widget_timetable"
+    android:initialLayout="@layout/widget_timetable"
+    android:minWidth="300dp"
+    android:minHeight="140dp"
+    android:previewImage="@drawable/ic_graduation_cap"
+    android:previewLayout="@layout/widget_timetable"
+    android:resizeMode="horizontal|vertical"
+    android:targetCellWidth="4"
+    android:targetCellHeight="3"
+    android:updatePeriodMillis="1800000"
+    android:widgetCategory="home_screen"
+    tools:targetApi="s" />

+ 1 - 1
app/src/main/res/xml/timetable_widget_info.xml

@@ -6,7 +6,7 @@
     android:initialLayout="@layout/widget_timetable"
     android:minWidth="300dp"
     android:minHeight="140dp"
-    android:previewImage="@drawable/ic_calendar"
+    android:previewImage="@drawable/ic_globe_alt"
     android:previewLayout="@layout/widget_timetable"
     android:resizeMode="horizontal|vertical"
     android:targetCellWidth="4"

+ 1 - 1
settings.gradle

@@ -7,7 +7,7 @@ pluginManagement {
     plugins {
         id 'com.android.application' version '7.1.1'
         id 'com.android.library' version '7.1.1'
-        id 'org.jetbrains.kotlin.android' version '1.6.10'
+        id 'org.jetbrains.kotlin.android' version '1.6.20'
     }
 }
 dependencyResolutionManagement {