-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0f0668f
commit 4cd892d
Showing
17 changed files
with
382 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
app/src/main/java/net/devemperor/wristassist/activities/UsageActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package net.devemperor.wristassist.activities; | ||
|
||
import android.os.Bundle; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.ListView; | ||
import android.widget.TextView; | ||
|
||
import androidx.appcompat.app.AppCompatActivity; | ||
|
||
import net.devemperor.wristassist.R; | ||
import net.devemperor.wristassist.adapters.UsageAdapter; | ||
import net.devemperor.wristassist.database.UsageDatabaseHelper; | ||
|
||
import java.util.Locale; | ||
|
||
public class UsageActivity extends AppCompatActivity { | ||
|
||
ListView usageLv; | ||
Button resetUsageBtn; | ||
TextView totalCostTv; | ||
|
||
UsageDatabaseHelper usageDatabaseHelper; | ||
UsageAdapter usageAdapter; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_usage); | ||
|
||
usageDatabaseHelper = new UsageDatabaseHelper(this); | ||
usageAdapter = new UsageAdapter(this, usageDatabaseHelper.getAll()); | ||
usageLv = findViewById(R.id.usage_lv); | ||
usageLv.setAdapter(usageAdapter); | ||
|
||
View footerView = LayoutInflater.from(this).inflate(R.layout.layout_usage_footer, usageLv, false); | ||
resetUsageBtn = footerView.findViewById(R.id.usage_reset_btn); | ||
|
||
totalCostTv = footerView.findViewById(R.id.usage_total_cost_tv); | ||
totalCostTv.setText(getString(R.string.wristassist_total_cost, | ||
String.format(Locale.getDefault(), "%,.2f", usageDatabaseHelper.getTotalCost()))); | ||
|
||
usageLv.addFooterView(footerView); | ||
|
||
usageLv.requestFocus(); | ||
|
||
if (usageAdapter.getCount() == 0) { | ||
noUsage(); | ||
} | ||
} | ||
|
||
public void resetUsage(View view) { | ||
usageDatabaseHelper.reset(); | ||
usageAdapter.clear(); | ||
usageAdapter.addAll(usageDatabaseHelper.getAll()); | ||
usageAdapter.notifyDataSetChanged(); | ||
|
||
noUsage(); | ||
} | ||
|
||
private void noUsage() { | ||
totalCostTv.setText(getString(R.string.wristassist_no_usage_yet)); | ||
resetUsageBtn.setEnabled(false); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
app/src/main/java/net/devemperor/wristassist/adapters/UsageAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package net.devemperor.wristassist.adapters; | ||
|
||
import android.content.Context; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.ArrayAdapter; | ||
import android.widget.TextView; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import net.devemperor.wristassist.R; | ||
import net.devemperor.wristassist.database.UsageModel; | ||
import net.devemperor.wristassist.util.Util; | ||
|
||
import java.util.List; | ||
import java.util.Locale; | ||
|
||
|
||
public class UsageAdapter extends ArrayAdapter<UsageModel> { | ||
final Context context; | ||
final List<UsageModel> objects; | ||
|
||
|
||
public UsageAdapter(@NonNull Context context, @NonNull List<UsageModel> objects) { | ||
super(context, -1, objects); | ||
this.context = context; | ||
this.objects = objects; | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public View getView (int position, View convertView, @NonNull ViewGroup parent) { | ||
View listItem = LayoutInflater.from(context).inflate(R.layout.item_usage, parent, false); | ||
|
||
UsageModel dataProvider = objects.get(position); | ||
|
||
TextView modelNameTv = listItem.findViewById(R.id.usage_model_tv); | ||
modelNameTv.setText(Util.translateModelNames(dataProvider.getModelName())); | ||
modelNameTv.setTextSize(18 * Util.getFontMultiplier(context)); | ||
|
||
TextView tokensTv = listItem.findViewById(R.id.usage_tokens_tv); | ||
tokensTv.setText(context.getString(R.string.wristassist_token_usage, | ||
String.format(Locale.getDefault(), "%,d", dataProvider.getTokens()))); | ||
tokensTv.setTextSize(16 * Util.getFontMultiplier(context)); | ||
|
||
TextView costTv = listItem.findViewById(R.id.usage_cost_tv); | ||
costTv.setText(context.getString(R.string.wristassist_estimated_cost, | ||
String.format(Locale.getDefault(), "%,.2f", dataProvider.getCost()))); | ||
costTv.setTextSize(16 * Util.getFontMultiplier(context)); | ||
|
||
return listItem; | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
app/src/main/java/net/devemperor/wristassist/database/UsageDatabaseHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package net.devemperor.wristassist.database; | ||
|
||
import android.content.ContentValues; | ||
import android.content.Context; | ||
import android.database.Cursor; | ||
import android.database.sqlite.SQLiteDatabase; | ||
import android.database.sqlite.SQLiteOpenHelper; | ||
|
||
import androidx.annotation.Nullable; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class UsageDatabaseHelper extends SQLiteOpenHelper { | ||
|
||
Context context; | ||
|
||
public UsageDatabaseHelper(@Nullable Context context) { | ||
super(context, "usage.db", null, 1); | ||
this.context = context; | ||
} | ||
|
||
@Override | ||
public void onCreate(SQLiteDatabase db) { | ||
db.execSQL("CREATE TABLE USAGE (MODEL_NAME TEXT PRIMARY KEY, TOKENS LONG, COST DOUBLE)"); | ||
} | ||
|
||
@Override | ||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } | ||
|
||
public void edit(String model, long tokensToAdd, double costToAdd) { | ||
SQLiteDatabase db = this.getWritableDatabase(); | ||
Cursor cursor = db.rawQuery("SELECT * FROM USAGE WHERE MODEL_NAME='" + model + "'", null); | ||
|
||
boolean entryExists = cursor.moveToFirst(); | ||
cursor.close(); | ||
|
||
if (!entryExists) { | ||
ContentValues cv = new ContentValues(); | ||
cv.put("MODEL_NAME", model); | ||
cv.put("TOKENS", tokensToAdd); | ||
cv.put("COST", costToAdd); | ||
db.insert("USAGE", null, cv); | ||
} else { | ||
cursor = db.rawQuery("SELECT * FROM USAGE WHERE MODEL_NAME='" + model + "'", null); | ||
if (cursor.moveToFirst()) { | ||
long lastTokens = cursor.getLong(1); | ||
double lastCost = cursor.getDouble(2); | ||
ContentValues cv = new ContentValues(); | ||
cv.put("TOKENS", lastTokens + tokensToAdd); | ||
cv.put("COST", lastCost + costToAdd); | ||
db.update("USAGE", cv, "MODEL_NAME='" + model + "'", null); | ||
} | ||
cursor.close(); | ||
} | ||
|
||
db.close(); | ||
} | ||
|
||
public void reset() { | ||
SQLiteDatabase db = this.getWritableDatabase(); | ||
db.execSQL("DELETE FROM USAGE"); | ||
db.close(); | ||
} | ||
|
||
public List<UsageModel> getAll() { | ||
SQLiteDatabase db = this.getWritableDatabase(); | ||
Cursor cursor = db.rawQuery("SELECT * FROM USAGE", null); | ||
|
||
List<UsageModel> models = new ArrayList<>(); | ||
if (cursor.moveToFirst()) { | ||
do { | ||
models.add(new UsageModel(cursor.getString(0), cursor.getLong(1), cursor.getDouble(2))); | ||
} while (cursor.moveToNext()); | ||
} | ||
cursor.close(); | ||
db.close(); | ||
return models; | ||
} | ||
|
||
public double getTotalCost() { | ||
SQLiteDatabase db = this.getWritableDatabase(); | ||
Cursor cursor = db.rawQuery("SELECT SUM(COST) FROM USAGE", null); | ||
|
||
double totalCost = 0; | ||
if (cursor.moveToFirst()) { | ||
totalCost = cursor.getDouble(0); | ||
} | ||
cursor.close(); | ||
db.close(); | ||
return totalCost; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
app/src/main/java/net/devemperor/wristassist/database/UsageModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package net.devemperor.wristassist.database; | ||
|
||
public class UsageModel { | ||
private final String modelName; | ||
private final long tokens; | ||
private final double cost; | ||
|
||
public UsageModel(String modelName, long tokens, double cost) { | ||
this.modelName = modelName; | ||
this.tokens = tokens; | ||
this.cost = cost; | ||
} | ||
|
||
public String getModelName() { | ||
return modelName; | ||
} | ||
|
||
public long getTokens() { | ||
return tokens; | ||
} | ||
|
||
public double getCost() { | ||
return cost; | ||
} | ||
} |
Oops, something went wrong.