Skip to content

Commit

Permalink
database working + added more activities
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew committed Nov 20, 2024
1 parent 1a38570 commit 1b95887
Show file tree
Hide file tree
Showing 16 changed files with 502 additions and 15 deletions.
6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/deploymentTargetSelector.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 13 additions & 7 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,25 @@
android:theme="@style/Theme.KomodoHub"
tools:targetApi="31">
<activity
android:name=".ui.login.LoginActivity"
android:exported="false"
android:label="@string/title_activity_login" />
<activity
android:name=".MainActivity"
android:name=".LoginActivity"
android:exported="true"
android:theme="@style/Theme.KomodoHub">
android:label="@string/title_activity_login" >

<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/Theme.KomodoHub">
</activity>
<activity
android:name=".SearchActivity"
android:exported="false"
android:theme="@style/Theme.KomodoHub">
</activity>
</application>

</manifest>
111 changes: 111 additions & 0 deletions app/src/main/java/com/example/komodohub/DatabaseAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.example.komodohub;

import android.annotation.SuppressLint;
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.NonNull;

import com.example.komodohub.models.User;

public class DatabaseAdapter {
DatabaseHelper dbHelper;

public DatabaseAdapter(Context context) {
dbHelper = new DatabaseHelper(context);
}

public long insertUser(String username, String password) {
SQLiteDatabase db = dbHelper.getWritableDatabase();

ContentValues contentValues = new ContentValues();
contentValues.putNull("Id");
contentValues.put("Username", username);
contentValues.put("Password", password);
contentValues.putNull("CommunityId");
contentValues.put("IsCommunityOwner", 0);
contentValues.put("IsAdmin", 0);

long id = db.insert("Users", null, contentValues);
return id;
}

public User getUserByName(String username)
{
SQLiteDatabase db = dbHelper.getWritableDatabase();

Cursor c = db.rawQuery("SELECT * FROM Users WHERE Username = ?", new String[] {username});
c.moveToFirst();

@SuppressLint("Range") User newUser = new User(
c.getInt(c.getColumnIndex("Id")),
c.getString(c.getColumnIndex("Username")),
c.getString(c.getColumnIndex("Password")),
c.getInt(c.getColumnIndex("CommunityId")),
c.getInt(c.getColumnIndex("IsCommunityOwner")) != 0, // != 0 is to convert to a bool
c.getInt(c.getColumnIndex("IsAdmin")) != 0);

c.close();

return newUser;
}

public int deleteUser(String username) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
String[] whereArgs = {username};

int count = db.delete("Users", "Username" + " = ?", whereArgs);
return count;
}



static class DatabaseHelper extends SQLiteOpenHelper {
private Context context;
private static final String DATABASE_NAME = "KomodoDB";

public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
this.context = context;
}

@Override
public void onCreate(@NonNull SQLiteDatabase sqLiteDatabase) {
String usersTable = "CREATE TABLE Users(\n" +
" Id INTEGER PRIMARY KEY,\n" +
" Username TEXT NOT NULL UNIQUE,\n" +
" Password TEXT NOT NULL,\n" +
" CommunityId INTEGER,\n" +
" IsCommunityOwner INTEGER NOT NULL,\n" +
" IsAdmin INTEGER NOT NULL\n" +
");";

String communitiesTable = "CREATE TABLE Communities(\n" +
" Id INTEGER PRIMARY KEY,\n" +
" Name TEXT NOT NULL UNIQUE\n" +
");\n";

String resourcesTable = "CREATE TABLE Resources(\n" +
" Id INTEGER PRIMARY KEY,\n" +
" CreatorId INTEGER NOT NULL,\n" +
" DataCreated TEXT NOT NULL,\n" +
" Type TEXT NOT NULL,\n" +
" Content TEXT NOT NULL\n" +
");";


sqLiteDatabase.execSQL(usersTable);
sqLiteDatabase.execSQL(communitiesTable);
sqLiteDatabase.execSQL(resourcesTable);
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

}

}
}
105 changes: 105 additions & 0 deletions app/src/main/java/com/example/komodohub/LoginActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.example.komodohub;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.example.komodohub.databinding.ActivityMainBinding;
import com.google.android.material.snackbar.Snackbar;

