Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

continue single category adoption and fallback to current database if download failed #1467

Merged
merged 5 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ android {

defaultConfig {
applicationId "com.money.manager.ex"
versionCode 1003
versionCode 1004
//versionName getVersionAsDate() + "." + versionCode
versionName "2023.12.06"
versionName "2023.12.11"
// $applicationId
setProperty("archivesBaseName", "ammx-$versionName")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,6 @@ private boolean isRemoteFileChanged(DatabaseMetadata metadata) {
* @param metadata Database file metadata.
*/
private void pullDatabase(DatabaseMetadata metadata) {
// Delete previous local file, if found.
File prevFile = new File(metadata.localPath);
boolean deleted = prevFile.delete();

// copy the contents into a local database file.
Uri uri = Uri.parse(metadata.remotePath);
try {
Expand Down Expand Up @@ -337,20 +333,27 @@ private void deleteRemoteFile(DatabaseMetadata metadata) {
* @param uri Remote Uri
* @throws IOException boom
*/
private void downloadDatabase(Uri uri, String localPath) throws IOException {
private void downloadDatabase(Uri uri, String localPath) throws Exception {
ContentResolver resolver = getContext().getContentResolver();

// Use try-with-resources to automatically close resources
try (FileOutputStream outputStream = new FileOutputStream(localPath);
File tempDatabaseFile = File.createTempFile("database", ".db", getContext().getFilesDir());
try (FileOutputStream outputStream = new FileOutputStream(tempDatabaseFile);
InputStream is = resolver.openInputStream(uri)) {

// Copy contents
long bytesCopied = ByteStreams.copy(is, outputStream);
Timber.i("copied %d bytes", bytesCopied);

} catch (Exception e) {
Timber.e(e);
return;
}

// Replace local database with downloaded version
File localDatabaseFile = new File(localPath);
Timber.i("%s %s %s", tempDatabaseFile.toPath(), localDatabaseFile.toPath(), localPath);
// StandardCopyOption.REPLACE_EXISTING ensures that the destination file is replaced if it exists
Files.move(tempDatabaseFile, localDatabaseFile);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ public interface ISplitTransaction
Integer getCategoryId();
void setCategoryId(int categoryId);

Integer getSubcategoryId();
void setSubcategoryId(Integer subCategoryId);

TransactionTypes getTransactionType(TransactionTypes parentTransactionType);
void setTransactionType(TransactionTypes value, TransactionTypes parentTransactionType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public String[] getAllColumns() {
SplitCategory.SPLITTRANSID,
SplitCategory.TRANSID,
SplitCategory.CATEGID,
SplitCategory.SUBCATEGID,
SplitCategory.SPLITTRANSAMOUNT };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public class SplitCategory
public static final String SPLITTRANSID = "SPLITTRANSID";
public static final String TRANSID = "TRANSID";
public static final String CATEGID = "CATEGID";
public static final String SUBCATEGID = "SUBCATEGID";
public static final String SPLITTRANSAMOUNT = "SPLITTRANSAMOUNT";

public static SplitCategory create(int transactionId, int categoryId, int subcategoryId,
Expand All @@ -54,7 +53,6 @@ public static SplitCategory create(int transactionId, int categoryId, int subcat

entity.setId(Constants.NOT_SET);
entity.setCategoryId(categoryId);
entity.setSubcategoryId(subcategoryId);
entity.setAmount(amount);
entity.setTransId(transactionId);

Expand Down Expand Up @@ -104,11 +102,6 @@ public Money getAmount() {
return getMoney(SPLITTRANSAMOUNT);
}

@Override
public Integer getSubcategoryId() {
return getInt(SUBCATEGID);
}

@Override
public void setCategoryId(int categoryId) {
setInt(CATEGID, categoryId);
Expand All @@ -119,11 +112,6 @@ public void setAmount(Money splitTransAmount) {
setMoney(SPLITTRANSAMOUNT, splitTransAmount);
}

@Override
public void setSubcategoryId(Integer subCategoryId) {
setInt(SUBCATEGID, subCategoryId);
}

@Override
public void loadFromCursor(Cursor c) {
super.loadFromCursor(c);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public static SplitRecurringCategory create(int transactionId, int categoryId, i
SplitRecurringCategory entity = new SplitRecurringCategory();

entity.setCategoryId(categoryId);
entity.setSubcategoryId(subcategoryId);
entity.setAmount(amount);
entity.setTransId(transactionId);

Expand Down Expand Up @@ -104,11 +103,6 @@ public Money getAmount() {
return getMoney(SPLITTRANSAMOUNT);
}

@Override
public Integer getSubcategoryId() {
return getInt(SUBCATEGID);
}

@Override
public void setCategoryId(int categId) {
setInt(CATEGID, categId);
Expand All @@ -119,11 +113,6 @@ public void setAmount(Money splitTransAmount) {
setMoney(SPLITTRANSAMOUNT, splitTransAmount);
}

@Override
public void setSubcategoryId(Integer subCategoryId) {
setInt(SUBCATEGID, subCategoryId);
}

@Override
public void loadFromCursor(Cursor c) {
super.loadFromCursor(c);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,17 @@
package com.money.manager.ex.home;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import android.text.TextUtils;

import com.money.manager.ex.MmexApplication;
import com.money.manager.ex.R;
import com.money.manager.ex.common.MmxBaseFragmentActivity;
import com.money.manager.ex.core.docstorage.DocFileMetadata;
import com.money.manager.ex.core.docstorage.FileStorageHelper;
import com.money.manager.ex.core.IntentFactory;
import com.money.manager.ex.core.RequestCodes;
import com.money.manager.ex.core.UIHelper;
import com.money.manager.ex.settings.AppSettings;
import com.money.manager.ex.settings.SyncPreferencesActivity;
import com.money.manager.ex.utils.MmxDatabaseUtils;
import com.money.manager.ex.core.docstorage.FileStorageHelper;
import com.money.manager.ex.utils.MmxFileUtils;

import javax.inject.Inject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,30 +87,27 @@ public int createNewSubcategory(String name, int categoryId) {
return ((int) id);
}

public String getCategorySubcategoryName(int categoryId, int subCategoryId) {
public String getCategorySubcategoryName(int categoryId) {
String categoryName = "";
String subCategoryName = "";

if (categoryId != Constants.NOT_SET) {
CategoryRepository categoryRepository = new CategoryRepository(getContext());
Category category = categoryRepository.load(categoryId);
categoryName = category != null
? category.getName()
: "n/a";
}
if (subCategoryId != Constants.NOT_SET) {
CategoryRepository categoryRepository = new CategoryRepository(getContext());
Category subcategory = categoryRepository.load(subCategoryId);
subCategoryName = subcategory != null
? subcategory.getName()
: "n/a";
if (category != null) {
categoryName = category.getName();
// TODO parent category : category
if (category.getParentId() > 0)
{
Category parentCategory = categoryRepository.load(category.getParentId());
if (parentCategory != null)
categoryName = parentCategory.getName() + " : " + category.getName();
}
} else {
categoryName = null;
}
}

String result = "";
if (!TextUtils.isEmpty(categoryName)) result += categoryName;
if (!TextUtils.isEmpty(subCategoryName)) result += ":" + subCategoryName;

return result;
return categoryName;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ private String getSplitCategory(ISplitTransaction split, String transactionType)

// category
CategoryService service = new CategoryService(getContext());
String category = service.getCategorySubcategoryName(split.getCategoryId(), split.getSubcategoryId());
String category = service.getCategorySubcategoryName(split.getCategoryId());
builder.append("S");
builder.append(category);
builder.append(lineSeparator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,21 @@
import android.util.Log;
import android.widget.Toast;

import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceScreen;

import com.afollestad.materialdialogs.MaterialDialog;
import com.money.manager.ex.BuildConfig;
import com.money.manager.ex.core.UIHelper;
import com.money.manager.ex.core.database.DatabaseManager;
import com.money.manager.ex.home.DatabaseMetadata;
import com.money.manager.ex.home.DatabaseMetadataFactory;
import com.money.manager.ex.MmexApplication;
import com.money.manager.ex.R;
import com.money.manager.ex.core.Core;
import com.money.manager.ex.core.UIHelper;
import com.money.manager.ex.core.database.DatabaseManager;
import com.money.manager.ex.database.DatabaseMigrator14To20;
import com.money.manager.ex.database.MmxOpenHelper;
import com.money.manager.ex.home.DatabaseMetadata;
import com.money.manager.ex.home.DatabaseMetadataFactory;
import com.money.manager.ex.home.MainActivity;
import com.money.manager.ex.home.RecentDatabasesProvider;
import com.money.manager.ex.utils.DonateDialogUtils;
Expand All @@ -44,9 +48,6 @@

import javax.inject.Inject;

import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceScreen;
import dagger.Lazy;
import timber.log.Timber;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {

ISplitTransaction split = mAdapter.splitTransactions.get(location);
split.setCategoryId(categoryId);
split.setSubcategoryId(subcategoryId);

mAdapter.notifyItemChanged(location);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private void bindAmount(ISplitTransaction splitTransaction, SplitItemViewHolder
private void bindCategory(Context context, SplitItemViewHolder holder, ISplitTransaction split) {
CategoryService service = new CategoryService(context);

String buttonText = service.getCategorySubcategoryName(split.getCategoryId(), split.getSubcategoryId());
String buttonText = service.getCategorySubcategoryName(split.getCategoryId());
holder.txtSelectCategory.setText(buttonText);
}

Expand Down
Loading