-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
database working + added more activities
- Loading branch information
Matthew
committed
Nov 20, 2024
1 parent
1a38570
commit 1b95887
Showing
16 changed files
with
502 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
111 changes: 111 additions & 0 deletions
111
app/src/main/java/com/example/komodohub/DatabaseAdapter.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,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
105
app/src/main/java/com/example/komodohub/LoginActivity.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,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
22
app/src/main/java/com/example/komodohub/ProfileActivity.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,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
28
app/src/main/java/com/example/komodohub/SearchActivity.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,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); | ||
|
||
} | ||
} |
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,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; | ||
} | ||
|
||
} |
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,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 | ||
); |
Oops, something went wrong.