From f32367ea81c5d4e9b2cb2748e858eb42110e8f30 Mon Sep 17 00:00:00 2001 From: Fernando Maldonado Date: Tue, 4 May 2021 20:36:55 -0500 Subject: [PATCH 1/3] Added auto updater --- mobile_app/app/build.gradle | 5 +- mobile_app/app/src/main/AndroidManifest.xml | 12 ++++ .../data/models/version/GithubVersion.kt | 13 ++++ .../data/network/LatestVersionDataSource.kt | 9 +++ .../network/interceptor/NetworkInterceptor.kt | 2 +- .../repositories/updater/UpdaterRepository.kt | 64 +++++++++++++++++++ .../akiyama/data/utils/ApkInstaller.kt | 42 ++++++++++++ .../fmaldonado/akiyama/di/NetworkModule.kt | 24 +++++++ .../ui/activities/main/MainActivity.kt | 38 ++++++++++- .../activities/main/MainActivityViewModel.kt | 33 ++++++++++ .../main/fragments/more/PreferenceKeys.kt | 1 + .../main/fragments/more/SettingsFragment.kt | 35 +++++----- .../fmaldonado/akiyama/ui/common/Status.kt | 13 ++++ .../downloadDialog/DownloadDialog.kt | 53 +++++++++++++++ .../downloadDialog/DownloadDialogViewModel.kt | 32 ++++++++++ .../src/main/res/drawable/download_icon.xml | 10 +++ .../res/layout/download_dialog_fragment.xml | 38 +++++++++++ .../app/src/main/res/values/strings.xml | 7 ++ .../app/src/main/res/xml/preferences.xml | 4 ++ .../app/src/main/res/xml/provider_paths.xml | 9 +++ 20 files changed, 424 insertions(+), 20 deletions(-) create mode 100644 mobile_app/app/src/main/java/com/fmaldonado/akiyama/data/models/version/GithubVersion.kt create mode 100644 mobile_app/app/src/main/java/com/fmaldonado/akiyama/data/network/LatestVersionDataSource.kt create mode 100644 mobile_app/app/src/main/java/com/fmaldonado/akiyama/data/repositories/updater/UpdaterRepository.kt create mode 100644 mobile_app/app/src/main/java/com/fmaldonado/akiyama/data/utils/ApkInstaller.kt create mode 100644 mobile_app/app/src/main/java/com/fmaldonado/akiyama/ui/activities/main/MainActivityViewModel.kt create mode 100644 mobile_app/app/src/main/java/com/fmaldonado/akiyama/ui/common/fragments/downloadDialog/DownloadDialog.kt create mode 100644 mobile_app/app/src/main/java/com/fmaldonado/akiyama/ui/common/fragments/downloadDialog/DownloadDialogViewModel.kt create mode 100644 mobile_app/app/src/main/res/drawable/download_icon.xml create mode 100644 mobile_app/app/src/main/res/layout/download_dialog_fragment.xml create mode 100644 mobile_app/app/src/main/res/xml/provider_paths.xml diff --git a/mobile_app/app/build.gradle b/mobile_app/app/build.gradle index e1a6e5f..381ac89 100644 --- a/mobile_app/app/build.gradle +++ b/mobile_app/app/build.gradle @@ -22,7 +22,7 @@ android { minSdkVersion 23 targetSdkVersion 30 versionCode 2 - versionName "2.0" + versionName "v2.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } @@ -32,10 +32,12 @@ android { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' buildConfigField "String", "BASE_URL", "\"https://akiyamarest.herokuapp.com/\"" + buildConfigField "String", "GITHUB_URL", "\"https://api.github.com/repos/Fmaldonado6/Akiyama/releases/\"" } debug { + buildConfigField "String", "GITHUB_URL", "\"https://api.github.com/repos/Fmaldonado6/Akiyama/releases/\"" buildConfigField "String", "BASE_URL", "\"http://10.0.2.2:4000/\"" applicationIdSuffix ".debug" @@ -73,6 +75,7 @@ dependencies { implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.google.code.gson:gson:2.8.6' implementation 'com.squareup.retrofit2:converter-gson:2.9.0' + implementation("ru.gildor.coroutines:kotlin-coroutines-okhttp:1.0") //Glide implementation 'com.github.bumptech.glide:glide:4.12.0' diff --git a/mobile_app/app/src/main/AndroidManifest.xml b/mobile_app/app/src/main/AndroidManifest.xml index 6868201..9730e34 100644 --- a/mobile_app/app/src/main/AndroidManifest.xml +++ b/mobile_app/app/src/main/AndroidManifest.xml @@ -3,6 +3,8 @@ package="com.fmaldonado.akiyama"> + + + + + + \ No newline at end of file diff --git a/mobile_app/app/src/main/java/com/fmaldonado/akiyama/data/models/version/GithubVersion.kt b/mobile_app/app/src/main/java/com/fmaldonado/akiyama/data/models/version/GithubVersion.kt new file mode 100644 index 0000000..475ceaf --- /dev/null +++ b/mobile_app/app/src/main/java/com/fmaldonado/akiyama/data/models/version/GithubVersion.kt @@ -0,0 +1,13 @@ +package com.fmaldonado.akiyama.data.models.version + +data class GithubVersion( + val tag_name: String, + val name: String, + val prerelease: Boolean, + val assets: List +) + +data class GithubAsset( + val name: String, + val browser_download_url: String +) diff --git a/mobile_app/app/src/main/java/com/fmaldonado/akiyama/data/network/LatestVersionDataSource.kt b/mobile_app/app/src/main/java/com/fmaldonado/akiyama/data/network/LatestVersionDataSource.kt new file mode 100644 index 0000000..fb6f5d2 --- /dev/null +++ b/mobile_app/app/src/main/java/com/fmaldonado/akiyama/data/network/LatestVersionDataSource.kt @@ -0,0 +1,9 @@ +package com.fmaldonado.akiyama.data.network + +import com.fmaldonado.akiyama.data.models.version.GithubVersion +import retrofit2.http.GET + +interface LatestVersionDataSource { + @GET("latest") + suspend fun getLatestRelease(): GithubVersion +} \ No newline at end of file diff --git a/mobile_app/app/src/main/java/com/fmaldonado/akiyama/data/network/interceptor/NetworkInterceptor.kt b/mobile_app/app/src/main/java/com/fmaldonado/akiyama/data/network/interceptor/NetworkInterceptor.kt index cfd0655..abab888 100644 --- a/mobile_app/app/src/main/java/com/fmaldonado/akiyama/data/network/interceptor/NetworkInterceptor.kt +++ b/mobile_app/app/src/main/java/com/fmaldonado/akiyama/data/network/interceptor/NetworkInterceptor.kt @@ -24,4 +24,4 @@ class NetworkInterceptor : Interceptor { throw AppError(response.body()) } -} \ No newline at end of file +} diff --git a/mobile_app/app/src/main/java/com/fmaldonado/akiyama/data/repositories/updater/UpdaterRepository.kt b/mobile_app/app/src/main/java/com/fmaldonado/akiyama/data/repositories/updater/UpdaterRepository.kt new file mode 100644 index 0000000..844d965 --- /dev/null +++ b/mobile_app/app/src/main/java/com/fmaldonado/akiyama/data/repositories/updater/UpdaterRepository.kt @@ -0,0 +1,64 @@ +package com.fmaldonado.akiyama.data.repositories.updater + + +import androidx.lifecycle.MutableLiveData +import com.fmaldonado.akiyama.AkiyamaApplication +import com.fmaldonado.akiyama.BuildConfig +import com.fmaldonado.akiyama.data.models.version.GithubVersion +import com.fmaldonado.akiyama.data.network.LatestVersionDataSource +import com.fmaldonado.akiyama.ui.common.UpdateEvents +import com.fmaldonado.akiyama.ui.common.UpdateStatus +import kotlinx.coroutines.delay +import okhttp3.OkHttpClient +import okhttp3.Request +import ru.gildor.coroutines.okhttp.await +import java.io.File +import javax.inject.Inject +import javax.inject.Singleton + +@Singleton +class UpdaterRepository +@Inject +constructor( + private val latestVersionDataSource: LatestVersionDataSource, + private val okHttpClient: OkHttpClient, + private val application: AkiyamaApplication +) { + + val hasNewVersion = MutableLiveData() + val newVersionUri = MutableLiveData() + private var version: GithubVersion? = null + + suspend fun lookForNewVersion(manualClick: Boolean) { + if (manualClick){ + hasNewVersion.postValue(UpdateEvents(UpdateStatus.Looking, manualClick)) + delay(1000) + } + version = latestVersionDataSource.getLatestRelease() + version?.let { + if (it.prerelease) + return + val status = + if (it.tag_name != BuildConfig.VERSION_NAME) UpdateStatus.NewUpdate else UpdateStatus.Updated + hasNewVersion.postValue( + UpdateEvents( + status, + manualClick + ) + ) + } + } + + suspend fun downloadLatest() { + version?.let { + val asset = it.assets.firstOrNull() ?: throw Exception() + val req = Request.Builder().url(asset.browser_download_url).build() + val res = okHttpClient.newCall(req).await() + val bytes = res.body()?.byteStream() ?: throw Exception() + val file = File(application.externalCacheDir, "update.apk") + bytes.copyTo(file.outputStream()) + newVersionUri.postValue(file) + } + } + +} \ No newline at end of file diff --git a/mobile_app/app/src/main/java/com/fmaldonado/akiyama/data/utils/ApkInstaller.kt b/mobile_app/app/src/main/java/com/fmaldonado/akiyama/data/utils/ApkInstaller.kt new file mode 100644 index 0000000..4d1a730 --- /dev/null +++ b/mobile_app/app/src/main/java/com/fmaldonado/akiyama/data/utils/ApkInstaller.kt @@ -0,0 +1,42 @@ +package com.fmaldonado.akiyama.data.utils + +import android.content.ActivityNotFoundException +import android.content.Context +import android.content.Intent +import android.net.Uri +import android.os.Build +import android.util.Log +import androidx.core.content.FileProvider +import com.fmaldonado.akiyama.BuildConfig +import java.io.File + + +object ApkInstaller { + fun installApplication(context: Context, file: File) { + val intent = Intent(Intent.ACTION_VIEW) + intent.setDataAndType( + uriFromFile(context, file), + "application/vnd.android.package-archive" + ) + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + try { + context.startActivity(intent) + } catch (e: ActivityNotFoundException) { + e.printStackTrace() + Log.e("TAG", "Error in opening the file!") + } + } + + private fun uriFromFile(context: Context, file: File): Uri? { + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + FileProvider.getUriForFile( + context, + BuildConfig.APPLICATION_ID + ".provider", + file + ) + } else { + Uri.fromFile(file) + } + } +} \ No newline at end of file diff --git a/mobile_app/app/src/main/java/com/fmaldonado/akiyama/di/NetworkModule.kt b/mobile_app/app/src/main/java/com/fmaldonado/akiyama/di/NetworkModule.kt index 833aafa..117b262 100644 --- a/mobile_app/app/src/main/java/com/fmaldonado/akiyama/di/NetworkModule.kt +++ b/mobile_app/app/src/main/java/com/fmaldonado/akiyama/di/NetworkModule.kt @@ -1,6 +1,7 @@ package com.fmaldonado.akiyama.di import com.fmaldonado.akiyama.BuildConfig +import com.fmaldonado.akiyama.data.network.LatestVersionDataSource import com.fmaldonado.akiyama.data.network.NetworkDataSource import com.fmaldonado.akiyama.data.network.interceptor.NetworkInterceptor import dagger.Module @@ -34,6 +35,29 @@ object NetworkModule { .create(NetworkDataSource::class.java) } + @Singleton + @Provides + fun provideOkHttpClient(networkInterceptor: NetworkInterceptor): OkHttpClient { + return OkHttpClient.Builder().addInterceptor(networkInterceptor).build() + } + + @Singleton + @Provides + fun provideLatestVersionDataSource(networkInterceptor: NetworkInterceptor): LatestVersionDataSource { + val specs = listOf(ConnectionSpec.CLEARTEXT, ConnectionSpec.MODERN_TLS) + val okHttpClient = + OkHttpClient.Builder() + .addInterceptor(networkInterceptor) + .build() + + return Retrofit.Builder() + .baseUrl(BuildConfig.GITHUB_URL) + .client(okHttpClient) + .addConverterFactory(GsonConverterFactory.create()) + .build() + .create(LatestVersionDataSource::class.java) + } + @Singleton @Provides fun provideNetworkInterceptor(): NetworkInterceptor = NetworkInterceptor() diff --git a/mobile_app/app/src/main/java/com/fmaldonado/akiyama/ui/activities/main/MainActivity.kt b/mobile_app/app/src/main/java/com/fmaldonado/akiyama/ui/activities/main/MainActivity.kt index 9073cfd..c4fe3e7 100644 --- a/mobile_app/app/src/main/java/com/fmaldonado/akiyama/ui/activities/main/MainActivity.kt +++ b/mobile_app/app/src/main/java/com/fmaldonado/akiyama/ui/activities/main/MainActivity.kt @@ -2,16 +2,20 @@ package com.fmaldonado.akiyama.ui.activities.main import androidx.appcompat.app.AppCompatActivity import android.os.Bundle +import androidx.activity.viewModels import androidx.navigation.findNavController import androidx.navigation.fragment.NavHostFragment import androidx.navigation.ui.setupWithNavController import com.fmaldonado.akiyama.R import com.fmaldonado.akiyama.databinding.ActivityMainBinding +import com.fmaldonado.akiyama.ui.common.UpdateStatus +import com.fmaldonado.akiyama.ui.common.fragments.downloadDialog.DownloadDialog +import com.google.android.material.snackbar.Snackbar import dagger.hilt.android.AndroidEntryPoint @AndroidEntryPoint class MainActivity : AppCompatActivity() { - + private val viewModel: MainActivityViewModel by viewModels() private lateinit var binding: ActivityMainBinding override fun onCreate(savedInstanceState: Bundle?) { @@ -22,5 +26,37 @@ class MainActivity : AppCompatActivity() { val navHost = supportFragmentManager.findFragmentById(R.id.hostFragment) as NavHostFragment val navController = navHost.navController binding.bottomNav.setupWithNavController(navController) + viewModel.checkLatestVersion() + + viewModel.hasNewVersion.observe(this, { + val manual = it.manualClick + when (it.status) { + UpdateStatus.Looking -> Snackbar.make( + binding.bottomNav, + getString(R.string.looking_for_update_text), + Snackbar.LENGTH_SHORT + ).show() + UpdateStatus.NewUpdate -> Snackbar.make( + binding.bottomNav, + getString(R.string.new_update_text), + Snackbar.LENGTH_SHORT + ).setAction("Download") { + viewModel.checkLatestVersion(manual) + }.show() + UpdateStatus.Updated -> + if (it.manualClick) Snackbar.make( + binding.bottomNav, + getString(R.string.updated_text), + Snackbar.LENGTH_SHORT + ).show() + UpdateStatus.Error -> Snackbar.make( + binding.bottomNav, + getString(R.string.error_update_text), + Snackbar.LENGTH_SHORT + ).show() + } + }) + } + } \ No newline at end of file diff --git a/mobile_app/app/src/main/java/com/fmaldonado/akiyama/ui/activities/main/MainActivityViewModel.kt b/mobile_app/app/src/main/java/com/fmaldonado/akiyama/ui/activities/main/MainActivityViewModel.kt new file mode 100644 index 0000000..40e8c39 --- /dev/null +++ b/mobile_app/app/src/main/java/com/fmaldonado/akiyama/ui/activities/main/MainActivityViewModel.kt @@ -0,0 +1,33 @@ +package com.fmaldonado.akiyama.ui.activities.main + +import android.util.Log +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import com.fmaldonado.akiyama.data.repositories.updater.UpdaterRepository +import com.fmaldonado.akiyama.ui.common.UpdateEvents +import com.fmaldonado.akiyama.ui.common.UpdateStatus +import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import java.lang.Exception +import javax.inject.Inject + +@HiltViewModel +class MainActivityViewModel +@Inject +constructor( + private val updaterRepository: UpdaterRepository +) : ViewModel() { + + val hasNewVersion = updaterRepository.hasNewVersion + fun checkLatestVersion(manual: Boolean = false) { + viewModelScope.launch(Dispatchers.IO) { + try { + updaterRepository.lookForNewVersion(manual) + } catch (e: Exception) { + updaterRepository.hasNewVersion.postValue(UpdateEvents(UpdateStatus.Error, manual)) + } + } + } + +} \ No newline at end of file diff --git a/mobile_app/app/src/main/java/com/fmaldonado/akiyama/ui/activities/main/fragments/more/PreferenceKeys.kt b/mobile_app/app/src/main/java/com/fmaldonado/akiyama/ui/activities/main/fragments/more/PreferenceKeys.kt index 3adede0..1f37ebd 100644 --- a/mobile_app/app/src/main/java/com/fmaldonado/akiyama/ui/activities/main/fragments/more/PreferenceKeys.kt +++ b/mobile_app/app/src/main/java/com/fmaldonado/akiyama/ui/activities/main/fragments/more/PreferenceKeys.kt @@ -3,6 +3,7 @@ package com.fmaldonado.akiyama.ui.activities.main.fragments.more object PreferenceKeys { val ABOUT = "about" val TWITTER = "twitter" + val CHECK_UPDATES = "updates" val VERSION = "version" val SOURCE_CODE = "sourceCode" diff --git a/mobile_app/app/src/main/java/com/fmaldonado/akiyama/ui/activities/main/fragments/more/SettingsFragment.kt b/mobile_app/app/src/main/java/com/fmaldonado/akiyama/ui/activities/main/fragments/more/SettingsFragment.kt index 5a79f1c..f074f04 100644 --- a/mobile_app/app/src/main/java/com/fmaldonado/akiyama/ui/activities/main/fragments/more/SettingsFragment.kt +++ b/mobile_app/app/src/main/java/com/fmaldonado/akiyama/ui/activities/main/fragments/more/SettingsFragment.kt @@ -5,33 +5,29 @@ import android.net.Uri import android.os.Bundle import android.util.Log import android.view.View +import androidx.fragment.app.activityViewModels import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat +import com.fmaldonado.akiyama.BuildConfig import com.fmaldonado.akiyama.R import com.fmaldonado.akiyama.ui.activities.about.AboutActivity +import com.fmaldonado.akiyama.ui.activities.main.MainActivityViewModel +import dagger.hilt.android.AndroidEntryPoint import java.lang.Exception +@AndroidEntryPoint class SettingsFragment : PreferenceFragmentCompat() { + private val viewModel: MainActivityViewModel by activityViewModels() override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { addPreferencesFromResource(R.xml.preferences) - try { - val pInfo = - requireContext() - .packageManager?.getPackageInfo(requireContext().packageName, 0) - - pInfo?.let { - val pref = findPreference(PreferenceKeys.VERSION) as Preference? - pref?.let { - it.title = String.format( - resources.getString(R.string.preferences_version_text), - pInfo.versionName - ) - } - } - - } catch (e: Exception) { - Log.e(this.tag, "Error", e) + val pref = findPreference(PreferenceKeys.VERSION) as Preference? + pref?.let { + it.title = String.format( + resources.getString(R.string.preferences_version_text), + BuildConfig.VERSION_NAME + ) } + } override fun onPreferenceTreeClick(preference: Preference?): Boolean { @@ -42,6 +38,7 @@ class SettingsFragment : PreferenceFragmentCompat() { PreferenceKeys.ABOUT -> startIntent(AboutActivity::class.java) PreferenceKeys.TWITTER -> startURLIntent("https://twitter.com/Fmaldonado4202") PreferenceKeys.SOURCE_CODE -> startURLIntent("https://github.com/Fmaldonado6/Akiyama") + PreferenceKeys.CHECK_UPDATES -> checkUpdates() else -> Log.d(this.tag, "Unregistered preference") } @@ -50,6 +47,10 @@ class SettingsFragment : PreferenceFragmentCompat() { return super.onPreferenceTreeClick(preference) } + private fun checkUpdates() { + viewModel.checkLatestVersion(true) + } + private fun startURLIntent(url: String) { val intent = Intent( Intent.ACTION_VIEW, diff --git a/mobile_app/app/src/main/java/com/fmaldonado/akiyama/ui/common/Status.kt b/mobile_app/app/src/main/java/com/fmaldonado/akiyama/ui/common/Status.kt index ea7adc0..d10adc7 100644 --- a/mobile_app/app/src/main/java/com/fmaldonado/akiyama/ui/common/Status.kt +++ b/mobile_app/app/src/main/java/com/fmaldonado/akiyama/ui/common/Status.kt @@ -7,6 +7,19 @@ enum class Status(value: Int) { Error(4), } +data class UpdateEvents( + val status: UpdateStatus, + val manualClick: Boolean +) + + +enum class UpdateStatus(value: Int) { + Looking(0), + NewUpdate(1), + Updated(2), + Error(3) +} + enum class FavoritesStatus(value: Int) { Added(0), Removed(1), diff --git a/mobile_app/app/src/main/java/com/fmaldonado/akiyama/ui/common/fragments/downloadDialog/DownloadDialog.kt b/mobile_app/app/src/main/java/com/fmaldonado/akiyama/ui/common/fragments/downloadDialog/DownloadDialog.kt new file mode 100644 index 0000000..ced963c --- /dev/null +++ b/mobile_app/app/src/main/java/com/fmaldonado/akiyama/ui/common/fragments/downloadDialog/DownloadDialog.kt @@ -0,0 +1,53 @@ +package com.fmaldonado.akiyama.ui.common.fragments.downloadDialog + +import android.app.ActionBar +import android.app.Dialog +import android.content.Intent +import android.os.Bundle +import android.util.Log +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.core.content.ContextCompat +import androidx.core.content.FileProvider +import androidx.fragment.app.DialogFragment +import androidx.fragment.app.viewModels +import com.fmaldonado.akiyama.BuildConfig +import com.fmaldonado.akiyama.data.utils.ApkInstaller +import com.fmaldonado.akiyama.databinding.DownloadDialogFragmentBinding +import com.google.android.material.dialog.MaterialAlertDialogBuilder +import dagger.hilt.android.AndroidEntryPoint + + +@AndroidEntryPoint +class DownloadDialog : DialogFragment() { + private lateinit var binding: DownloadDialogFragmentBinding + private val viewModel: DownloadDialogViewModel by viewModels() + + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { + val builder = MaterialAlertDialogBuilder(requireActivity()) + binding = DownloadDialogFragmentBinding.inflate(requireActivity().layoutInflater) + builder.setView(binding.root) + return builder.create() + } + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + return binding.root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + Log.d("BUenas", "AAAAAAAAAA") + viewModel.downloadLatest() + viewModel.file.observe(viewLifecycleOwner, { + this.dismiss() + ApkInstaller.installApplication(requireContext(), it) + }) + + } + +} \ No newline at end of file diff --git a/mobile_app/app/src/main/java/com/fmaldonado/akiyama/ui/common/fragments/downloadDialog/DownloadDialogViewModel.kt b/mobile_app/app/src/main/java/com/fmaldonado/akiyama/ui/common/fragments/downloadDialog/DownloadDialogViewModel.kt new file mode 100644 index 0000000..ae08454 --- /dev/null +++ b/mobile_app/app/src/main/java/com/fmaldonado/akiyama/ui/common/fragments/downloadDialog/DownloadDialogViewModel.kt @@ -0,0 +1,32 @@ +package com.fmaldonado.akiyama.ui.common.fragments.downloadDialog + +import android.util.Log +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import com.fmaldonado.akiyama.data.repositories.updater.UpdaterRepository +import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import java.lang.Exception +import javax.inject.Inject + +@HiltViewModel +class DownloadDialogViewModel +@Inject +constructor( + private val updaterRepository: UpdaterRepository +) : ViewModel() { + + val file = updaterRepository.newVersionUri + fun downloadLatest() { + viewModelScope.launch(Dispatchers.IO) { + try { + Log.d("Downloading", "Yes") + updaterRepository.downloadLatest() + } catch (e: Exception) { + Log.e("Error", "e", e) + } + } + } + +} \ No newline at end of file diff --git a/mobile_app/app/src/main/res/drawable/download_icon.xml b/mobile_app/app/src/main/res/drawable/download_icon.xml new file mode 100644 index 0000000..dc16366 --- /dev/null +++ b/mobile_app/app/src/main/res/drawable/download_icon.xml @@ -0,0 +1,10 @@ + + + diff --git a/mobile_app/app/src/main/res/layout/download_dialog_fragment.xml b/mobile_app/app/src/main/res/layout/download_dialog_fragment.xml new file mode 100644 index 0000000..8e53219 --- /dev/null +++ b/mobile_app/app/src/main/res/layout/download_dialog_fragment.xml @@ -0,0 +1,38 @@ + + + + + + + + \ No newline at end of file diff --git a/mobile_app/app/src/main/res/values/strings.xml b/mobile_app/app/src/main/res/values/strings.xml index e69dc19..cd836da 100644 --- a/mobile_app/app/src/main/res/values/strings.xml +++ b/mobile_app/app/src/main/res/values/strings.xml @@ -43,11 +43,18 @@ About Twitter + Check for updates Version: %1$s Source Code Work in progress + Looking for updates... + No updates available + New update available + Couldn\'t get latest update + + Akiyama is an open-source anime website that uses the Unofficial AnimeFLV REST API, developed with the objective of learning and improving my coding skills. diff --git a/mobile_app/app/src/main/res/xml/preferences.xml b/mobile_app/app/src/main/res/xml/preferences.xml index 64f57a0..67e62a1 100644 --- a/mobile_app/app/src/main/res/xml/preferences.xml +++ b/mobile_app/app/src/main/res/xml/preferences.xml @@ -10,6 +10,10 @@ android:icon="@drawable/twitter_icon" android:key="twitter" android:title="@string/preferences_twitter_text" /> + + + + + \ No newline at end of file From 265950055b33c7eec0b122aa91c802a2950ab543 Mon Sep 17 00:00:00 2001 From: Fernando Maldonado Date: Tue, 4 May 2021 20:41:36 -0500 Subject: [PATCH 2/3] Added auto updater --- mobile_app/app/build.gradle | 4 ++-- .../data/repositories/updater/UpdaterRepository.kt | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/mobile_app/app/build.gradle b/mobile_app/app/build.gradle index 381ac89..7ba6ba5 100644 --- a/mobile_app/app/build.gradle +++ b/mobile_app/app/build.gradle @@ -21,8 +21,8 @@ android { applicationId "com.fmaldonado.akiyama" minSdkVersion 23 targetSdkVersion 30 - versionCode 2 - versionName "v2.0" + versionCode 3 + versionName "v2.1" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } diff --git a/mobile_app/app/src/main/java/com/fmaldonado/akiyama/data/repositories/updater/UpdaterRepository.kt b/mobile_app/app/src/main/java/com/fmaldonado/akiyama/data/repositories/updater/UpdaterRepository.kt index 844d965..5dcfcd4 100644 --- a/mobile_app/app/src/main/java/com/fmaldonado/akiyama/data/repositories/updater/UpdaterRepository.kt +++ b/mobile_app/app/src/main/java/com/fmaldonado/akiyama/data/repositories/updater/UpdaterRepository.kt @@ -30,7 +30,7 @@ constructor( private var version: GithubVersion? = null suspend fun lookForNewVersion(manualClick: Boolean) { - if (manualClick){ + if (manualClick) { hasNewVersion.postValue(UpdateEvents(UpdateStatus.Looking, manualClick)) delay(1000) } @@ -38,8 +38,10 @@ constructor( version?.let { if (it.prerelease) return - val status = - if (it.tag_name != BuildConfig.VERSION_NAME) UpdateStatus.NewUpdate else UpdateStatus.Updated + val status = if (it.tag_name != BuildConfig.VERSION_NAME && !BuildConfig.DEBUG) + UpdateStatus.NewUpdate + else + UpdateStatus.Updated hasNewVersion.postValue( UpdateEvents( status, From 2e10c58f836d8d04600cff70df2d0a06b45eca39 Mon Sep 17 00:00:00 2001 From: Fernando Maldonado Date: Tue, 4 May 2021 20:42:50 -0500 Subject: [PATCH 3/3] Added auto updater --- mobile_app/app/release/output-metadata.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mobile_app/app/release/output-metadata.json b/mobile_app/app/release/output-metadata.json index b10733a..f3b6fd5 100644 --- a/mobile_app/app/release/output-metadata.json +++ b/mobile_app/app/release/output-metadata.json @@ -10,8 +10,8 @@ { "type": "SINGLE", "filters": [], - "versionCode": 2, - "versionName": "2.0", + "versionCode": 3, + "versionName": "v2.1", "outputFile": "app-release.apk" } ]