import org.w3c.dom.Text;

import java.util.Objects;

public class LoginActivity extends AppCompatActivity {
private DatabaseAdapter dbAdapter;
private TextView usernameBox;
private TextView passwordBox;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

dbAdapter = new DatabaseAdapter(LoginActivity.this);

EdgeToEdge.enable(this);

setContentView(R.layout.activity_login);

usernameBox = findViewById(R.id.login_username_box);
passwordBox = findViewById(R.id.login_password_box);
}

public void loginButton(View view) {
String username = "" + usernameBox.getText();
String password = "" + passwordBox.getText();

if(Objects.equals(username, "") || Objects.equals(password, ""))
{
ErrorToast("Please enter username and password");
}

var user = dbAdapter.getUserByName(username);

var userState = UserState.getInstance();
userState.setUser(user);
userState.setLoggedIn(true);


Intent myIntent = new Intent(this, SearchActivity.class);
startActivity(myIntent);
}

public void registerButton(View view) {
String username = "" + usernameBox.getText();
String password = "" + passwordBox.getText();

if(Objects.equals(username, "") || Objects.equals(password, ""))
{
ErrorToast("Please enter a username and password");
return;
}

var userId = dbAdapter.insertUser(username, password);
var user = dbAdapter.getUserByName(username);

var userState = UserState.getInstance();
userState.setUser(user);
userState.setLoggedIn(true);


Intent myIntent = new Intent(this, SearchActivity.class);
startActivity(myIntent);
}

public void noAccountButton(View view)
{
Intent myIntent = new Intent(this, SearchActivity.class);
startActivity(myIntent);
}

private void ErrorToast(String msg)
{
String toast_msg = "Error: ";

toast_msg = toast_msg + msg;

Toast t = Toast.makeText(this, toast_msg, Toast.LENGTH_SHORT);
t.show();
}
}
22 changes: 22 additions & 0 deletions app/src/main/java/com/example/komodohub/ProfileActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.komodohub;

import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

public class ProfileActivity extends AppCompatActivity {
private DatabaseAdapter dbAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

dbAdapter = new DatabaseAdapter(ProfileActivity.this);

EdgeToEdge.enable(this);

setContentView(R.layout.activity_profile);

}
}
28 changes: 28 additions & 0 deletions app/src/main/java/com/example/komodohub/SearchActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.komodohub;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import java.util.Objects;

public class SearchActivity extends AppCompatActivity {
private DatabaseAdapter dbAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

dbAdapter = new DatabaseAdapter(SearchActivity.this);

EdgeToEdge.enable(this);

setContentView(R.layout.activity_search);

}
}
41 changes: 41 additions & 0 deletions app/src/main/java/com/example/komodohub/UserState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.example.komodohub;

import com.example.komodohub.models.User;

public class UserState {
private static final UserState instance;

private User user;

public boolean isLoggedIn() {
return isLoggedIn;
}

public void setLoggedIn(boolean loggedIn) {
isLoggedIn = loggedIn;
}

private boolean isLoggedIn;


public User getUser() {
return user;
}

public void setUser(User myVar) {
this.user = myVar;
}


static {
instance = new UserState();
}

private UserState() {
}

public static UserState getInstance() {
return UserState.instance;
}

}
21 changes: 21 additions & 0 deletions app/src/main/java/com/example/komodohub/create_tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
CREATE TABLE Users(
Id INTEGER PRIMARY KEY,
Username TEXT NOT NULL UNIQUE,
Password TEXT NOT NULL,
CommunityId INTEGER,
IsCommunityOwner INTEGER NOT NULL,
IsAdmin INTEGER NOT NULL
);

CREATE TABLE Communities(
Id INTEGER PRIMARY KEY,
Name TEXT NOT NULL
);

CREATE TABLE Resources(
Id INTEGER PRIMARY KEY,
CreatorId INTEGER NOT NULL,
DataCreated TEXT NOT NULL,
Type TEXT NOT NULL,
Content TEXT NOT NULL
);
Loading

0 comments on commit 1b95887

Please sign in to comment.